我想出了如何创建包含动画片段的资源包,然后将该动画片段加载到另一个项目中,然后通过脚本覆盖现有动画片段。基本上,目标是允许用户编辑动画。
现在一切都值得庆幸,但由于某种原因,速度有所放缓。我不确定在开始帧中加载所有内容时导致速度减慢的原因是什么:
这是我的代码:
protected Animator animator;
protected AnimatorOverrideController animatorOverrideController;
protected AnimationClipOverrides clipOverrides;
void Start()
{
animator = GetComponent<Animator>();
animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
animator.runtimeAnimatorController = animatorOverrideController;
clipOverrides = new AnimationClipOverrides(animatorOverrideController.overridesCount);
animatorOverrideController.GetOverrides(clipOverrides);
LoadAnimation();
}
public void LoadAnimation()
{
StartCoroutine(LoadAnimClip());
}
IEnumerator LoadAnimClip(string )
{
var FilePath = Application.streamingAssetsPath + "/Characters/Sonic/Animations/stand";
var req = AssetBundle.LoadFromFileAsync(FilePath);
yield return req;
AssetBundle animationAsset = req.assetBundle;
AnimationClip animation = animationAsset.LoadAsset<AnimationClip>("Stand") as AnimationClip;
clipOverrides["Idle"] = animation;
animatorOverrideController.ApplyOverrides(clipOverrides);
}
如果您想知道animationclipoverrides是什么,这里是类:
public class AnimationClipOverrides : List<KeyValuePair<AnimationClip, AnimationClip>
{
public AnimationClipOverrides(int capacity) : base(capacity) { }
public AnimationClip this[string name]
{
get { return this.Find(x => x.Key.name.Equals(name)).Value; }
set
{
int index = this.FindIndex(x => x.Key.name.Equals(name));
if (index != -1)
this[index] = new KeyValuePair<AnimationClip, AnimationClip>(this[index].Key, value);
}
}
}