Skip to content

zh CN scripting api

nullus-docs-bot edited this page Oct 14, 2026 · 3 revisions

脚本 API 参考

中文 | English | Docs | Repository

本页列出当前脚本清单中可用的类型与成员。清单文件是 Library/ScriptApi/ScriptApi.json,schema 版本为 2,C# 与 Lua 由同一份清单生成绑定。

Object

所有引擎对象的基类。

成员 说明
name 对象名
GetInstanceID() 实例 ID
Destroy(Object, delay = 0) 请求销毁,可延迟秒数
Instantiate(GameObject) 克隆对象层级
相等性比较 按句柄比较,已销毁对象视为空

失效或已销毁对象继续访问会抛出失效引用错误,不要缓存并复用已销毁对象。

Component

成员 说明
gameObject 宿主对象
transform 宿主对象的 Transform
tag 宿主对象标签
CompareTag(string) 标签比较
GetComponent<T>() 取同对象上的组件

Behaviour

脚本组件基类。

成员 说明
enabled 组件自身启用状态
isActiveAndEnabled 自身启用且宿主在层级上激活

GameObject

成员 说明
GameObject(string name = "New GameObject", string tag = "") 新建对象
name 读写名字
tag 读写标签
layer 读写层
activeSelf 自身激活状态
activeInHierarchy 层级激活状态
transform Transform 组件
SetActive(bool) 切换激活状态
AddComponent<T>() 添加组件
GetComponent<T>() 取组件
GameObject.Find(string) 按名字查找
GameObject.FindWithTag(string) 按标签查找
CreatePrimitive(string type = "Cube") 创建图元

Transform

成员 说明
position / rotation 世界空间位置与旋转
localPosition / localRotation 局部空间位置与旋转
eulerAngles / localEulerAngles 欧拉角读写
localScale / lossyScale 缩放
parent / root 父子关系
forward / right / up 朝向向量
Translate(Vector3) 平移,默认相对自身
Rotate(Vector3 eulerAngles) 旋转,默认相对自身
LookAt(Vector3) / LookAt(Transform) 朝向目标
TransformPoint / InverseTransformPoint 点空间转换
TransformDirection / InverseTransformDirection 方向空间转换

角度使用度数;Quaternion 使用 (x, y, z, w) 分量。

Camera

成员 说明
fieldOfView 透视视场角
orthographic 是否正交投影
orthographicSize 正交尺寸
nearClipPlane / farClipPlane 裁剪面
clearColor 清屏颜色

Light

成员 说明
color 颜色
intensity 强度
range 影响范围
spotAngle 外锥角
type 光源类型

UICanvasComponent

UI 元素按名字寻址。当前脚本成员:

成员 说明
LoadDocumentScript(string) 加载 UI 文档
GetDocumentGeneration() 取文档代次
GetElementHandle(string) 由名字取元素句柄
GetElementText(h) / SetElementText(h, string) 读写文本
GetElementValue(h) / SetElementValue(h, float) 读写数值
GetElementChecked(h) / SetElementChecked(h, bool) 读写勾选状态
FocusElement(h) 设置焦点
GetRenderModeValue() / SetRenderModeValue(uint) 渲染模式
GetWorldRenderPathValue() / SetWorldRenderPathValue(uint) 世界空间渲染路径
GetSortOrderScript() / SetSortOrderScript(int) 排序序号
GetSortLayer() / SetSortLayer(int) 排序层
GetCameraId() / SetCameraId(uint64) 关联相机
GetDpiMatch() / SetDpiMatch(float) DPI 匹配
GetPixelsPerUnit() / SetPixelsPerUnit(float) 像素密度
GetDepthTest() / SetDepthTest(bool) 深度测试
GetDepthWrite() / SetDepthWrite(bool) 深度写入
GetAlwaysOnTop() / SetAlwaysOnTop(bool) 置顶显示

Time

成员 说明
deltaTime 受 timeScale 影响
unscaledDeltaTime 不受 timeScale 影响
time / unscaledTime 累计时间
frameCount / fixedFrameCount 帧计数
timeScale 时间缩放
fixedDeltaTime 固定步长,默认 0.02
maximumDeltaTime 单帧最大步长,默认 0.1

固定更新在普通更新之前执行。

SceneManager 与 Scene

成员 说明
SceneManager.GetActiveScene() 取活动场景
SceneManager.LoadScene(string path) 请求加载场景
Scene.name / path / buildIndex / isLoaded 场景信息
Scene.IsValid() 是否已加载

LoadScene 是单活动场景的同步请求,在当前脚本帧结束后切换。

Input

成员 说明
GetKey(KeyCode) / GetKeyDown / GetKeyUp 键盘
GetMouseButton(int) / GetMouseButtonDown / GetMouseButtonUp 鼠标按键
mousePosition 鼠标位置
mouseScrollDelta 滚轮增量

Debug

成员 说明
Log / LogWarning / LogError 写日志
LogException 记录异常
Assert(bool, message) 断言失败时记录错误

Lua 运行时错误会进入脚本诊断与编辑器 Console。Lua 脚本用语言自身的 error 产生可定位错误。

数学类型

Vector2、Vector3、Vector4、Quaternion、Color、Rect、Vector2Int、Vector3Int、RectInt、Bounds、BoundsInt、Ray 与 Mathf 提供小写字段、常量、运算符与常用函数。

清单与边界

  • Library/ScriptApi/ScriptApi.json 是合并后的 API 清单,schema 版本 2;
  • 各模块的源清单位于对应的 */Gen/ScriptApi.json;
  • 不支持的可移植签名在构建期被拒绝,不会被静默跳过。

当前清单只包含引擎侧反射类型。脚本自定义类型间的互引用、协程、资源加载、物理与音频 API 尚未纳入。

下一步

Nullus Engine

Home

中文

English

Clone this wiki locally