unity Animator 添加事件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace WhackAMole
{
    /// <summary>
    /// 所有动物
    /// </summary>
    public class AnimalManage : MonoBehaviour
    {
        public Button SelfButton;
        public Animator Animator_;
        public  AnimationClip[] clips;
        float LingShiTime = 0;
        bool bo;//是否被打
        public virtual void Awake()
        {
            bo = false;
            SelfButton = this.GetComponent<Button>();
            Animator_ = this.GetComponent<Animator>();
            SelfButton.interactable = true;
            SelfButton.onClick.AddListener(AnimalDie);
        }
        public virtual void Start()
        {
            clips = Animator_.runtimeAnimatorController.animationClips;
            //AddAnimationEvent(Animator_, _clip.name, "StartEvent", 0);//开始播放动画
            //AddAnimationEvent(Animator_, _clip.name, "HalfEvent", _clip.length * 0.5f);//动画播放了一半
            AddAnimationEvent(Animator_, clips[0].name, "MaoTouEvent", clips[2].length);
            AddAnimationEvent(Animator_, clips[1].name, "RemainEvent", clips[2].length);
            AddAnimationEvent(Animator_, clips[2].name, "HuiDongEvent", clips[2].length);
            AddAnimationEvent(Animator_, clips[3].name, "DieEvent", clips[3].length);
            //for (int i = 0; i < clips.Length; i++)
            //{
            //    Debug.LogError("donghua名字=" + clips[i].name);
            //}
        }
        public virtual void Update()
        {
            LingShiTime += Time.deltaTime;
            if (LingShiTime >= GlobalVariable.RemainTime)
            {
                LingShiTime = 0;
                Huidong();
            }
        }

        /// <summary>
        /// 自己回洞
        /// </summary>
        public virtual void Huidong()
        {
            if (bo)
            {
                Debug.Log("被打死了,不执行回洞");
                return;
            }
            Debug.Log("未被打击,正常回洞!");
            SelfButton.interactable = false;
            Animator_.speed = GlobalVariable.AnimatorSpeed;
            Animator_.Play("HuiDong", 0, 0);//猫和老鼠
            //Destroy(this.gameObject, 1.6f);
            //SetBurrowsPools02();
            //Invoke("SetObj", 1.6f);
        }
        /// <summary>
        /// 被打
        /// </summary>
        public virtual void AnimalDie()
        {
            Debug.Log("被打了");

            SelfButton.interactable = false;
            bo = true;
            GlobalVariable.Score += 10;
            Animator_.speed = GlobalVariable.AnimatorSpeed;
            Animator_.Play("Die", 0, 0);
            // Destroy(this.gameObject,1.6f);
            //SetBurrowsPools02();
            //Invoke("SetObj", 1.6f);
        }

        /// <summary>
        /// 冒头动画
        /// </summary>
        public virtual void MaoTouEvent()
        {
            Debug.Log("冒头动画——播放完毕");
        }
        /// <summary>
        /// 地面停留动画
        /// </summary>
        public virtual void RemainEvent()
        {
            Debug.Log("地面停留动画——播放完毕");
            SetObj();
        }
        /// <summary>
        /// 回洞动画
        /// </summary>
        public  virtual void HuiDongEvent()
        {
            Debug.Log("回洞动画——播放完毕");
            SetObj();
        }
        /// <summary>
        /// 被打死动画
        /// </summary>
        public virtual void DieEvent()
        {
            Debug.Log("被打动画——播放完毕");
            SetObj();
        }
        
        public void SetObj()
        {
            bo = false;
            SelfButton.interactable = true;
            SetBurrowsPools02();
            if (this.gameObject.tag == ("Diglett"))
            {
                GameManage.Instance.SetDiglettObj(this.gameObject);
            }
            else
            {
                GameManage.Instance.SetCatObj(this.gameObject);
            }
            
        }
        public void SetBurrowsPools02()
        {
            for (int i = 0; i < GameManage.Instance.BurrowsPools02.Count; i++)
            {
                if (GameManage.Instance.BurrowsPools02[i].position == this.transform.position)
                {
                    switch (GlobalVariable.GameLevel)
                    {
                        case 1:
                            GameManage.Instance.BurrowsPools6.Add(GameManage.Instance.BurrowsPools02[i]);//添加位置
                            break;
                        default:
                            GameManage.Instance.BurrowsPools9.Add(GameManage.Instance.BurrowsPools02[i]);//添加位置
                            break;
                    }

                    GameManage.Instance.BurrowsPools02.Remove(GameManage.Instance.BurrowsPools02[i]);
                }
            }
        }

        /// <summary>
        /// 添加动画事件
        /// </summary>
        /// <param name="_animator"></param>
        /// <param name="_clipName">动画名称</param>
        /// <param name="_eventFunctionName">事件方法名称</param>
        /// <param name="_time">添加事件时间。单位:秒</param>
        private  void AddAnimationEvent(Animator _animator, string _clipName, string _eventFunctionName, float _time)
        {
            AnimationClip[] _clips = _animator.runtimeAnimatorController.animationClips;
            for (int i = 0; i < _clips.Length; i++)
            {
                if (_clips[i].name == _clipName)
                {
                    AnimationEvent _event = new AnimationEvent();
                    _event.functionName = _eventFunctionName;
                    _event.time = _time;
                    _clips[i].AddEvent(_event);
                    break;
                }
            }
            _animator.Rebind();
        }

        /// <summary>
        /// 清除所有事件
        /// </summary>
        public virtual void CleanAllEvent()
        {
            for (int i = 0; i < clips.Length; i++)
            {
                clips[i].events = default(AnimationEvent[]);
            }
            Debug.Log("清除所有事件");
        }


        public virtual void OnDestroy()
        {
            SelfButton.onClick.RemoveListener(AnimalDie);
        }


    }
}

猜你喜欢

转载自blog.csdn.net/qq_37524903/article/details/123421160