Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug InspectorElement shows only Custom inspector and not the default ones

Discussion in 'UI Toolkit' started by Magnilum, Sep 10, 2023.

  1. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    Hi,

    I am doing a mini inspector under a object field to quickly show the values of a scriptable object.

    upload_2023-9-10_15-16-15.png
    The problem is, if I don't create a custom inspector here is what I have:

    upload_2023-9-10_15-17-1.png

    In this video, it is said that if that if the inspector element does not find a custom editor, it will show the default one but it is not the case.

    Time code: 7min 35


     
    Last edited: Sep 12, 2023
  2. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Hello,

    Which version of the editor are you using?
     
  3. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    I am using Unity 2022.3.6f1
     
  4. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Please also let me know the time code, for some reason the video starts at 00:00 for me! Thank you.
     
  5. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    The time code is 7min35
     
  6. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Could you send a snippet of your InspectorElement code?
     
  7. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    Code (CSharp):
    1. public class ObjectInspector : ObjectCSCC {
    2.  
    3.         public new class UxmlFactory : UxmlFactory<ObjectInspector, UxmlTraits> { }
    4.  
    5.         Toggle toggleDisplay;
    6.         VisualElement inspector;
    7.  
    8.         public ObjectInspector()
    9.         {
    10.             InitFields();
    11.             InitCallbacks();
    12.         }
    13.  
    14.         #region Init
    15.  
    16.         void InitFields()
    17.         {
    18.             toggleDisplay = new Toggle();
    19.             toggleDisplay.style.marginLeft = 5;
    20.             toggleDisplay.tooltip = "Show values";
    21.             ElementAt(0).Add(toggleDisplay);
    22.  
    23.             inspector = new VisualElement();
    24.             inspector.name = "inspector";
    25.             Add(inspector);
    26.  
    27.             toggleDisplay.Display(false);
    28.             UpdateInspector(value);
    29.         }
    30.  
    31.         void InitCallbacks()
    32.         {
    33.             this.RegisterValueChangedCallback(evt =>
    34.             {
    35.                 toggleDisplay.value = evt.newValue != null;
    36.                 toggleDisplay.Display(evt.newValue != null);
    37.  
    38.                 UpdateInspector(evt.newValue);
    39.             });
    40.  
    41.             toggleDisplay.RegisterValueChangedCallback(evt =>
    42.             {
    43.                 inspector.Display(evt.newValue);
    44.             });
    45.         }
    46.  
    47.         #endregion
    48.  
    49.         void UpdateInspector(Object obj)
    50.         {
    51.             inspector.Clear();
    52.  
    53.             inspector.Display(toggleDisplay.value && obj != null);
    54.  
    55.             if (obj == null) return;
    56.  
    57.             inspector.Add(new InspectorElement(obj));
    58.         }
    59.     }
    ObjectCSCC is a derived class of ObjectField.