码迷,mamicode.com
首页 > 编程语言 > 详细

unity+动画状态机

时间:2019-11-11 12:35:30      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:tco   runtime   game   epo   screen   mon   inf   state   tcl   

代码如下:

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

public class ARModeAnimationController : MonoBehaviour {
    public Animator animatorTemp;
    public Animator OtherAnimatorTemp;
    public int ClickNumber;
    private double t1;
    private double t2;
    public bool isCurrentAnimator;
    public bool isCanPlayOpen=true;
    public GameObject[] HideGameObject;
    public GameObject[] HideSteam;
    public int isOnlyUIAnimation=0;
	public bool isELE;
    // Use this for initialization
    void Start()
    {
        //animatorTemp = GetComponent<Animator>();
        OnARHideGameobject(HideGameObject, true);
        OnARHideGameobject(HideSteam, false);
        isCanPlayOpen = true;
    }
    public void OnHideGameobject( bool isHide)
    {
        foreach (var item in HideGameObject)
        {
            Debug.LogError("hide");
            item.SetActive(isHide);
        }
    }
    public void OnARHideGameobject(GameObject[] hideObject, bool isHide)
    {
        foreach (var item in hideObject)
        {
            //Debug.LogError("hide");
            item.SetActive(isHide);
        }
    }
    //双击播放动画
    public void OnDoubleClickPlayAnimation()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = ARGlobalData.ARCameraInstance.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider.gameObject.layer == 8)
                {
                    if (hitInfo.collider.gameObject.tag == "Mode")
                    {
                        t2 = Time.realtimeSinceStartup;
                        if (t2 - t1 < 0.3)
                        {
                            if (isCanPlayOpen)
                            {
                                Debug.LogError("double click");

                                PlayAnimation();
                                ClickNumber++;
                            }
                            //Debug.LogError("click   ");
                        }
                        t1 = t2;
                    }
                }
            }
        }
        //if (isCurrentAnimator)
        //{
        //    Debug.LogError("current "+GetClipLength(animatorTemp, "Open"));
        //    AnimatorStateInfo info = animatorTemp.GetCurrentAnimatorStateInfo(0);
        //    Debug.LogError("info.normalizedTime" + info.normalizedTime);

        //    if (info.normalizedTime >= 1f)
        //    {
        //        Debug.LogError("哈哈哈"+ info.normalizedTime);
        //    }
        //}
    }
    // Update is called once per frame
    void Update()
    {
        //OnPlayAnimation();
        OnDoubleClickPlayAnimation();
    }
    //单击播放动画
    private void OnPlayAnimation()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = ARGlobalData.ARCameraInstance.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider.gameObject.layer == 8)
                {
                    if (hitInfo.collider.gameObject.tag == "Mode")
                    {
                        PlayAnimation();
                        ClickNumber++;
                        Debug.LogError("click   ");
                    }
                }
            }
        }

    }
    private void PlayAnimation()
    {
        if (ClickNumber % 2 == 0)
        {

            animatorTemp.SetBool("IsOpen", true);
            animatorTemp.SetBool("IsTake001", false);
            Debug.LogError(GetClipLength(animatorTemp, "Open"));
        }
        else
        {
            animatorTemp.SetBool("IsOpen", false);
            animatorTemp.SetBool("IsTake001", false);
            isCurrentAnimator = true;

        }
    }
    public void OnClickUIPlayAnimation(bool isPlay)
    {
        if(isOnlyUIAnimation==-1)
        {
			if (!isELE) 
			{
				///2018.9.11蒸汽箭头动画和设备一体
				isCurrentAnimator = true;

				animatorTemp.SetBool ("IsTake001", isPlay);
				animatorTemp.SetBool ("IsNewState", !isPlay);
			}
			else 
			{
				isCurrentAnimator = true;
				animatorTemp.SetBool ("IsTake001", isPlay);
				animatorTemp.SetBool ("IsNewState", !isPlay);
				animatorTemp.SetBool ("IsAroundClose", !isPlay);

			}

        }
        else if(isOnlyUIAnimation==1)
        { 
            /// 2018.9.11蒸汽箭头动画单独导入项目
            isCurrentAnimator = true;
            OtherAnimatorTemp.SetBool("IsSteam", isPlay);
            //OtherAnimator.SetBool("IsNewState", !isPlay);
        }


        /////2018.9.11蒸汽箭头动画和设备一体
        ////isCurrentAnimator = true;

        ////animatorTemp.SetBool("IsTake001", isPlay);
        ////animatorTemp.SetBool("IsNewState", !isPlay);
        ///// 2018.9.11蒸汽箭头动画单独导入项目
        //isCurrentAnimator = true;
        //OtherAnimatorTemp.SetBool("IsSteam", isPlay);
        ////OtherAnimator.SetBool("IsNewState", !isPlay);
    }

    //得到当前动画状态机中动画片段的时长
    public float GetClipLength(Animator animator, string clip)
    {
        if (null == animator || string.IsNullOrEmpty(clip) || null == animator.runtimeAnimatorController)
        {
            Debug.LogError("0");
            return 0f;
        }
        RuntimeAnimatorController ac = animator.runtimeAnimatorController;
        AnimationClip[] tAnimationClipS = ac.animationClips;
        if (null == tAnimationClipS || tAnimationClipS.Length <= 0)
        {
            Debug.LogError("0");

            return 0f;
        }
        AnimationClip tAnimationClip;
        for (int tCounter = 0, tLen = tAnimationClipS.Length; tCounter < tLen; tCounter++)
        {
            tAnimationClip = ac.animationClips[tCounter];
            if (null != tAnimationClip && tAnimationClip.name == clip)
            {

                //Debug.LogError("0" + tAnimationClip.length);
                return tAnimationClip.length;
            }
        }
        Debug.LogError("0");

        return 0f;

    }
}

  

using System.Collections;using System.Collections.Generic;using UnityEngine;
public class ARModeAnimationController : MonoBehaviour {    public Animator animatorTemp;    public Animator OtherAnimatorTemp;    public int ClickNumber;    private double t1;    private double t2;    public bool isCurrentAnimator;    public bool isCanPlayOpen=true;    public GameObject[] HideGameObject;    public GameObject[] HideSteam;    public int isOnlyUIAnimation=0;public bool isELE;    // Use this for initialization    void Start()    {        //animatorTemp = GetComponent<Animator>();        OnARHideGameobject(HideGameObject, true);        OnARHideGameobject(HideSteam, false);        isCanPlayOpen = true;    }    public void OnHideGameobject( bool isHide)    {        foreach (var item in HideGameObject)        {            Debug.LogError("hide");            item.SetActive(isHide);        }    }    public void OnARHideGameobject(GameObject[] hideObject, bool isHide)    {        foreach (var item in hideObject)        {            //Debug.LogError("hide");            item.SetActive(isHide);        }    }    //双击播放动画    public void OnDoubleClickPlayAnimation()    {        if (Input.GetMouseButtonDown(0))        {            Ray ray = ARGlobalData.ARCameraInstance.ScreenPointToRay(Input.mousePosition);            RaycastHit hitInfo;            if (Physics.Raycast(ray, out hitInfo))            {                if (hitInfo.collider.gameObject.layer == 8)                {                    if (hitInfo.collider.gameObject.tag == "Mode")                    {                        t2 = Time.realtimeSinceStartup;                        if (t2 - t1 < 0.3)                        {                            if (isCanPlayOpen)                            {                                Debug.LogError("double click");
                                PlayAnimation();                                ClickNumber++;                            }                            //Debug.LogError("click   ");                        }                        t1 = t2;                    }                }            }        }        //if (isCurrentAnimator)        //{        //    Debug.LogError("current "+GetClipLength(animatorTemp, "Open"));        //    AnimatorStateInfo info = animatorTemp.GetCurrentAnimatorStateInfo(0);        //    Debug.LogError("info.normalizedTime" + info.normalizedTime);
        //    if (info.normalizedTime >= 1f)        //    {        //        Debug.LogError("哈哈哈"+ info.normalizedTime);        //    }        //}    }    // Update is called once per frame    void Update()    {        //OnPlayAnimation();        OnDoubleClickPlayAnimation();    }    //单击播放动画    private void OnPlayAnimation()    {        if (Input.GetMouseButtonDown(0))        {            Ray ray = ARGlobalData.ARCameraInstance.ScreenPointToRay(Input.mousePosition);            RaycastHit hitInfo;            if (Physics.Raycast(ray, out hitInfo))            {                if (hitInfo.collider.gameObject.layer == 8)                {                    if (hitInfo.collider.gameObject.tag == "Mode")                    {                        PlayAnimation();                        ClickNumber++;                        Debug.LogError("click   ");                    }                }            }        }
    }    private void PlayAnimation()    {        if (ClickNumber % 2 == 0)        {
            animatorTemp.SetBool("IsOpen", true);            animatorTemp.SetBool("IsTake001", false);            Debug.LogError(GetClipLength(animatorTemp, "Open"));        }        else        {            animatorTemp.SetBool("IsOpen", false);            animatorTemp.SetBool("IsTake001", false);            isCurrentAnimator = true;
        }    }    public void OnClickUIPlayAnimation(bool isPlay)    {        if(isOnlyUIAnimation==-1)        {if (!isELE) {///2018.9.11蒸汽箭头动画和设备一体isCurrentAnimator = true;
animatorTemp.SetBool ("IsTake001", isPlay);animatorTemp.SetBool ("IsNewState", !isPlay);}else {isCurrentAnimator = true;animatorTemp.SetBool ("IsTake001", isPlay);animatorTemp.SetBool ("IsNewState", !isPlay);animatorTemp.SetBool ("IsAroundClose", !isPlay);
}
        }        else if(isOnlyUIAnimation==1)        {             /// 2018.9.11蒸汽箭头动画单独导入项目            isCurrentAnimator = true;            OtherAnimatorTemp.SetBool("IsSteam", isPlay);            //OtherAnimator.SetBool("IsNewState", !isPlay);        }

        /////2018.9.11蒸汽箭头动画和设备一体        ////isCurrentAnimator = true;
        ////animatorTemp.SetBool("IsTake001", isPlay);        ////animatorTemp.SetBool("IsNewState", !isPlay);        ///// 2018.9.11蒸汽箭头动画单独导入项目        //isCurrentAnimator = true;        //OtherAnimatorTemp.SetBool("IsSteam", isPlay);        ////OtherAnimator.SetBool("IsNewState", !isPlay);    }
    //得到当前动画状态机中动画片段的时长    public float GetClipLength(Animator animator, string clip)    {        if (null == animator || string.IsNullOrEmpty(clip) || null == animator.runtimeAnimatorController)        {            Debug.LogError("0");            return 0f;        }        RuntimeAnimatorController ac = animator.runtimeAnimatorController;        AnimationClip[] tAnimationClipS = ac.animationClips;        if (null == tAnimationClipS || tAnimationClipS.Length <= 0)        {            Debug.LogError("0");
            return 0f;        }        AnimationClip tAnimationClip;        for (int tCounter = 0, tLen = tAnimationClipS.Length; tCounter < tLen; tCounter++)        {            tAnimationClip = ac.animationClips[tCounter];            if (null != tAnimationClip && tAnimationClip.name == clip)            {
                //Debug.LogError("0" + tAnimationClip.length);                return tAnimationClip.length;            }        }        Debug.LogError("0");
        return 0f;
    }}

 

unity+动画状态机

标签:tco   runtime   game   epo   screen   mon   inf   state   tcl   

原文地址:https://www.cnblogs.com/WalkingSnail/p/11833902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!