Editor自动生成Animator Controller State,使用new ChildAnimatorState()状态保存无效的问题

以下代码无法正确保存生成的IdleState

private void CreateLayerStates(AnimatorStateMachine sm,AnimationClip idleAnim)
{
List states = new List();
ChildAnimatorState idleState = new ChildAnimatorState();
idleState.state.motion = idleAnim;
states.Add(idleState);
sm.states = states.ToArray();
}

需要使用AddState方法

private void CreateLayerStates(AnimatorStateMachine sm,AnimationClip idleAnim)
{
AnimatorState idleState = sm.AddState(idleStateName);
idleState.motion = idleAnim;
}

可能的原因

使用new生成的实例没有被unity正确的序列化到磁盘上,而AddState内部实现了序列化功能

使用AddTransition

同样的,以下代码也不会被正确保存

AnimatorStateTransition talkToIdle = new AnimatorStateTransition()
            {
                destinationState = idleState,
                hasExitTime = true
            };
            talkState.AddTransition(talkToIdle);

需要改成

AnimatorStateTransition talkToIdle = talkState.AddTransition(idleState);
talkToIdle.hasExitTime = true;

上一篇:【算法图解】——集合覆盖问题


下一篇:线程安全问题 - 发布与逸出