首页 > 代码库 > Animator组件关闭再打开后参数丢失问题

Animator组件关闭再打开后参数丢失问题

问题如下,因为再激活Animator时,它会重置一次,参数也会丢失

技术分享

 

这个问题一直存在,论坛给出的解释是把参数缓存下来,在激活时重置

http://answers.unity3d.com/questions/919995/parameters-and-states-of-animator-reset-on-objects.html

 

我把论坛的脚本修改了一下,在需要关闭GameObject时添加该脚本,再次打开GameObject时会自动删除。

即可解决参数丢失问题

 

技术分享
//Modify from: http://answers.unity3d.com/questions/919995/parameters-and-states-of-animator-reset-on-objects.htmlusing UnityEngine;using System.Collections.Generic;public class AnimParamRecover : MonoBehaviour{    class AnimCacheParam    {        public AnimatorControllerParameterType type;        public string paramName;        object mData;        public AnimCacheParam(Animator anim, string paramName, AnimatorControllerParameterType type)        {            this.type = type;            this.paramName = paramName;            switch (type)            {                case AnimatorControllerParameterType.Int:                    this.mData = http://www.mamicode.com/(int)anim.GetInteger(paramName);                    break;                case AnimatorControllerParameterType.Float:                    this.mData = http://www.mamicode.com/(float)anim.GetFloat(paramName);                    break;                case AnimatorControllerParameterType.Bool:                    this.mData = http://www.mamicode.com/(bool)anim.GetBool(paramName);                    break;            }        }        public object getData()        {            return mData;        }    }    Animator mAnimator;    List<AnimCacheParam> mParamsCacheList;    bool mIsDisable;    void Awake()    {        Record();    }    /// <summary>    /// Recovery.    /// </summary>    void OnEnable()    {        if (mIsDisable)        {            mIsDisable = false;        }        else        {            return;        }        Recovery();        Destroy(this);    }    void OnDisable()    {        mIsDisable = true;    }    [ContextMenu("Manual Record")]    public void Record()    {        mParamsCacheList = new List<AnimCacheParam>();        mAnimator = GetComponent<Animator>();        for (int i = 0; i < mAnimator.parameters.Length; i++)        {            var p = mAnimator.parameters[i];            var animParam = new AnimCacheParam(mAnimator, p.name, p.type);            mParamsCacheList.Add(animParam);        }    }    [ContextMenu("Manual Recovery")]    public void Recovery()    {        foreach (var param in mParamsCacheList)        {            switch (param.type)            {                case AnimatorControllerParameterType.Int:                    mAnimator.SetInteger(param.paramName, (int)param.getData());                    break;                case AnimatorControllerParameterType.Float:                    mAnimator.SetFloat(param.paramName, (float)param.getData());                    break;                case AnimatorControllerParameterType.Bool:                    mAnimator.SetBool(param.paramName, (bool)param.getData());                    break;            }        }        mParamsCacheList.Clear();    }}
AnimParamRecover

 

Animator组件关闭再打开后参数丢失问题