Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Other than using Resources.Load() to load prefabs, what other ways can be used?

Discussion in 'Editor & General Support' started by asperatology, May 24, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I only learned how to load prefabs using the following code:

    Code (CSharp):
    1. Instantiate(Resources.Load("Prefabs/Cube"));
    Are there other ways of loading prefabs than using Resources.Load()?

    I am hoping for an object loader that, given a known folder location, one can load the asset into the game. And the object loader isn't always Resources.

    Thanks.
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Reference it through a public variable. This is the usual way. Resourses is only for specific uses, as you are pointing to a specific path. Referencing an asset directly creates a.connection to its guid, so you can move it or rename it, and you don't have to change its code. Also, since it's tracked by reference, if an asset is not used, it won't get put in the build.
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Ah, thanks for letting me know. In UE4, we are so accustomed to using FObjectLoader<T> and class blueprints that I thought Unity only has Resources.Load() available, feeling limited.
     
  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I think putting it in a public variable will get it in memory, even if you don't use it.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    public GameObject[] resource;

    Zombie is suggesting you drag a bunch of gameobjects into the resource array and you'll be able to instantiate resource[4] or whatever. Or by name (use dictionary for speedy lookup). Anything, up to you.

    We do this as well, no need for Resources.Load
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    No. It is a reference that points the actual asset. It wll be placed in memory when used. If it has no references, it won't even be in the build.

    By contrast, resourse load always is added to the build, and easily point to the wrong one if you are not careful. Additionally it will stay in memory if don't unload it.
     
  7. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I tried that in an iOS-project back in Unity 4. I had
    public GameObject prefab;
    and it should be instantiated when the camera gets to the right catalogue-page. The app took 300mb ram. After I changed it to resources.load(), it took only 24MB plus the loaded model.
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Must have had something else going on, or you were accidentally loading it. It doesn't work like that. If you can reproduce it, you should submit a bug.