Search Unity

Would it be possible and is it a good idea? [Loading prefabs by name]

Discussion in 'Scripting' started by michal_k, Nov 15, 2015.

  1. michal_k

    michal_k

    Joined:
    Jul 31, 2014
    Posts:
    33
    Hi,
    I'm asking this question because I'm not very familiar with Editor GUI Scripting but I have checked the API and it does not seem impossible.

    In Unity, I can write a class with an array of gameobjects. I can drag to it multiple prefabs to assign references. Then I can access references to prefabs in the array and instantiate them at runtime.

    I want to make this automatic and in a dictionary.

    Namely, would it be possible to make something like below?
    1. I am in editor mode (not play mode).
    2. I have a directory like MyPrefabs/Ships and MyPrefabs/Weapons with simple prefabs there.
    3. I have an input field in a custom inspector where I specify search directory "MyPrefabs" and press a custom button which finds all *.prefab files in this directory.
    4. I create a text file which is a textfile with a C# enum definition. It is a "flat list" of all prefabs prefixed with directory which looks something like this:
      enum AllPrefabs {
      WeaponsWeapon1,
      WeaponsWeapon2,
      ...
      ShipsSomeShip,
      ShipsBigShip
      }
    5. I have a class with dictionary of prefabs, mapped by enum AllPrefabs -> Gameobject reference for prefab. Then I get each prefab from point 3. and assign it by decoded String path as enum name.
    6. I have all refences set up, BEFORE runtime, so I don't need to put everything in Resources folder (or am I wrong?) and now I can do
    Code (CSharp):
    1. Gameobject prefab = PrefabManager.getByName(AllPrefabs.WeaponsWeapon1);
    2. Instantiate(prefab, position, rotation);
    Would it be possible? It would be the same as assignin it all manually but using a script unless I'm missing something.

    Thanks for answering.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The API to load stuff dynamically is Resources.Load() and Resources.LoadAll()

    Pretty sure that even if things are referenced in a scene, you MUST have it located under some directory named "Resources" or else the naming won't work and it won't be found. Not sure if this is still true under the Editor however...
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Resources is for runtime dynamic loading. At editor time you can use AssetDatabase and just assign the references directly. You get the benefit of editor references without the cost of assigning them all by hand.
     
    Kurt-Dekker likes this.
  4. michal_k

    michal_k

    Joined:
    Jul 31, 2014
    Posts:
    33
    Hi,
    that's what I planned to do - For example I've found AssetDatabase.LoadAllAssetsAtPath can retrieve asset outside Resources folder (in Editor mode only I guess). Just for assigning references (as if you drag and drop into editor) and instantiating at runtime from those references. I'll try to make that work and see if there will be any problems...

    The main reason is, since nested prefabs don't work, and free "Poor Mans Nested Prefab" doesn't work for dynamic prefabs, one needs to instantiate manually and assigning all that is a lot of work...