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.

Custom Editor - FindProperty of parent class?

Discussion in 'Scripting' started by jwvanderbeck, Jan 1, 2021.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    793
    I am attempting to make a custom editor for a LayoutGroup derived class, but running into a problem in that properties of the parent LayoutGroup class don't seem to be able to be found using FindProperty?

    For example the padding property. In the code below, the padding property will not be found. Is there some special way of referencing a parent property? I have also tried using LayoutGroup.padding but that doesn't work either.

    Code (CSharp):
    1. using UnityEditor;
    2. using omg.ui;
    3.  
    4. namespace omg.Editor.ui
    5. {
    6.     [CustomEditor(typeof(FlexibleLayoutGroup))]
    7.     public class FlexibleLayoutGroupEditor : UnityEditor.Editor
    8.     {
    9.         private SerializedProperty padding;
    10.         private SerializedProperty spacing;
    11.         private SerializedProperty layoutType;
    12.         private void OnEnable()
    13.         {
    14.             padding = serializedObject.FindProperty(nameof(FlexibleLayoutGroup.padding));
    15.             spacing = serializedObject.FindProperty(nameof(FlexibleLayoutGroup.spacing));
    16.             layoutType = serializedObject.FindProperty(nameof(FlexibleLayoutGroup.layoutType));
    17.         }
    18.        
    19.         public override void OnInspectorGUI()
    20.         {
    21.             serializedObject.Update();
    22.  
    23.             // EditorGUILayout.PropertyField(padding);
    24.             EditorGUILayout.PropertyField(spacing);
    25.             EditorGUILayout.PropertyField(layoutType);
    26.            
    27.             serializedObject.ApplyModifiedProperties();
    28.         }
    29.     }
    30. }
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    793
    Ok so for future posterity (AKA Google Search), I discovered that the padding property is really just a getter/setter for the underlying field which is named m_Padding.

    I determined that by using the below snippet of code to print out all the properties inside the serializedObject.

    Code (CSharp):
    1.             var prop = serializedObject.GetIterator();
    2.             do
    3.             {
    4.                 Debug.Log(prop.name);
    5.             } while (prop.Next(true));
    6.