枚举

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

枚举 数据类型,或 Enum,是一个固定项的列表。您可以通过名为 Enum 的全局对象访问枚举。有关完整列表及其各自项,请参见 枚举

枚举项

要获取枚举的所有项,请在该枚举上调用 GetEnumItems() 方法。以下代码示例演示了如何在 Enum.PartType 枚举上调用 GetEnumItems()


local partTypes = Enum.PartType:GetEnumItems()
for index, enumItem in partTypes do
print(enumItem)
end
--[[
Enum.PartType.Ball
Enum.PartType.Block
Enum.PartType.Cylinder
Enum.PartType.Wedge
Enum.PartType.CornerWedge
]]

数据类型

EnumItem 是枚举项的数据类型。一个 EnumItem 有三个属性:

某些对象的属性只能是特定枚举的项。例如,Shape 属性的 Part 对象是 Enum.PartType 枚举的一个项。以下代码示例演示了如何打印 Enum.PartType.Cylinder 枚举项的属性。


print(Enum.PartType.Cylinder.Name) --> "Cylinder"
print(Enum.PartType.Cylinder.Value) --> 2
print(Enum.PartType.Cylinder.EnumType) --> PartType

要将 EnumItem 赋值为属性的值,请使用完整的 Enum 声明。您也可以将该项的 Name 属性作为字符串使用。


local Workspace = game:GetService("Workspace")
local part = Instance.new("Part") -- 创建一个新部件
part.Shape = Enum.PartType.Cylinder -- 通过完整的枚举项声明(最佳实践)
part.Shape = "Cylinder" -- 通过枚举项的名称作为字符串
part.Parent = Workspace
©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。