用animator控制animation并记录正播倒播

//父级脚本,挂在animator上

using UnityEngine;

using System.Collections;

public class UIAnimatorParent : MonoBehaviour
{
    //父级脚本
    //控制动画时长
    float animTime = 0.1f;
    //停止时间
    float stopTime = 0f;
    //是否开始播放动画
    bool isStart = false;
    public bool isBacking = false;
    Animator animator;
    string nextAnim;
    public string playName = "";
    private UIAnimator[] allUIA;

    void Awake()
    {
        animator = this.transform.GetComponent<Animator>();
        allUIA = transform.GetComponentsInChildren<UIAnimator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            PlayAnimation(playName, 1);
        }
        if (isBacking)
        {
            stopTime -= Time.deltaTime;
            //Debug.Log(animator.playbackTime);
            if (stopTime <= 0f)
            {
                isBacking = false;
                animator.StopPlayback();
                isStart = false;
                if (string.IsNullOrEmpty(nextAnim))
                {

                }
                else
                {
                    PlayAnimation(nextAnim, 1);
                    nextAnim = null;
                }
            }
            return;
        }
        //判断播放时长是否大于最大动画时长
        if (isStart)
        {
            if (stopTime > animTime)
            {

                AllStoped();
            }
            else
            {
                stopTime += Time.deltaTime;
            }
        }
    }
    //全部结束
    public void AllStoped()
    {
        isStart = false;
        stopTime = 0;
        animTime = 0.1f;
        //animator.CrossFade("default", 0);
        animator.StopPlayback();
        animator.StopRecording();
        animator.Stop();
        animator.enabled = false;
        for (int i = 0; i < allUIA.Length; i++)
        {
            allUIA[i].StopAnimation();
        }
        if (stopDelegate != null)
        {
            if (callDelegateName == playName)
            {
                stopDelegate();
                ClearDelegate();
            }
        }
        //所有动画播放完毕回调,动画播放完毕后开始正式执行逻辑
    }

    int lastPlayType = 1;
    public delegate void animStoped();
    animStoped stopDelegate;
    string callDelegateName;

    //清除结束回调
    public void ClearDelegate()
    {
        stopDelegate = null;
        callDelegateName = null;
    }
    //播放动画
    public void PlayAnimation(string _playName, int type = 1,animStoped delega = null)
    {
        playName = _playName;
        if (lastPlayType == 2)
        {
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值