动画状态机
阅读时间:1分钟更新于 2025-08-12 18:39
Galacean Effects 编辑器中添加的状态机参数,在运行时可以通过代码动态设置参数值。
状态机参数设置
与设计师确定好状态机参数的名称后。可以通过合成的状态机组件来动态设置参数变量。
import { Animator } from '@galacean/effects';
// 从 JSON 加载场景并等待加载完成,返回场景的 composition 对象
const composition = await player.loadScene(json);
// 从场景中获取 Animator 组件,用于控制动画状态机
const animator = composition.getComponent(Animator);
// 设置名为 'Boolean1' 的布尔类型参数为 true
animator.setBool('Boolean1', true);
// 设置名为 'Number1' 的浮点数类型参数为 2
animator.setFloat('Number1', 2);
// 触发名为 'Trigger1' 的触发器参数
animator.setTrigger('Trigger1');
如果设计师在编辑器设定了状态机的事件监听,那么请引入相关代码模块,或自行监听元素点击事件,触发状态机参数。
Preview