我试图从一个AssetBundle加载Prefab,从另一个AnimationClips加载相应的Prefab。到目前为止,从AssetBundle加载预置文件和实例化都是成功的。
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
if (assetBundle == null) {
return;
}
GameObject prefab = assetBundle.LoadAsset<GameObject>(name);
Instantiate(prefab, targetTransform.position, targetTransform.rotation);
assetBundle.Unload(false);加载AnimationClips (遗产动画)并将其添加到上述实例化的游戏对象中也是成功的。
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
if (assetBundle == null) {
return;
}
List<AnimationClip> animationClips = new List<AnimationClip>();
foreach (string name in names) {
AnimationClip animationClip = assetBundle.LoadAsset<AnimationClip>(name);
if (animationClip != null) {
animationClips.Add(animationClip);
}
}
assetBundle.Unload(false);当我试图播放动画,它不工作,但我没有收到任何错误。
Animation animation = prefab.GetComponent<Animation>();
foreach (AnimationClip animationClip in animationClips) {
string clipName = animationClip.name;
animation.AddClip(animationClip, clipName);
}
foreach (AnimationClip animationClip in animationClips) {
string clipName = animationClip.name;
animation.PlayQueued(clipName, QueueMode.CompleteOthers);
}我错过了什么吗?该怎么做?
发布于 2018-09-26 14:09:43
问题是,您试图在预置对象上播放动画,而不是实例化对象:
GameObject prefab = assetBundle.LoadAsset<GameObject>(name);
//You instantiated object but did nothing with it. What's the point of the instantiation?
Instantiate(prefab, targetTransform.position, targetTransform.rotation);
//Don't do this. The Animation is attached to the prefab
Animation animation = prefab.GetComponent<Animation>();调用Instantiate函数时,它将返回实例化的对象。返回的对象应该用于获取Animation组件,然后播放您的动画。请注意,您的代码还没有完成,因此可能会出现其他问题,但这个问题也可能导致您的问题。
GameObject prefab = assetBundle.LoadAsset<GameObject>(name);
//Instantiate the prefab the return the instantiated object
GameObject obj = Instantiate(prefab, targetTransform.position, targetTransform.rotation);
//Get the Animation component from the instantiated prefab
Animation animation = obj.GetComponent<Animation>();现在,你可以弹了。
https://stackoverflow.com/questions/52519094
复制相似问题