首页 > 代码库 > 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(); }}
Animator组件关闭再打开后参数丢失问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。