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

How to sort Serialized Properties in custom editor

Discussion in 'Scripting' started by Epic_Cube_, Dec 14, 2016.

  1. Epic_Cube_

    Epic_Cube_

    Joined:
    Jul 3, 2012
    Posts:
    97
    Hi all,
    I have a script with a number of parameters.
    The class of the script derives from another script.
    When I see the parameters in the editor, the superclass properties are shown before the subclass ones.
    I need to show the subclass properties first and the subclass properties then.

    Any suggest?

    Thx a lot
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    most straightforward way is to simply write an editor script that explicitly defines the order that the parameters are rendered. you can even go so far as create a class heirarchy of the editor scripts that overrides the base.OnInspectorGUI, and render their properties before, or after the base class
     
  3. Epic_Cube_

    Epic_Cube_

    Joined:
    Jul 3, 2012
    Posts:
    97
    yes,
    I've already written an editor script that fetches all the relevant properties and draws them in the order I want.
    That's it:
    Code (CSharp):
    1.  
    2. [...]
    3.     private List<SerializedProperty> allProperties = new List<SerializedProperty>();
    4.  
    5.     void OnEnable()
    6.     {
    7.          allProperties.Clear();
    8.          allProperties.Add( serializedObject.FindProperty( "Colors" ) );
    9.          allProperties.Add( serializedObject.FindProperty( "Target" ) );
    10.          allProperties.Add( serializedObject.FindProperty( "MixMode" ) );
    11.          allProperties.Add( serializedObject.FindProperty( "MaterialProperty" ) );
    12.         ...
    13.         }
    14.     }
    15.  
    16.     public override void OnInspectorGUI()
    17.     {
    18.          serializedObject.Update();
    19.  
    20.          foreach ( SerializedProperty prop in allProperties )
    21.              EditorGUILayout.PropertyField( prop );
    22.  
    23.         serializedObject.ApplyModifiedProperties();
    24.     }
    My real question is: is it possible to achieve the same result by changin any property "order", "index" or decorating the serialized property in some way in the source script without passing through an editor script?

    Thx a lot!
     
  4. Mariusz-Born7

    Mariusz-Born7

    Joined:
    Sep 4, 2015
    Posts:
    40
    Since there is no any kind of "order" attribute, you can use Tooltips to sort the list of properties. Just write [Tooltip("0. Some text")] or [Tooltip("Some text, Order(0)")] or use other scheme, parse the numbers inside OnEnable() method and sort properties. It is a hack, but it works. This way you can easily sort all variables from base and derived classes.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,525
    Why would you use / abuse the Tooltip attribute for that? Why don't you create your own "Order" attribute? Keep in mind that other properties like Tooltip, Header, Space, ... belong to the property that follows. So reordering them may render those attributes / decorators pointless, faulty or at least may not do what you expect or wanted.