Search Unity

Question How to create custom GUI for properties of bound ScriptableObjects

Discussion in 'Editor Workflows' started by wmspier16, Feb 10, 2023.

  1. wmspier16

    wmspier16

    Joined:
    Nov 11, 2013
    Posts:
    1
    I am working on an editor window for modifying serialized data objects. To create the visual elements that display the data (which inherits ScriptableObject) I just make an empty element and bind the object to it.

    Code (CSharp):
    1. public static VisualElement Create(DataBase target)
    2.         {
    3.             var container = new VisualElement { style = { flexGrow = new StyleFloat(1) } };
    4.             var serializedObject = new SerializedObject(target);
    5.  
    6. var fields = GetVisibleSerializedFields(target.GetType().BaseType);
    7. fields.AddRange(GetVisibleSerializedFields(target.GetType()));
    8. foreach (var field in fields)
    9. {
    10.     var serializedProperty = serializedObject.FindProperty(field.Name);
    11.    
    12.     var propertyField = new PropertyField(serializedProperty);
    13.     container.Add(propertyField);
    14. }
    15.  
    16.             container.Bind(serializedObject);
    17.             return container;
    18.         }
    Now in an extended class I have a field (DataReference) that I would like to make a custom drawer for but I am not sure how to set it up properly.

    Code (CSharp):
    1. public class BattleData : DataBase
    2. {
    3.         [field: SerializeField] public DataReference Test { get; private set; }
    4. }
    5.  
    6. [Serializable]
    7. public class DataReference
    8. {
    9.    [field:SerializeField] public string Guid { get; private set; }
    10.  
    11.    public DataReference(string guid)
    12.    {
    13.       Guid = guid;
    14.    }
    15. }
    16.  
    I have tried something along these lines to no avail. Can anyone point me in the right direction?

    Code (CSharp):
    1. [CustomEditor(typeof(DataReference))]
    2.     public class DataReferenceEditor : UnityEditor.Editor
    3.     {
    4.         public override VisualElement CreateInspectorGUI()
    5.         {
    6.             var parent = new VisualElement();
    7.        
    8.             parent.Add(new TextField("This is a custom inspector"));
    9.  
    10.             return parent;
    11.         }
    12.     }
     
    Last edited: Feb 10, 2023