Unity3d动画播放器

1、基本UI布局这里使用UGUI,Unity版本2017.1.0f3 

 

2、在Scrollbar添加按下抬起的监听

 

3、代码清单

3.1驱动类

using UnityEngine;
using UnityEngine.UI;

public class FootballDriver : MonoBehaviour
{
    public Animator animator;
    string playAnimatorName = "a07"; //clip in Animator
    public Text speedText;
    public Scrollbar scrollbarObject;
    public GameObject canvas;

    bool isManualState = false;  //手动播放
    bool isAutoState = false;    //自动播放
    float animatorTempSpeed = 1;     //存储播放速度 默认1

    public void UpdateAnimatorPostion()   //手动拖动 自动播放两种情况
    {
        float time = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
        float len = 0;

        foreach (AnimationClip clip in animator.runtimeAnimatorController.animationClips)
        {
            if (clip.name.Equals(playAnimatorName))
            {
                len = clip.length;
            }
        }

        if (scrollbarObject.value == 0)
        {
            speedText.text = "0";
        }

        //重置
        if (scrollbarObject.value >= 1)
        {
            animator.Play("idle1");
            animator.speed = 0;
            scrollbarObject.value = 0;
            speedText.text = "0";
            animatorTempSpeed = 1;
            Utility.FindObject(canvas, "start").gameObject.SetActive(true);
            Utility.FindObject(canvas, "stop").gameObject.SetActive(false);
        }
        else
        {
            if (isManualState)  //手动拖拽
            {
                animator.speed = 0;

                animator.Play(playAnimatorName, 0, scrollbarObject.value);
                Utility.FindObject(canvas, "speedText").GetComponent<Text>().text = "0";
            }

            if(isAutoState)
            {
                animator.Play(playAnimatorName, 0, scrollbarObject.value);
                Utility.FindObject(canvas, "speedText").GetComponent<Text>().text = "" + animatorTempSpeed;
            }
        }
    }

    public void SetAnimatorSpeed()
    {
        if (animatorTempSpeed == 1)
        {
            animatorTempSpeed = 0.8f;
        }
        else if (animatorTempSpeed == 0.8f)
        {
            animatorTempSpeed = 0.5f;
        }
        else
        {
            animatorTempSpeed = 1;
        }

        Utility.FindObject(canvas, "speedText").GetComponent<Text>().text = "" + animatorTempSpeed;

        if (!isManualState && isAutoState)
        {
            animator.speed = animatorTempSpeed;
        }
    }

    public void StartAnimatorPlay()
    {
        isAutoState = true;
        animator.speed = animatorTempSpeed;
        animator.Play(playAnimatorName, 0, scrollbarObject.value);

        Utility.FindObject(canvas, "start").gameObject.SetActive(false);
        Utility.FindObject(canvas, "stop").gameObject.SetActive(true);
    }
    public void StopAnimatorPlay()
    {
        isAutoState = false;
        animator.speed = 0;
        Utility.FindObject(canvas, "start").gameObject.SetActive(true);
        Utility.FindObject(canvas, "stop").gameObject.SetActive(false);
    }

    public void StartMenualPlay()
    {
        isManualState = true;
    }

    public void StopMenualPlay()
    {
        isManualState = false;
        if(isAutoState)
        {
            animator.speed = animatorTempSpeed;
            animator.Play(playAnimatorName, 0, scrollbarObject.value);
        }
    }

    void Update()
    {
        if (!isManualState)
        {
            if (animator.GetCurrentAnimatorStateInfo(0).IsName(playAnimatorName))
            {
                float time = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
                scrollbarObject.value = time;
            }
        }
    }
}

3.2 工具方法 查找未激活子物体

public class Utility{
    /// <summary>
    /// 查找子物体(包含未激活)
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static GameObject FindObject(GameObject parent, string name)
    {
        var trs = parent.GetComponentsInChildren(typeof(Transform), true);

        foreach (Transform t in trs)
        {
            if (t.name == name)
            {
                return t.gameObject;
            }
        }
        return null;
    }
}

 

posted @ 2017-08-24 14:24  GamePal  阅读(1320)  评论(0编辑  收藏  举报