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:
    26
    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. }
     
    petey, Elmundo, seec1 and 3 others like this.
  3. Elmundo

    Elmundo

    Joined:
    Mar 30, 2015
    Posts:
    2
    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. }
     
    petey and RedHillbilly like this.
  4. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey I've been using this to add a few extra buttons to the rectransform component and it's so handy, but I keep getting these errors -

    SerializedObjectNotCreatableException: Object at index 0 is null


    Does anyone know what might cause that?
     
  5. Mr-Frost

    Mr-Frost

    Joined:
    Oct 10, 2014
    Posts:
    6
    Hey. You need to destroy created editor. It's mentioned in documentation.
    In my case I had to call it from OnDisable and OnDestroy from my custom editor script.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Ace! Thanks for that :)