Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Recttransform Custom inspector

Discussion in 'UGUI & TextMesh Pro' started by sp-sergio-gil, Feb 16, 2018.

  1. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    Hi! I have created a custom inspector for RectTransform but doing this, I lose the anchor points gizmos in the scene.

    If I only use this as base code
    Code (CSharp):
    1.     [CustomEditor(typeof(RectTransform)), CanEditMultipleObjects]
    2.     public class RectTransformInspector : UnityEditor.Editor
    3.     {
    and nothing more, the gizmos disappear

    If I comment the class, the anchor points are visible again.... any clue????

    Thanks

    Sergi
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Probably because you are deriving (e.g: inheriting) from UnityEditor.Editor.
    The RectTransform class probably has its own custom inspector that does a bunch of fancy stuff. when you define your own inspector, you essentially lose that functionality.

    You could try the technique i showed in my blog for "extending unity's built-in inspectors": www.li0rtal.com/extending-unity-inspectors/
     
  3. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    I cannot access the web page that you are publishing here... do you have other access methods to that page? if not can you post the article here?

    Thanks!
     
  4. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    I was using the code that you have posted in your blog (good job) and for RecTransform i t continues not working.... I cannot access the anchors from scene editor... any clue?
     
  5. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    no one? Unity help please!
     
  6. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    I'm really stuck here... at this point I will revert my custom inspectors if this is not working anymore... Unity guyus please... any idea about fixing this?
     
  7. Deleted User

    Deleted User

    Guest

    You probably don't want to extend the RectTransform inspector. Unity will not be happy and will throw bunch of harmless exceptions that clutter your console window. I chose to find another way. However, I'm gonna tell you how you can do this:

    Code (CSharp):
    1. [CustomEditor(typeof(RectTransform), true)]
    2. public class RectTransformEditor : Editor
    3. {
    4.     private Editor editorInstance;
    5.     private Type nativeEditor;
    6.     private MethodInfo onSceneGui;
    7.     private MethodInfo onValidate;
    8.  
    9.     public override void OnInspectorGUI()
    10.     {
    11.         editorInstance.OnInspectorGUI();
    12.         // Code here
    13.     }
    14.  
    15.     private void OnEnable()
    16.     {
    17.         if (nativeEditor == null)
    18.             Initialize();
    19.  
    20.         nativeEditor.GetMethod("OnEnable",BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(editorInstance, null);
    21.         onSceneGui = nativeEditor.GetMethod("OnSceneGUI", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    22.         onValidate = nativeEditor.GetMethod("OnValidate",BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    23.     }
    24.  
    25.     private void OnSceneGUI()
    26.     {
    27.         onSceneGui.Invoke(editorInstance, null);
    28.     }
    29.  
    30.     private void OnDisable()
    31.     {
    32.         nativeEditor.GetMethod("OnDisable", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(editorInstance, null);
    33.     }
    34.  
    35.     private void Awake()
    36.     {
    37.         Initialize();
    38.         nativeEditor.GetMethod("Awake", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.Invoke(editorInstance, null);
    39.     }
    40.  
    41.     private void Initialize()
    42.     {
    43.         nativeEditor = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.RectTransformEditor");
    44.         editorInstance = CreateEditor(target, nativeEditor);
    45.     }
    46.  
    47.     private void OnDestroy()
    48.     {
    49.         nativeEditor.GetMethod("OnDestroy", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.Invoke(editorInstance, null);
    50.     }
    51.  
    52.     private void OnValidate()
    53.     {
    54.         if (nativeEditor == null)
    55.             Initialize();
    56.  
    57.         onValidate?.Invoke(editorInstance, null);
    58.     }
    59.  
    60.     private void Reset()
    61.     {
    62.         if (nativeEditor == null)
    63.             Initialize();
    64.  
    65.         nativeEditor.GetMethod("Reset", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.Invoke(editorInstance, null);
    66.     }
    67. }
    68.  
     
    AndersonMarquess likes this.
  8. sp-sergio-gil

    sp-sergio-gil

    Joined:
    Mar 5, 2014
    Posts:
    45
    Hi! it seems that this is working right now... the only thing is that I'm using Unity Stable Scripting Runtime Version and with this I cannot use:

    Code (CSharp):
    1. nativeEditor.GetMethod("Reset", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.Invoke(editorInstance, null);
    instead I need to use something like this

    Code (CSharp):
    1. var method = nativeEditor.GetMethod("Reset", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    2. if(method != null)
    3. {
    4.     method.Invoke(editorInstance, null);
    5. }