Search Unity

CustomPropertyDrawer for System.Guid doesn't show up

Discussion in 'Scripting' started by Vapid-Linus, Jan 12, 2020.

  1. Vapid-Linus

    Vapid-Linus

    Joined:
    Aug 6, 2013
    Posts:
    64
    I am trying to create a property drawer for System.Guid, which does have the [Serializable] attribute.

    However, public or [SerializeField] fields still do not show up for fields of type System.Guid.

    This is my code for the property drawer:

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. [CustomPropertyDrawer(typeof(Guid))]
    7. public class GuidDrawer : PropertyDrawer
    8. {
    9.     private VisualElement root;
    10.     private VisualTreeAsset template;
    11.  
    12.     public override VisualElement CreatePropertyGUI(SerializedProperty property)
    13.     {
    14.         root = new VisualElement();
    15.         template = ElementAssets.Template.LoadAsset();
    16.         template.CloneTree(root);
    17.         Debug.Log("CustomPropertyDrawer Enabled!");
    18.         return root;
    19.     }
    20. }
    21.  
    Yet, when I select an object who has a component who has a [SerializeField] Guid field, it just doesn't show up. No errors or other messages in the console either.

    The docs only state that the type for custom property drawers must have the [Serializable] attribute, which System.Guid does. Is there something else I'm missing?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    Custom property drawers only customize the UI, they don't affect what the serialization system supports.
    System.Guid
    is not supported by the Unity serializer and adding a custom property drawer won't change this.

    The usual rules for what is supported by the serializer do not apply to the .Net API. Only the types explicitly mentioned are supported (primitives, enums, arrays, lists).

    You have to convert the
    System.Guid
    to something Unity can serialize and then you can also create a custom property drawer for it.

    I found an instance where Unity has done this themselves, converting it to a byte array:
    https://github.com/Unity-Technologi.../CrossSceneReference/Runtime/GuidComponent.cs

    Instead of a component, you could also use a serializable struct that more directly converts to
    System.Guid
    , by implementing implicit conversion operators.
     
    Vapid-Linus likes this.