概要
属性
UniqueId:UniqueId |
方法
活动
AncestryChanged(child: Instance,parent: Instance):RBXScriptSignal |
AttributeChanged(attribute: string):RBXScriptSignal |
ChildAdded(child: Instance):RBXScriptSignal |
childAdded(child: Instance):RBXScriptSignal |
ChildRemoved(child: Instance):RBXScriptSignal |
DescendantAdded(descendant: Instance):RBXScriptSignal |
DescendantRemoving(descendant: Instance):RBXScriptSignal |
继承成员
6 个继承自 Object
API 参考
属性
Archivable
代码示例
可归档
local part = Instance.new("Part")
print(part:Clone()) --> 部件
part.Archivable = false
print(part:Clone()) --> nilarchivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
方法
children
ClearAllChildren
Instance:ClearAllChildren():()
返回
()
Clone
返回
代码示例
克隆一个实例
local Workspace = game:GetService("Workspace")
-- 获取对现有对象的引用
local model = script.Parent.Model
-- 创建模型的克隆
local clone = model:Clone()
-- 移动克隆,以便它不会与原始模型重叠
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- 将克隆添加到工作区
clone.Parent = Workspaceclone
Destroy
Instance:Destroy():()
返回
()
代码示例
Instance:Destroy()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
FindFirstChildOfClass
参数
返回
代码示例
Instance:FindFirstChildOfClass
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
endFindFirstChildWhichIsA
GetAttributeChangedSignal
参数
GetChildren
Instance:GetChildren():Instances
返回
Instances
代码示例
Instance:GetChildren
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
endgetChildren
GetDebugId
GetDescendants
Instance:GetDescendants():Instances
返回
Instances
代码示例
Instance:GetDescendants
local descendants = workspace:GetDescendants()
-- 遍历工作区的所有后代。如果找到一个
-- BasePart,代码将该部件的颜色更改为绿色
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
endGetFullName
返回
代码示例
Instance:GetFullName
-- 创建一个简单的层次结构
local model = Instance.new("Model")
local part = Instance.new("Part")
part.Parent = model
local fire = Instance.new("Fire")
fire.Parent = part
print(fire:GetFullName()) --> Model.Part.Fire
model.Parent = workspace
print(fire:GetFullName()) --> Workspace.Model.Part.Fire
part.Name = "你好,世界"
print(fire:GetFullName()) --> Workspace.Model.你好,世界.Fire实例:获取完整名称 Lua 实现
local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- 添加父级名称
result = object.Name .. "." .. result
-- 向上遍历层级
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.CameraGetStyled
返回
Variant
代码示例
获取样式
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local rule = coreSheet:FindFirstChildWhichIsA("StyleRule")
rule.Selector = "TextButton"
-- 引用一个按钮
local button = HUDContainer:FindFirstChildWhichIsA("TextButton")
print(button:GetStyled("Rotation")) --> 0 (默认值)
print(button.Rotation) --> 0 (默认值)
-- 通过样式规则属性应用旋转
rule:SetProperty("Rotation", 30)
print(button:GetStyled("Rotation")) --> 30 (基于规则的样式值)
print(button.Rotation) --> 0 (默认值)
-- 显式修改/覆盖样式属性
button.Rotation = 45
print(button:GetStyled("Rotation")) --> 45 (修改后的值)
print(button.Rotation) --> 45 (修改后的值)GetStyledPropertyChangedSignal
参数
代码示例
获取样式属性更改信号
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("样式属性已更改!")
end
meterBar:GetStyledPropertyChangedSignal("AnchorPoint"):Connect(stylePropertyChanged)IsAncestorOf
参数
返回
代码示例
Instance:IsAncestorOf()
local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- 这些语句为真
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- 这些语句为假
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))IsDescendantOf
参数
返回
代码示例
Instance:IsDescendantOf
local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> false
part.Parent = workspace
print(part:IsDescendantOf(game))
--> true
part.Parent = game
print(part:IsDescendantOf(game))
--> trueisDescendantOf
Remove
remove
SetAttribute
活动
AncestryChanged
代码示例
Instance.AncestryChanged
local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- 根据其 Parent 更改 changingPart 的颜色
local function onAncestryChanged(part: Part, parent: Instance)
if parent == redPart then
changingPart.Color = Color3.new(1, 0, 0)
elseif parent == bluePart then
changingPart.Color = Color3.new(0, 0, 1)
else
changingPart.Color = Color3.new(1, 1, 1)
end
print(`{part.Name} 现在的父级是 {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- 随着时间的推移,将 changingPart 的 Parent 属性设置为不同的实例
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
endChildAdded
参数
代码示例
Instance.ChildAdded
local function onChildAdded(instance)
print(instance.Name .. " 添加到工作区")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> 部件添加到工作区childAdded
ChildRemoved
参数
代码示例
Instance.ChildRemoved
local function onChildRemoved(instance)
print(instance.Name .. " 从工作区移除")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()DescendantAdded
参数
代码示例
Instance.DescendantAdded
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspaceDescendantRemoving
参数
代码示例
Instance.DescendantRemoving
workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " 当前父级为 " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> 部件当前父级为工作区
print(part.Parent)
--> nilDestroying
代码示例
使用销毁事件(即时信号)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("暂停前:", part:GetFullName(), #part:GetChildren())
task.wait()
print("暂停后:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()使用销毁事件(延迟信号)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("在信号中:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("在销毁之前:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("在销毁之后:", part:GetFullName(), #part:GetChildren())StyledPropertiesChanged
代码示例
样式属性已更改
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("样式属性已更改")
end
meterBar:StyledPropertiesChanged():Connect(stylePropertyChanged)