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

Assigning prefab references in code

Discussion in 'Scripting' started by Saticmotion, Nov 29, 2018.

  1. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    I'm trying to create a sort of asset database, with prefabs of a particular type (specifically objects I can place in edit mode of my game). The database side works fine, but it's a bit cumbersome to keep manually assigning newly made prefabs to my database. The definition of my database is as follows:

    Code (CSharp):
    1. public class ObjectDatabase : MonoBehaviour
    2. {
    3.     public List<GameObject> objectPrefabs;
    4.  
    5.     public Dictionary<ObjectClasses, Dictionary<ObjectTypes, List<GameObject>>> objects;
    6.  
    7.     //irrelevant functions
    8. }
    9.  
    The objectPrefabs List is where I assign my prefabs. On Start() they get sorted and put into objects, which will be available to anyone in the scene that requires it.

    What I'm trying to do is write an editor script that will take all prefabs in a certain path and assign them to the objectPrefabs Array.

    This is what I've got so far:

    Code (CSharp):
    1. public void Refresh()
    2. {
    3.     var assets = AssetDatabase.LoadAllAssetsAtPath("Libraries/FS FloorPlanner/Prefabs/ObjectDatabase");
    4.  
    5.     foreach (GameObject asset in assets)
    6.     {
    7.         if (asset.GetComponent<ObjectDatabase>() == null)
    8.         {
    9.             //Here I would assign the found prefab to the ObjectDatabase.objectPrefabs array
    10.         }
    11.     }
    12.  
    13.     Debug.Log("asset db updated");
    14. }
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    I wrote an editor for prefabs once. There I found the prefabs via the extension of the file name like this:
    string[] prefabPaths = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);

    the paths I found had to be changed a bit to make it working with the AssetDatabase, but it works well.
     
  3. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    Thanks for the reply! The problem however is not the loading of prefabs, but assigning them to the database. The manual workflow is dragging a prefab onto a script parameter, like in this screenshot:
    Untitled.png
     
  4. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    I ran into a very helpful post here: https://answers.unity.com/questions/598277/generating-a-prefab-from-audio-clip-via-script.html

    And from that I made this script:

    Code (CSharp):
    1.     public class ObjectDatabaseUpdater : MonoBehaviour
    2.     {
    3.         [MenuItem("Custom/Generate object db")]
    4.         public static void Refresh()
    5.         {
    6.             var db = GameObject.Find("ObjectDatabase").GetComponent<ObjectDatabase>();
    7.             var assetPaths = Directory.GetFiles(Path.Combine(Application.dataPath, "Libraries/FS Floorplanner/Prefabs/ObjectDatabase"), "*.prefab", SearchOption.TopDirectoryOnly);
    8.  
    9.             AssetDatabase.StartAssetEditing();
    10.             db.objectPrefabs.Clear();
    11.  
    12.             foreach (string path in assetPaths)
    13.             {
    14.                 var asset = AssetDatabase.LoadAssetAtPath<GameObject>(path.Substring(path.IndexOf("Asset", StringComparison.Ordinal)));
    15.  
    16.                 if (asset.GetComponent<ObjectDatabase>() == null)
    17.                 {
    18.                     db.objectPrefabs.Add(asset);
    19.                 }
    20.             }
    21.  
    22.             AssetDatabase.StopAssetEditing();
    23.             Debug.Log("asset db updated");
    24.         }
    25.     }
    which does exactly what I want
     
    Halfheartdk likes this.