Search Unity

Hiding some fields in inspector with custom editor and inheritance

Discussion in 'Scripting' started by Mish, Dec 4, 2015.

  1. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    I have a custom inspector for a parent class in which I want to hide some fields based on a specific condition like this:

    Code (CSharp):
    1. if (condition)
    2. EditorGUILayout.PropertyField(serializedObject.FindProperty("name"));
    In the custom inspector I draw each field through the EditorGUILayout.PropertyField() method. However all the child classes loose their own fields unless I also draw them through the custom inspector.

    Is there a way to keep the child classes untouched and still keep the changes from the custom parent class?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    You can derive custom editor classes from other custom editor classes. Create an inspector for your child class that derives from the parent's inspector, add the fields which are unique to your children to it, and call base.OnInspectorGUI().
     
  3. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    I think this would still require me to draw every field of every child class since at no point I am calling DrawDefaultInspector.

    However, I stumbled upon this method: DrawPropertiesExcluding. This seems to be able to solve some of my issues.