Загрузите Prefab и AnimationClips из разных AssetBundle - PullRequest
0 голосов
/ 26 сентября 2018

Я пытаюсь загрузить Prefab из одного AssetBundle и соответствующие ему AnimationClips из другого.Пока загрузка Prefab из AssetBundle и 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 (устаревшие анимации) и добавление его в вышеупомянутый экземпляр 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);
}

Я что-то упустил или как это должно быть сделано?

1 Ответ

0 голосов
/ 26 сентября 2018

Проблема заключается в том, что вы пытаетесь воспроизвести анимацию на префабе вместо экземпляра объекта:

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

Теперь вы можете играть в нее.

...