unity+动画状态机

代码如下:

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;
    }}

猜你喜欢

转载自www.cnblogs.com/WalkingSnail/p/11833902.html
今日推荐