Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Overriding Canvas-Inspector does not show Nested Inspector when calling base

Discussion in 'Immediate Mode GUI (IMGUI)' started by SF_FrankvHoof, Feb 24, 2023.

  1. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    I'm trying to override the Inspector for the Canvas-Component, so that I can add some buttons to quickly set the Sorting-Order based on 'layers' I've created within my Game.
    (Canvas-based UI doesn't use Sorting Layers, so I've resorted to setting up my own 'layers' based on sorting order).

    However.. Whenever I override the Inspector for the Canvas, and call either
    base.OnInspectorGUI()
    or
    DrawDefaultInspector()
    I am NOT getting the proper inspector for nested Canvas-Components.
    Instead, I get an Inspector showing the Canvas as being in WorldSpace, not showing the 'sortingOrder' at all.


    Regular Inspector for nested Canvas:
    upload_2023-2-24_14-4-57.png

    When Overridden:
    upload_2023-2-24_14-5-34.png

    Is there any way for me to have the engine render the correct Inspector for nested Canvases?


    Full script (have to cast target for setting order because they're properties with InternalCall-attributes) :

    Code (CSharp):
    1. [CustomEditor(typeof(Canvas))]
    2.     public class CanvasEditor : UnityEditor.Editor
    3.     {
    4.         public const int BACKGROUND_LAYER = -7_500;
    5.         public const int MIDGROUND_LAYER = 1_000;
    6.         public const int FOREGROUND_LAYER = 7_500;
    7.  
    8.         public override void OnInspectorGUI()
    9.         {
    10.             serializedObject.Update();
    11.             Canvas cv = (Canvas)target;
    12.             if (!cv.isRootCanvas) // Sorting Order not shown on Root Canvas
    13.             {
    14.                 if (GUILayout.Button(new GUIContent("Move to Background")))
    15.                     SetOrder(BACKGROUND_LAYER);
    16.                 if (GUILayout.Button(new GUIContent("Move to MidGround")))
    17.                     SetOrder(MIDGROUND_LAYER);
    18.                 if (GUILayout.Button("Move to ForeGround"))
    19.                     SetOrder(FOREGROUND_LAYER);
    20.                 GUILayout.Space(10f);
    21.                 if (GUILayout.Button(new GUIContent("Move Forward 5")))
    22.                     SetOrder(cv.sortingOrder + 5);
    23.                 if (GUILayout.Button(new GUIContent("Move Backward 5")))
    24.                     SetOrder(cv.sortingOrder - 5);
    25.                 GUILayout.Space(15f);
    26.             }
    27.             DrawDefaultInspector();
    28.         }
    29.  
    30.         private void SetOrder(int order)
    31.         {
    32.             serializedObject.Update();
    33.             Canvas cv = (Canvas)target;
    34.             cv.overrideSorting = true;
    35.             cv.sortingOrder = order;
    36.             EditorUtility.SetDirty(cv);
    37.         }
    38.     }
     
    Last edited: Feb 24, 2023