概要
方法
SubstituteGeometry(source: Instance):() |
API 參考
屬性
方法
SubstituteGeometry
參數
返回
()
範例程式碼
替代幾何體和放置約束
local GeometryService = game:GetService("GeometryService")
local mainPart = workspace.PurpleBlock
local otherParts = { workspace.BlueBlock }
local options = {
CollisionFidelity = Enum.CollisionFidelity.Default,
RenderFidelity = Enum.RenderFidelity.Automatic,
SplitApart = false,
}
local constraintOptions = {
tolerance = 0.1,
weldConstraintPreserve = Enum.WeldConstraintPreserve.All,
}
-- 在 pcall() 中執行聯集操作,因為它是非同步的
local success, newParts = pcall(function()
return GeometryService:UnionAsync(mainPart, otherParts, options)
end)
if success and #newParts > 0 and mainPart:IsA("PartOperation") then
-- 將結果操作中的第一個部分設置為用於替代的部分
-- 第一部分僅僅是一個選項;這可以是任何 PartOperation
local substitutePart = newParts[1]
-- 將部分重新定位到主部分的位置
substitutePart.CFrame = mainPart.CFrame
-- 計算要保留或放置的約束/附件
local recommendedTable = GeometryService:CalculateConstraintsToPreserve(mainPart, newParts, constraintOptions)
-- 用替代幾何體替代主部分的幾何體
mainPart:SubstituteGeometry(substitutePart)
-- 放置不會自動隨替代保留的約束/附件
for _, item in pairs(recommendedTable) do
if item.Attachment then
if item.ConstraintParent == nil then
item.Constraint.Parent = nil
end
if item.AttachmentParent == nil then
item.Attachment.Parent = nil
end
elseif item.WeldConstraint then
if item.Parent == nil then
item.WeldConstraint.Parent = nil
end
end
end
-- 銷毀其他部分
for _, otherPart in pairs(otherParts) do
otherPart.Parent = nil
otherPart:Destroy()
end
end