0

In the manual of AssetBundle usage, https://docs.unity3d.com/Manual/AssetBundles-Native.html

I can see that when use LoadAsset loaded an GameObject, why we need to do the extra step Instantiate it ? this will create another copy of the prefab, I'm confuse why we do not use the loaded prefab GameObject directly?

public class LoadFromFileExample extends MonoBehaviour {
    function Start() {
        var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
        if (myLoadedAssetBundle == null) {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
        Instantiate(prefab); // why do not use prefab directly ?
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Liu Hao
  • 472
  • 4
  • 24

1 Answers1

1

Instating prefabs really has nothing to do with AssetBundle. It's also the-same when using the Resources.Load API. You need to understand what prefab is before you can answer this question.

Prefabs are just GameObjects and Components put together and made to be re-usable. It is used so that if you want the-same type of Object, you won't have to manually create them over and over again every-time. You just instantiate the already created and saved prefab. This is really important when you want to share resources between multiple scenes.

When you load a prefab, it is only stored in the memory waiting to be used or instantiated. It's not visible in the scene or Hierarchy tab. There is no reason the loading function should also instantiate the prefab because you may not need it right when it is loaded. Usually prefabs are loaded when game is loading and then appropriate prefabs are instantiated during run-time when needed.

Finally, instantiating a prefab, crates a copy of it and put's in the scene. Any operation should be done on the instantiated object the loaded prefab.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Do you mean whatever types the LoadAsset function loaded, for example texture ,shader, material or scene, we do need to call Instantiate function ? or just need for prefab type ? – Liu Hao Oct 21 '18 at 08:30
  • No, not whatever types. You asked about prefabs and everything in this answer is talking about prefabs. Prefabs has the *".prefab"* extension. If not this extension then it's not a prefab and you can use it directly. For example, a texture image `LoadAsset.("MyObject")`.... – Programmer Oct 21 '18 at 08:46
  • I ask "why do we need to Instantiate the loaded asset after LoadAsset in Unity AssetBundle?" It's just that the file that was loaded from it happened to be prefab in the example code – Liu Hao Oct 21 '18 at 15:52
  • But in the body of the question, you said *"I'm confuse why we do not use the loaded **prefab** GameObject directly?"*. I went with what's in the body of the question since the body of the question is usually more descriptive than the title + the code looks like it is also loading a prefab. Anyways, my second comment should address what's in the title too. – Programmer Oct 21 '18 at 15:57
  • OK, my bad, because the variable has an ambiguous name "prefab" , which lead you to think it's a Prefab – Liu Hao Oct 21 '18 at 16:10
  • 1
    @LiuHao it's not the variable name `prefab` that makes us all "think" it's a prefab but it's rather the fact that you can `LoadAsset.` only either on a prefab or on `ScriptableObject` while `Instantiate` can only be used on prefabs or already existing GameObjects in the scene => it seems to be a prefab ;) In short: `LoadAsset` is somehow what the Editor does automatically when drag&drop something new into the `Project` view. `Instantiate` is used as Programmer already explained to create a new copy of a prefab (or GameObject in the Scene) **into the Scene**. – derHugo Oct 22 '18 at 06:43
  • @derHugo *In short: LoadAsset is somehow what the Editor does automatically when drag&drop something new into the Project view*. But when I drag&drop a prefab into the Project view, it will be show in the scene, maybe the editor called the `Instantiate` behind the scenes ? – Liu Hao Oct 23 '18 at 09:47
  • That behaviour is new to me, have never seen this before. Depends if the prefab maybe has a script on it that gets executed and instantiates the prefab into the scene? Or maybe: Where do you drag it from? – derHugo Oct 23 '18 at 11:46
  • @derHugo OP probably meant dragging it to Hierarchy tab instead of Project view. Prefabs are never in the scene unless there is a copy of it in the Hierarchy tab – Programmer Oct 23 '18 at 11:49
  • @derHugo as Programmer said, yes, I meant Hierarchy :) Anyway,I understand a little bit now. So the conclusion is when I `LoadAsset` a `GameObject` type asset, I need to `Instantiate` it so I can see it in the scene, when I `LoadAsset` another type asset, like `Shader` or `Font` , I can directly use it in my code, like `myTxt.font = fontLoaded` ? – Liu Hao Oct 24 '18 at 04:04