【问题标题】:Load Prefab and AnimationClips from different AssetBundle从不同的 AssetBundle 加载 Prefab 和 AnimationClip
【发布时间】:2018-09-27 14:05:38
【问题描述】:

我正在尝试从一个 AssetBundle 加载 Prefab,并从另一个加载相应的 AnimationClips。 至此从 AssetBundle 加载 Prefab 和 Instantiate 都是成功的。

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(Legacy动画)并将其添加到上面实例化的Gameobject中也是成功的。

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);
}

我是否遗漏了什么或应该如何做?

【问题讨论】:

  • 预制什么的?附有动画组件的网格?
  • @Programmer 是 Gameobject 网格和动画组件已附加。

标签: c# unity3d assetbundle


【解决方案1】:

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

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>();

现在,你可以玩了。

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    相关资源
    最近更新 更多