Engine Class
ProceduralModel
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
สมาชิกที่ได้รับทอด
สมาชิก 26 คนที่รับทอดมาจาก Model
สมาชิก 4 คนที่รับทอดมาจาก PVInstance
สมาชิก 58 คนที่รับทอดมาจาก Instance
สมาชิก 6 คนที่รับทอดมาจาก Object
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
Generator
ตัวอย่างโค้ด
โมดูลเจนเนอเรเตอร์เริ่มต้น
-- โมดูลเจนเนอเรเตอร์คือ ModuleScript ที่กำหนดวิธีที่ ProceduralModel สร้างเนื้อหาของมัน
-- ดูที่ https://create.roblox.com/docs/reference/engine/classes/ProceduralModel สำหรับรายละเอียด
--
-- เพื่อใช้เป็นเจนเนอเรเตอร์ ModuleScript ต้องคืนค่าตารางที่มีสองฟิลด์:
-- * `Attributes` - ตารางของค่าพารามิเตอร์เริ่มต้น แต่ละคีย์จะกลายเป็นพารามิเตอร์ในทุก
-- ProceduralModel ที่ใช้เจนเนอเรเตอร์นี้ ผู้ใช้สามารถปรับแต่งพารามิเตอร์เหล่านี้
-- ในแผงคุณสมบัติเพื่อทำการสร้างใหม่ด้วยข้อมูลนำเข้าใหม่
-- * `OnGenerate` - ฟังก์ชันที่เอนจินเรียกเพื่อ (สร้างใหม่) เนื้อหาของโมเดล
--
-- ไม่มีอะไรเกี่ยวกับสคริปต์นี้หรือการวางตำแหน่งของมันที่พิเศษ มันสามารถอยู่ที่ไหนก็ได้ใน DataModel
-- และคุณสมบัติ `Generator` บน ProceduralModel สามารถถูกกำหนดใหม่เพื่อชี้ไปที่มัน
-- การระบุประเภทด้านล่างเป็นทางเลือก แต่ถูกบันทึกไว้ที่นี่เพื่อให้รูปแบบที่จำเป็นชัดเจน
-- `GenerationFunctionParams` จะถูกส่งไปยัง `OnGenerate` ทุกครั้งที่การสร้างทำงาน มันเปิดเผย:
-- * `Attributes` - ค่าพารามิเตอร์ปัจจุบันบน ProceduralModel
-- * `Size` - ปริมาตรที่ต้องสร้างภายใน (จาก `ProceduralModel.Size`)
-- * `Pause` - การหยุดชั่วคราวแบบร่วมมือ เรียก `parameters:Pause()` ภายในลูปยาวเพื่อให้การ
-- สร้างขนาดใหญ่ไม่หยุดการทำงานของเธรดที่เรียก; เอนจินอาจกลับมาหาคุณในภายหลัง
type GenerationFunctionParams<Attributes> = {
Attributes: Attributes,
Size: Vector3,
Pause: (self: GenerationFunctionParams<Attributes>) -> (),
}
-- `GeneratorModuleDefinition` คือตารางที่ ModuleScript ต้องคืนค่า
-- `OnGenerate` ต้องไม่คืนค่าผลลัพธ์; ให้สร้างอินสแตนซ์ที่สร้างขึ้นใน `targetContainer` แทน
-- สิ่งใดก็ตามที่ถูกสร้างใน `targetContainer` (หรือทายาทของมัน) จะกลายเป็นผลลัพธ์ของการสร้าง
type GeneratorModuleDefinition<Attributes> = {
Attributes: Attributes,
OnGenerate: (
parameters: GenerationFunctionParams<Attributes>,
targetContainer: GeneratedFolder
) -> (),
}
-- พารามิเตอร์เริ่มต้น แต่ละรายการที่นี่จะแสดงเป็นพารามิเตอร์ที่แก้ไขได้บน ProceduralModel
-- `RandomSeed` ที่คงที่ทำให้การสร้างมีความแน่นอน: ข้อมูลนำเข้าที่เหมือนกันจะผลิตผลลัพธ์ที่เหมือนกันเสมอ
-- ซึ่งสำคัญเพราะการสร้างอาจทำงานหลายครั้งเมื่อผู้ใช้แก้ไขคุณสมบัติ
local defaultAttributes = {
BorderThickness = 1,
RandomSeed = 142376088,
}
local Generator: GeneratorModuleDefinition<typeof(defaultAttributes)> = {
Attributes = defaultAttributes,
OnGenerate = function(parameters, targetContainer)
-- ข้อมูลนำเข้า `parameters.Attributes` สะท้อนสิ่งที่ผู้ใช้ตั้งค่าไว้ใน
-- ProceduralModel; `parameters.Size` คือกรอบที่ต้องสร้างเข้าไป
local borderThickness = parameters.Attributes.BorderThickness
local random = Random.new(parameters.Attributes.RandomSeed)
local size = parameters.Size
-- ค่าที่ได้จากการคำนวณ `random` ที่ตั้งค่าไว้ข้างต้นทำให้ค่าต่างๆ เหล่านี้สามารถทำซ้ำได้ตาม RandomSeed
local minSize = math.min(size.X, size.Y, size.Z)
local baseColor = Color3.fromHSV(random:NextNumber(0.5, 0.8), 0.7, 0.8)
local outlineColor = Color3.fromHSV(random:NextNumber(), 0.4, 0.25)
-- ตัวช่วยเล็กๆ เพื่อทำให้การสร้างด้านล่างอ่านจากบนลงล่าง คุณไม่จำเป็นต้อง
-- ตัวช่วยแบบนี้ในการเขียนเจนเนอเรเตอร์; มันช่วยลดการทำซ้ำ
local function assignProperties(instance: Instance, properties: { [string]: unknown })
-- ตั้งค่า Parent เป็นลำดับสุดท้ายเพื่อให้คุณสมบัติอื่นๆ อยู่ในที่ที่เหมาะสมก่อนที่อินสแตนซ์จะเข้าสู่
-- DataModel และทำให้เกิดเหตุการณ์ Changed
for name, value in properties do
if name ~= "Parent" then
(instance :: any)[name] = value
end
end
if properties.Parent ~= nil then
instance.Parent = properties.Parent :: Instance
end
return instance
end
local function createInstance(className: string, properties: { [string]: unknown })
return assignProperties(Instance.new(className), properties)
end
local function createPartWithDefaults(properties: { [string]: unknown })
-- ส่วนที่สร้างขึ้นมักจะถูกยึดและไม่ชนกัน; สามารถปรับแต่งต่อส่วนได้ตามต้องการ
local part = createInstance("Part", {
Anchored = true,
CanCollide = false,
Color = baseColor,
Material = Enum.Material.SmoothPlastic,
BottomSurface = Enum.SurfaceType.Smooth,
TopSurface = Enum.SurfaceType.Smooth,
})
return assignProperties(part, properties)
end
-- ทุกอย่างด้านล่างจะถูกสร้างใน `targetContainer` ซึ่งเป็นวิธีที่อินสแตนซ์กลายเป็น
-- ส่วนหนึ่งของผลลัพธ์ที่สร้างขึ้น อินสแตนซ์ที่ไม่ถูกสร้างที่นี่จะถูกทิ้ง
-- ส่วนที่มองไม่เห็นที่ใช้เป็นบิลบอร์ด
local bounds = createPartWithDefaults({
Name = "Bounds",
Parent = targetContainer,
Transparency = 1,
Size = size,
})
-- แพลตฟอร์มที่ยุบตัวพร้อมขอบด้านล่าง
local base = createPartWithDefaults({
Name = "PlatformBase",
Parent = targetContainer,
Color = baseColor,
CFrame = CFrame.new(0, (-size.Y + 1) / 2, 0),
Size = Vector3.new(
size.X - 2 * borderThickness,
1.01,
size.Z - 2 * borderThickness
),
})
local outline = createPartWithDefaults({
Name = "PlatformOutline",
Parent = targetContainer,
Color = outlineColor,
Position = Vector3.yAxis * (-size.Y + 1) / 2,
Size = Vector3.new(size.X, 1, size.Z),
})
-- "สวัสดีชาวโลก!" บิลบอร์ด เจนเนอเรเตอร์สามารถสร้างประเภทอินสแตนซ์ใดก็ได้ ไม่ใช่แค่ส่วน
local billboard = createInstance("BillboardGui", {
Name = "Text",
Parent = targetContainer,
Adornee = bounds,
LightInfluence = 0,
Size = UDim2.fromScale(minSize, minSize),
})
local textLabel = createInstance("TextLabel", {
Name = "HelloWorld",
Parent = billboard,
AnchorPoint = Vector2.new(0.5, 0.5),
BackgroundTransparency = 1,
Position = UDim2.fromScale(0.5, 0.5),
Size = UDim2.fromScale(1, 1),
Text = "สวัสดีชาวโลก!",
TextColor3 = baseColor,
TextScaled = true,
TextStrokeTransparency = 0,
})
local textStroke = createInstance("UIStroke", {
Parent = textLabel,
Color = outlineColor,
Thickness = borderThickness * 3,
})
end,
}
-- การคืนค่าคำนิยามโมดูลคือสิ่งที่ทำให้ ModuleScript นี้สามารถใช้งานได้เป็น `Generator`.
return Generatorโมดูลสร้างบันไดคลาสสิก
-- โมดูลสร้างเป็น ModuleScript ที่คืนค่าตารางที่มี `Attributes` และ `OnGenerate`.
-- การกำหนด ModuleScript นี้ให้กับคุณสมบัติ `Generator` ของ ProceduralModel จะเรียกใช้ `OnGenerate`
-- ทุกครั้งที่ขนาด รุ่น หรือแหล่งที่มาของโมดูลเปลี่ยนแปลง.
-- ดูที่ https://create.roblox.com/docs/reference/engine/classes/ProceduralModel.
local ClassicRobloxLadderGenerator = {}
-- ค่าคุณสมบัติเริ่มต้น ค่าที่แต่ละคีย์กลายเป็นคุณสมบัติที่แก้ไขได้บน ProceduralModel ใดๆ ที่
-- ใช้ตัวสร้างนี้ และแสดงในแผงคุณสมบัติของ Studio ภายใต้ Attributes การแก้ไข
-- ค่าของพวกมันทั้งหมดจะเรียกใช้ `OnGenerate` อีกครั้งด้วยค่าที่ใหม่.
ClassicRobloxLadderGenerator.Attributes = {
RungSpacing = 2,
RungThickness = 1,
PostSize = 1,
Color = Color3.new(0.5, 0.3, 0),
}
-- `OnGenerate` จะถูกเรียกโดยเอนจินเพื่อสร้างเนื้อหาของโมเดล.
-- สัญญา:
-- * `parameters.Size` - กล่องขอบเขต (จาก `ProceduralModel.Size`) ที่จะสร้างภายใน.
-- * `parameters.Attributes` - ค่าคุณสมบัติปัจจุบันบน ProceduralModel.
-- * `parameters:Pause()` - การหยุดชะงักร่วมมือ; เรียกใช้ภายในลูปยาวเพื่อให้การสร้างขนาดใหญ่
-- ไม่หยุดการทำงานของเธรด.
-- * `targetContainer` - สิ่งใดก็ตามที่เป็นพ่อแม่ที่นี่ (หรือไปยังลูกหลานของมัน) จะกลายเป็นผลลัพธ์.
-- * อย返回ค่าจาก `OnGenerate`; ผลลัพธ์จะสื่อสารผ่าน `targetContainer`.
ClassicRobloxLadderGenerator.OnGenerate = function(parameters, targetContainer)
local size = parameters.Size
local rungThickness = parameters.Attributes.RungThickness
local postSize = parameters.Attributes.PostSize
local count = (size.Y - rungThickness) // parameters.Attributes.RungSpacing + 1
local function createPart(position, size)
local part = Instance.new("Part")
part.Size = size
part.Position = position
part.Anchored = true
part.Color = parameters.Attributes.Color
part.TopSurface = Enum.SurfaceType.Smooth
part.BottomSurface = Enum.SurfaceType.Smooth
-- การเพิ่มเข้าไปใน `targetContainer` คือสิ่งที่เพิ่มชิ้นส่วนไปยังผลลัพธ์ที่สร้างขึ้น.
part.Parent = targetContainer
end
-- ขั้นบันไดที่วางซ้อนจากด้านล่างของกล่องขอบเขตขึ้นไป.
local position = Vector3.new(0, -size.Y / 2 + rungThickness / 2, 0)
for i = 1, count do
-- หยุดชะงักระหว่างขั้นบันไดเพื่อให้บันไดที่มีหลายขั้นไม่หยุดการทำงานของเธรด.
parameters:Pause()
createPart(position, Vector3.new(size.X - 2 * postSize, rungThickness, size.Z))
position += Vector3.new(0, parameters.Attributes.RungSpacing, 0)
end
-- เสาซ้ายและขวาที่ขยายจากความสูงทั้งหมด.
createPart(Vector3.new(-0.5 * size.X + postSize / 2, 0, 0), Vector3.new(postSize, size.Y, size.Z))
createPart(Vector3.new(0.5 * size.X - postSize / 2, 0, 0), Vector3.new(postSize, size.Y, size.Z))
end
-- การคืนค่าคำนิยามโมดูลคือสิ่งที่ทำให้ ModuleScript นี้ใช้งานได้เป็น `Generator`.
return ClassicRobloxLadderGeneratorวิธีการ