Search Unity

RectTransform custom editor ontop of Unity RectTransform Custom Editor??

Discussion in 'Immediate Mode GUI (IMGUI)' started by Azmar, Feb 11, 2017.

  1. Azmar

    Azmar

    Joined:
    Feb 23, 2015
    Posts:
    246
    Using the code below I lose all the functionality of the current Unity Custom Editor on their RectTransform. How do I keep their exact RectTransform while also adding my own stuff underneath it? I tried Editor.CreateEditor stuff and I kept getting infinite loop crashing when I tried to create Editor based on the RectTransform.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. [CustomEditor(typeof(RectTransform))]
    7. public class RectTransformEditor : Editor {  
    8.    
    9.     public override void OnInspectorGUI () {
    10.         DrawDefaultInspector ();
    11.     }
    12. }
     
  2. MrCool92

    MrCool92

    Joined:
    Jul 13, 2015
    Posts:
    15
    You need to inherit RectTransformEditor and call base OnInspectorGUI function. Since RectTransformEditor is internal class you need System.Reflection to get editor type, then instantiate it.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Reflection;
    4. using System;
    5.  
    6. [CustomEditor(typeof(RectTransform), true)]
    7. public class RectTransformEditor : Editor
    8. {
    9.     private Editor _editorInstance;
    10.  
    11.     private void OnEnable()
    12.     {
    13.         Assembly ass = Assembly.GetAssembly(typeof(UnityEditor.Editor));
    14.         Type rtEditor = ass.GetType("UnityEditor.RectTransformEditor");
    15.         _editorInstance = CreateEditor(target, rtEditor);
    16.     }
    17.  
    18.     public override void OnInspectorGUI()
    19.     {
    20.         _editorInstance.OnInspectorGUI();
    21.         // ...
    22.     }
    23. }
     
    Elmundo, mnarimani, seec1 and 2 others like this.
  3. Elmundo

    Elmundo

    Joined:
    Mar 30, 2015
    Posts:
    1
    If you also want to see 'Anchor Indicators' in editor scene, you need to override 'OnSceneGUI' method explicitly as i've shown below.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Reflection;
    4. using System;
    5.  
    6. [CustomEditor(typeof(RectTransform), true)]
    7. public class RectTransformEditor : Editor {
    8.     private Editor _editorInstance;
    9.  
    10.     private void OnEnable() {
    11.         Assembly ass = Assembly.GetAssembly(typeof(UnityEditor.Editor));
    12.         Type rtEditor = ass.GetType("UnityEditor.RectTransformEditor");
    13.         _editorInstance = CreateEditor(target, rtEditor);
    14.     }
    15.  
    16.     public override void OnInspectorGUI() {
    17.         _editorInstance.OnInspectorGUI();
    18.     }
    19.  
    20.     private void OnSceneGUI() {
    21.         MethodInfo onSceneGUI_Method = _editorInstance.GetType().GetMethod("OnSceneGUI", BindingFlags.NonPublic | BindingFlags.Instance);
    22.         onSceneGUI_Method.Invoke(_editorInstance, null);
    23.     }
    24. }