首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从不同的AnimationClips和AssetBundle加载预制件

从不同的AnimationClips和AssetBundle加载预制件
EN

Stack Overflow用户
提问于 2018-09-26 13:37:10
回答 1查看 986关注 0票数 1

我试图从一个AssetBundle加载Prefab,从另一个AnimationClips加载相应的Prefab。到目前为止,从AssetBundle加载预置文件和实例化都是成功的。

代码语言:javascript
复制
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 (遗产动画)并将其添加到上述实例化的游戏对象中也是成功的。

代码语言:javascript
复制
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);

当我试图播放动画,它不工作,但我没有收到任何错误。

代码语言:javascript
复制
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);
}

我错过了什么吗?该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-26 14:09:43

问题是,您试图在预置对象上播放动画,而不是实例化对象:

代码语言:javascript
复制
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组件,然后播放您的动画。请注意,您的代码还没有完成,因此可能会出现其他问题,但这个问题也可能导致您的问题。

代码语言:javascript
复制
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>();

现在,你可以弹了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52519094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档