Search Unity

Positioning a CustomEditor

Discussion in 'Scripting' started by garrilla, Mar 30, 2019.

  1. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    If I add a custom editor to a class, it shows after any public fields of that class.

    is there a way of having it show before them?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Just change when you draw the default inspector. Post your code if you want help with it.
     
  3. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    thanks

    the dropdown appears at the end of the list - I would like it before the list of integer fields



    The code I'm currently using...

    Code (CSharp):
    1. public class MyClass: MonoBehaviour{
    2.  
    3. [HideInInpector]
    4. public string choice; // this is the target of my dropdown
    5. public int a, b, c, etc;
    6.  
    7. // blah
    8.  
    9. }
    10.  
    11. [CustomEditor(typeof(MyClass))]
    12. public class SomeEditor : Editor
    13. {
    14.  
    15.     List<string> _choices = new List<string>();
    16.     int _choiceIndex;
    17.  
    18.     // this all works fine, my issue is only about positioning
    19.     public override void OnInspectorGUI()
    20.     {
    21.  
    22.         PopulateList(); // calls some function that populates my list to form the dropdown
    23.  
    24.         // Draw the default inspector
    25.         DrawDefaultInspector();
    26.         var myClass = target as MyClass;
    27.  
    28.         _choiceIndex = _choices.IndexOf(myClass .choice);
    29.  
    30.         if (_choiceIndex < 0)
    31.             _choiceIndex = 0;
    32.  
    33.         _choiceIndex = EditorGUILayout.Popup(_choiceIndex, _choices.ToArray());
    34.  
    35.         myClass .choice = _choices[_choiceIndex];
    36.  
    37.         // create an undo possibilty
    38.         EditorUtility.SetDirty(target);
    39.     }
    40. }

    BTW, I'm using this approach as I need to be able to populate the dropdown menu dynamically, so I can't use an enum.
     
    Last edited: Mar 31, 2019