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

EditorWindow GameObject Array

Discussion in 'Editor & General Support' started by JoshSC, Dec 18, 2017.

  1. JoshSC

    JoshSC

    Joined:
    Jun 29, 2014
    Posts:
    2
    I've been trying to find a source on how to put a gameobject array (or list) in a custom editor window that will accept multiple items dragged into it at once. Is there a simple way to do this with an ObjectField? I tried using a foreach loop, but it gives me an error.
     
  2. JoshSC

    JoshSC

    Joined:
    Jun 29, 2014
    Posts:
    2
    So I finally was able to piece everything together, and for anyone else who might have the same problem:

    Code (CSharp):
    1. public class MyClass : EditorWindow{
    2.  
    3.     public GameObject[] someThings;
    4.  
    5.     [MenuItem("Window/My Class")]
    6.     public static void ShowWindow(){
    7.  
    8.         EditorWindow.GetWindow <MyClass>("My Class");
    9.     }
    10.  
    11.     void OnGUI(){
    12.      
    13.         ScriptableObject scriptableObj = this;
    14.         SerializedObject serialObj = new SerializedObject (scriptableObj);
    15.         SerializedProperty serialProp = serialObj.FindProperty ("someThings");
    16.  
    17.         EditorGUILayout.PropertyField (serialProp, true);
    18.         serialObj.ApplyModifiedProperties ();
    19.     }
    20. }
    I had seen very similar code segments around, but I didn't think they fit my use case.

    What took me a while to realize was that EditorWindow derives from ScriptableObject, so I can keep everything I need in this class by referencing itself. A SerializedObject can be initialized from that, and a global variable in the class can be accessed as a SerializedProperty.
     
  3. Bluefire1234

    Bluefire1234

    Joined:
    Feb 26, 2021
    Posts:
    11
    Thanks!
    exactly what I needed
     
  4. ce_jldf

    ce_jldf

    Joined:
    Jan 5, 2023
    Posts:
    3
    this is better than create a reorederablelist i've find before;)