主题
BaseScript
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [BaseScript](2 属性)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
BaseScript 是 Script、LocalScript 与 ModuleScript 的抽象基类,公开脚本源代码文本。SourceCode 可在创建具体脚本子类时作为 config 字段提供。
适用场景
在需要检查场景中已有脚本单位时使用。例如排查某个脚本是否被禁用,或读取脚本保存的代码文本用于展示与校验。
使用要点
通过 game:CreateUnit("Script"/"LocalScript"/"ModuleScript", config) 创建具体脚本,并在 config 中提供 SourceCode;创建后可读取 SourceCode。
注意事项
BaseScript 本身不可直接创建。通过宿主接口取得实例后,只读取本页声明的 SourceCode;不要把已创建实例上的 SourceCode 当作常规可写属性。
代码示例
创建具体脚本并读取源代码
lua
-- @runtime client
local scriptUnit = game:CreateUnit('LocalScript', {
Name = 'ClientHello',
SourceCode = "print('hello from client script')",
})
print('脚本源码:', scriptUnit.SourceCode)
scriptUnit:Destroy()属性 (2)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
SourceCode | String | "" | 脚本的源代码字符串。创建时可通过 config 的 SourceCode 字段提供;创建后为只读,不支持在实例上写入。 |
IsEnabled | Bool | true | IsEnabled 是 BaseScript 上公开的布尔(Bool)属性,用于表示对应脚本是否处于启用状态。该属性可读取也可修改,Meta 中给出的默认值为 true。 |
