zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    序列化与反序列化
    SQL Server 中常见的十张系统表
    DataGridView的18中用法
    文件操作
    数据库的学习
    GUI图形图像编程主要技术的学习梳理
    软件研发过程中常用图形总结
    windows10 升级1803后,远程错误提示“出现身份验证错误,要求的函数不受支持 CredSSP 加密 Oracle修正”的解决办法
    SQLServer中按照任意分钟(5分钟、10分钟)分组统计数据
    支付宝APP支付,提示代码 ALIN10070
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/7422853.html
Copyright © 2011-2022 走看看