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

Populate a dropdown, consisted of list of objects

Discussion in 'Scripting' started by v_chs, Sep 7, 2020.

  1. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    I want to populate a dropdown which would consist of a list of objects. I use the dropdown editor way for populating dropdowns in the inspector. Usually, I get a list from the main script and using it in Editor script. But now, the problem is that the list is a list of objects, from which I want to show only a value from every object in the dropdown and when is selected, to use all of its properties.



    Code (CSharp):
    1.  [CustomEditor(typeof(MaterialsDropdown))]
    2.     public class MaterialsDropdownEditor : Editor {
    3.         public override void OnInspectorGUI() {
    4.             base.OnInspectorGUI();
    5.  
    6.             MaterialsDropdown materialsDropdownScript = (MaterialsDropdown) target;
    7.             // TODO: Fix the dropdown: Get options from API
    8.             GUIContent label = new GUIContent("MyList");
    9.             materialsDropdownScript.listIdx =
    10.                 EditorGUILayout.Popup(label, materialsDropdownScript.listIdx, materialsDropdownScript.materialsList);
    11.         }
    12.     }
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I mean something like this should work:
    Code (CSharp):
    1. string[] options = new string[materialsDropdownScript.materialsList.length];
    2. int index = 0;
    3. foreach (Material material in materialsDropdownScript.materialsList)
    4. {
    5.    options[index++] = material.name;
    6. }
    7. EditorGUILayout.Popup(label, materialsDropdownScript.listIdx, options);
    But i fear you have to assign the choosen object somewhere later by the selected index.
     
    v_chs likes this.
  3. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    Thanks :)