Skip to content

Latest commit

 

History

History
109 lines (83 loc) · 5.96 KB

TsComponent.md

File metadata and controls

109 lines (83 loc) · 5.96 KB

介绍

实现于类似于UnityEngine.MonoBehaviour功能的组件, 可用于挂载并序列化ts脚本成员声明, 将自动构建JSObject对象并实现其生命周期管理(TsComponentLifecycle).

依赖typescript的AST分析功能, 分析ts脚本获取class声明及其成员信息, 然后传递到C# EditorGUI使用.

使用需知

  • ts类型必需继承自xor.TsComponent丶export且不是abstract才可以被TsComponent使用;
  • ts类型成员必需使用declare修饰符或被xor.field修饰才能被序列;
  • 枚举类型如不指定value, 其默认值为0(System.Int32)或null(System.String);
  • ts分析服务和SerializedObject渲染只在Unity Editor环境下使用;
  • ts分析服务运行在子线程中(ThreadWorker), 请按照其已知缺陷进行设置, 否则可能导致crash;
  • ts分析服务运行在子线程中, 指定value时的表达式必需要能在子线程中访问: 例如UnityEngine.Vector2.right是可以的, 而UnityEngine.Application.dataPath不可以.

定义

[C#]继承: XOR.TsComponentXOR.TsBehaviour

接口详情
成员 描述
Puerts.JSObject JSObject{ get; } 其创建的Puerts.JSObject对象
方法 描述
static void Register(Puerts.JsEnv) 注册TsCompoent使用的Puerts.JsEnv实例
static void Unregister() 移除已注册的Puerts.JsEnv实例
static void GC() 回收未正常释放的XOR.TsComponent对象(例如使用Object.DestroyImmediate时, OnDestroy不会被正常调用)
static void PrintStatus() 打印所有实例状态(先执行一次GC)
XOR.Serializables.ResultPair[] GetProperties() 获取所有序列化成员
void SetProperty(string, object) (EditorOnly)设置键值
void SetPropertyListener(Action<string, object>) (EditorOnly)设置键值更新回调

[ts]继承: xor.TsComponentxor.TsBehaviour

接口详情
装饰器 描述
@xor.guid(string): ClassDecorator 定义组件guid(⚠⚠⚠此语句应由xor生成和管理, 与class声明绑定, 用户不应该手动创建丶修改)
@xor.route(string): ClassDecorator 定义组件路由(唯一值), 后续可使用此值获取j组件实例(相比较guid更符合人类阅读和记忆的习惯)
@xor.field({...}): PropertyDecorator 定义序列化字段详情, 可设置RawType丶默认值丶Range(仅限number)

内置类型

查看详情
类型 基础 数组
string
number
boolean
bigint
UnityEngine.Vector2
UnityEngine.Vector3
UnityEngine.Object及其子类型

其他类型请参照自定义类型

基础类型演示

示例场景:projects/Assets/Samples/01_TsComponent
示例typescript代码: projects/TsProject/src/samples/01_TsComponent.ts

image

数组类型演示

image

RawType丶默认值丶Range演示

image

枚举类型演示

枚举类型如不指定value, 其默认值为0(System.Int32)或null(System.String)

image

自定义扩展类型演示

相关代码请查看示例中的TsComponent.partialSerializablesEditor

image

双向绑定演示

在Sample06.OnGUI中修改this._value将会同步到Inspector, 反之在Inspector中修改_value字段亦会同步到Sample06._value上

image

ts引用类型绑定

//这里的_sample01,_sample03和_sample04是Proxy对象, 不可直接通过'==='进行判断. //调用xxx.valueOf() 获取原始js对象

let isSample01 = this._sample04?.valueOf() === this._sample01?.valueOf();
let isSample03 = this._sample04?.valueOf() === this._sample03?.valueOf();
console.log(`this._sample04: is sample01 = ${isSample01},  is sample03 = ${isSample03}`);

image

运行时方法(AddComponent/GetComponent)

image 输出打印如下: image

UGUI事件绑定

请查看UGUI页面