切换Animator Controller后AnimationClip内存释放问题

当Animator Controller挂载的动作比较多时,即使切换成另外一个动作很少的Animator Controller,任务的动作信息还是不会释放。
英雄自带的Animator Controller是挂了所有动作的。我们试了替换Runtime Animator Controller之后,老的AnimationClip内存并没有释放,引用计数也是增加的。

解决办法:
1)销毁Animator Controller 后,通过Unity Profiler立刻真机Take Sample,查看Animation Clip的Ref count是否为0;
2)如果不是,则需要进一步查看这些资源的索引出处;如果为0,则可以通过UnloadUnusedAssets或UnloadAsset来将其从内存中去除。

//Animator Controller从AC1切换到AC2
animator.runtimeAnimatorController = null;
Resources.UnloadUnusedAssets();         //释放AC1的AnimationClip
animator.runtimeAnimatorController = (RuntimeAnimatorcontroller)Resources.Load("AC2");

参考文档:
关于Unity动画系统优化,你可能遇到这些问题

猜你喜欢

转载自blog.csdn.net/crayon_chen/article/details/80773285