Unity3D 获得 Animator 的 Trigger 和 Event

bool IsHaveTargetEvent(GameObject go, string triggerName)
    {
        Animator animator = go.GetComponentInChildren();
        Debug.Assert(animator != null, string.Format("{0} 缺少 Animator Com", go.name));
        if (animator == null)
        {
            return false;
        }

        UnityEditor.Animations.AnimatorController controller = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
        Debug.Assert(controller != null, string.Format("{0} 缺少 animator controller", go.name));
        if (controller == null)
        {
            return false;
        }

        UnityEditor.Animations.AnimatorStateMachine baseStateMachine = controller.layers[0].stateMachine;
        foreach (UnityEditor.Animations.AnimatorStateTransition t in baseStateMachine.anyStateTransitions)
        {
            foreach (UnityEditor.Animations.AnimatorCondition c in t.conditions)
            {
                if (triggerName == c.parameter)
                {
                    AnimationClip clip = t.destinationState.motion as AnimationClip;
                    if (clip == null)
                    {
                        return false;
                    }
                    AnimationEvent[] events = clip.events;
                    bool isHaveEndEvent = false;
                    foreach (AnimationEvent ev in events)
                    {
                        string functionName = ev.functionName;

                        if (functionName == Const.EVENT_NAME)
                        {
                            string strPar = ev.stringParameter;
                            if (strPar == Const.EVENT_END_NAME)
                            {
                                isHaveEndEvent = true;
                            }
                            
                        }
                    }
                    return isHaveEndEvent;
                }
            }
        }
        return false;
    }

你可能感兴趣的:(unity3d)