createScript()
创建一个 Entity-local Script Definition。
签名
ts
function createScript<T>(
name: string,
setup: (context: ScriptContext) => T,
options?: {
enabled?: boolean;
},
): ScriptDefinition<T>;ts
const Spin = createScript('spin', ({
entity,
onDestroy,
onUpdate,
}) => {
const speed = 0.8;
onUpdate(({ delta, world, views }) => {
entity.object.rotation.y += speed * delta;
});
onDestroy(() => console.log('removed'));
return { speed };
});
const spin = entity.addScript(Spin);
entity.setScriptEnabled(Spin, false);
entity.removeScript(Spin);Script setup 在 floating Entity 上也可完成,但 frame/Event effective gate 要求 Entity 进入 live World。Context 还提供 onLateUpdate/onResize/onEnable/onDisable 和 typed Event 方法。
Script 不提供 order,适合一个 Entity 的定制化行为;跨实体与场景级能力使用 System。