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

Question How to make a serialized field read only

Discussion in 'UI Toolkit' started by CyberAngel, Dec 18, 2022.

  1. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    In the old system I can do this.

    Code (CSharp):
    1.     public class DisplayIDAttribute : PropertyAttribute { }
    2.  
    3. #if UNITY_EDITOR
    4.     [CustomPropertyDrawer(typeof(DisplayIDAttribute))]
    5.     public class GenerateIDDrawer : PropertyDrawer
    6.     {
    7.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.         {
    9.             GUI.enabled = false;
    10.             EditorGUI.PropertyField(position, property, label, true);
    11.             GUI.enabled = true;
    12.         }
    13.     }
    And with that Attribute it would do what I need. I am now starting to switch to using the Toolkit more, and was trying to find how to that. So far all I have is this. I just can't seem to find the relevant information to continue from here.

    Code (CSharp):
    1.     [CustomEditor(typeof(UniqueID))]
    2.     public class UniqueIDEditor : Editor
    3.     {
    4.         public override VisualElement CreateInspectorGUI()
    5.         {
    6.             var root = new VisualElement();
    7.  
    8.             InspectorElement.FillDefaultInspector(root, serializedObject, this);
    9.  
    10.             return root;
    11.         }
    12.     }
     
  2. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    anyone?
     
  3. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    You can disable a VisualElement by calling SetEnabled.

    If you want to disable the whole inspector, you could do it with
    root.SetEnable(false);
    .

    If you want to disable a specific property field from
    CreateInspectorGUI
    , you should probably add each property field manually instead of doing it with
    FillDefaultInspector
    . Then you can disable any specific property fields you want. Or you can query the root to find a specific property field and disable it.

    If you want to disable a field inside a PropertyDrawer, you could call SetEnabled(false) from inside the property drawer, just like you are doing with IMGUI.
    Code (CSharp):
    1.  
    2. public class GenerateIDDrawer : PropertyDrawer
    3. {
    4.     public override VisualElement CreatePropertyGUI(SerializedProperty property)
    5.     {
    6.         var propertyRoot = new PropertyField(property);
    7.         propertyRoot.SetEnabled(false);
    8.         return propertyRoot;
    9.     }
    10. }
    11.  
     
  4. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    Original code was based on an Attribute, I was hoping to continue along those lines. I just couldn't find any decent examples to help me head in the right direction.
     
  5. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    It seems to me you are very new to custom editor stuff in general, am I right? If that's so, why do you care about using UIToolkit instead of IMGUI?

    Anyway, the code I posted can be easily added to your current property drawer:
    Code (CSharp):
    1.     [CustomPropertyDrawer(typeof(DisplayIDAttribute))]
    2.     public class GenerateIDDrawer : PropertyDrawer
    3.     {
    4.         // This method will be used with UIToolkit.
    5.         public override VisualElement CreatePropertyGUI(SerializedProperty property)
    6.         {
    7.             var propertyRoot = new PropertyField(property);
    8.             propertyRoot.SetEnabled(false);
    9.             return propertyRoot;
    10.         }
    11.  
    12.         // This method will be used with the old IMGUI system.
    13.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    14.         {
    15.             GUI.enabled = false;
    16.             EditorGUI.PropertyField(position, property, label, true);
    17.             GUI.enabled = true;
    18.         }
    19.     }
    This should fix your particular case.
     
  6. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    No I am not that new to Custom Editors, but I would not say I am a guru either.

    As for caring, its more of a learning curve.
     
  7. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    This gives me the same result that I had gotten when I tried something like this.

    upload_2022-12-21_18-33-25.png
     
  8. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Can you post how your code looks now?
     
  9. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    Code (CSharp):
    1.     public class DisplayIDAttribute : PropertyAttribute { }
    2.  
    3. #if UNITY_EDITOR
    4.  
    5.     [CustomPropertyDrawer(typeof(DisplayIDAttribute))]
    6.     public class DisplayIDDrawer : PropertyDrawer
    7.     {
    8.         public override VisualElement CreatePropertyGUI(SerializedProperty property)
    9.         {
    10.             var propertyRoot = new PropertyField(property);
    11.             propertyRoot.SetEnabled(false);
    12.             return propertyRoot;
    13.         }
    14.     }
    15.  
    16. #endif
     
  10. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Right, but also can you post the field where this attribute is used and your custom editor? Also, what version of Unity are you using?
     
  11. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    Code (CSharp):
    1. [DisplayID, SerializeField] private string _uniqueId;
    Latest Unity 2021 (LTS)
     
  12. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Okay, do you still have the UniqueIDEditor from your first post?

    EDIT
    I'm going to sleep, so I'll probably see your answer tomorrow. Just in case, in 2021.3 UIToolkit isn't used by default, so if you want a to use UI Toolkit in the Inspector, you need to kee using a custom Editor in addition to your property drawer. If you aren't using a custom Editor, that could be causing the behavior you are seeing.
     
    Last edited: Dec 21, 2022
  13. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    125
    No, that is what I am trying to replace.
     
  14. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Well there it is. You are not using UIToolkit; the UIToolkit Property Drawer isn't being used because in Unity 2021.3 you need a custom Editor to use UI Toolkit.

    Add the custom Editor in addition to your property drawer and it should work. I would also really recommend reading the UIToolkit section in the manual.
     
    Last edited: Dec 21, 2022
    CyberAngel likes this.