Search Unity

How to call base class from reference of derived class

Discussion in 'Scripting' started by Dabartos, Mar 24, 2018.

  1. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    How to call base class func from reference of derived class

    Hi, I have a problem with this script, take a look at line 35. it shows what I want to do.
    Is using delegates any kind of help here? Any other way you know of that would work? Is it even possible?

    Cheers.

    Code (CSharp):
    1.  
    2.     [CustomEditor(typeof(MonoClassA))]
    3.     public class InspectorClass : Editor {
    4.  
    5.         MonoClassA classA;
    6.         bool baseOn;
    7.  
    8.         private void OnEnable() {
    9.             classA = target as MonoClassA;
    10.         }
    11.  
    12.         public override void OnInspectorGUI() {
    13.             InspectorExtentions.InspectorHeader(classA, this, ref baseOn);
    14.         }
    15.     }
    16.  
    17.     public class MonoClassA :MonoBehaviour {
    18.  
    19.     }
    20.  
    21.     public static class InspectorExtentions {
    22.  
    23.         public static void InspectorHeader<T, M>(T target, M editor, ref bool baseOn) where M : Editor where T : MonoBehaviour {
    24.             using (new EditorGUILayout.HorizontalScope()) {
    25.  
    26.                 GUI.enabled = false;
    27.                 EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour(target), typeof(T), true);
    28.                 GUI.enabled = true;
    29.  
    30.                 if (GUILayout.Button(baseOn ? "O" : "-", GUILayout.Width(20)))
    31.                     baseOn = !baseOn;
    32.  
    33.                 // THIS IS THE PROBLEMATIC PART
    34.                 if (baseOn)
    35.                     editor.base.OnInspectorGUI();
    36.             }
    37.         }
    38.     }
    39.  
    40.  
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Why are you even using an extension method here? The calling code itself already has access to the base implementation just have the editor handle it. Its literally one of the things of a derived class does; extending/overriding base implementation.
     
  3. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    I appreciate you taking the time to read it and comment but please aim your attention at the problem at hand.
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    if you want the utility method call the base implementation its better that it doesn't even know that its the base method its calling, leave that to the editor to assign. you can actually simplify the data you are passing into your utility method, as it stands it has access to all sorts of data it doesn't even need. passing only the data it actually needs makes it more usable in many more places. furthermore, (and this is optional) turning the method into an extension method will make the method call on the calling code cleaner.

    Code (CSharp):
    1.     public static void InspectorHeader(this SerializedObject serializedObject, System.Action showGUI, ref bool show)
    2.     {
    3.         if (serializedObject != null)
    4.         {
    5.             EditorGUILayout.BeginHorizontal();
    6.  
    7.             SerializedProperty p_script = serializedObject.FindProperty("m_Script");
    8.             if (p_script != null)
    9.             {
    10.                 EditorGUI.BeginDisabledGroup(true);
    11.                 EditorGUILayout.PropertyField(p_script, new GUIContent("Script"), false);
    12.                 EditorGUI.EndDisabledGroup();
    13.             }
    14.  
    15.             if (GUILayout.Button(show? "O" : "-", GUILayout.Width(20)))
    16.             {
    17.                 show= !show;
    18.             }
    19.  
    20.             if (show&& showGUI!= null)
    21.                 showGUI();
    22.  
    23.             EditorGUILayout.EndHorizontal();
    24.         }
    25.     }
    using SerializedObject instead of Editor and Monobehaviour arguments makes the code more flexible (i think it should also work for ScriptableObjects now and you no longer need a separate editor class for each target class). And now the editor code is also simplified:
    Code (CSharp):
    1. [CustomEditor(typeof(MonoClassA))]
    2. public class InspectorClass : Editor
    3. {
    4.     bool baseOn;
    5.  
    6.     public override void OnInspectorGUI()
    7.     {
    8.         serializedObject.InspectorHeader(base.OnInspectorGUI, ref baseOn);
    9.     }
    10. }
     
  5. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    oh damn that's exactly it. It didn't occur to me I can pass function as Action reference. Thank you mate!