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

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.