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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Show Prefabs in custom Editor Window

Discussion in 'Scripting' started by grimmreefer, Mar 18, 2015.

  1. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Hey, im working on some "Tools" that help me whit Level creation. I made a custom Editor Window and want to show Prefabs from a specific folder in it, so i can drag and drop it from there to the world.

    Until now i cant find a solution for that.

    Can anyone help?
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You can use the File.IO classes to list asset files on the specified path - and then AssetDatabase.LoadMainAssetAtPath to get a reference to the asset data. Use UnityEditor.EditorGUI.ObjectField or UnityEditor.DragAndDrop to create GUI with draggable references to those prefabs.
     
  3. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Awesome, thanks. Cant wait till i was at home :)
     
  4. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    i get so far that i get the filenames of my prefabs, now i try to put them to an array but i always get a
    NullReferenceException: Object reference not set to an instance of an object error.

    Code (CSharp):
    1. private static GameObject[] prefabs = null;
    2.  
    3.   private static void getPrefabs()
    4.     {
    5.      
    6.          string[] search_results = System.IO.Directory.GetFiles("Assets/Prefabs/", "*.prefab",       System.IO.SearchOption.AllDirectories);
    7.  
    8.              for (int i = 0; i < search_results.Length; i++ )
    9.              {
    10.                  Object prefab = null;
    11.                  prefab = AssetDatabase.LoadAssetAtPath(search_results[i], typeof(GameObject));
    12.                  prefabs[i] = prefab as GameObject;
    13.  
    14.                  Debug.Log(prefabs[i].name + " " + prefabs.Length);
    15.              }
    16.    }
    I dont know what im doing wrong here.


    Here is the whole Script if anyone is interested:
    http://pastebin.com/QF8khq7A
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    First, verify which reference is null. Second, use "AssetDatabase.LoadMainAssetAtPath (path) as GameObject" in stead.
     
  6. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    oh, i didnt realize that i have to set a size to the array. Now its clear to me why i got a null reference error.
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Ah yes, the prefabs array has to be initialised. This may come as a surprise, as Unity will do this for you in the case of serialised (public) arrays on MonoBehaviours.
     
  8. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Well, now i know^^