Thank you for the syntax help with the generic and the typo (I swear I copied and pasted it at least once to make sure it was right haha)
I’m making progress, no more errors, however, I still can’t get the field rendering.
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
// Create property container element.
var container = new VisualElement();
var SOField = new PropertyField(property);
container.Add(SOField);
if (property.objectReferenceValue!=null)
{
SerializedObject soProperty = new(property.objectReferenceValue);
SerializedProperty prop = soProperty.GetIterator();
if(prop.NextVisible(true) )
{
do
{
Debug.Log(prop.name);
} while (prop.NextVisible(false));
}
var SOInitialValueField = new PropertyField(soProperty.FindProperty("initialValue"));
container.Add(SOInitialValueField);
}
return container;
}
I’ve made initialValue public just for now so it can be accessed. and I’ve confirmed by looping that the initialValue does exist in soProperty but I’m still not getting a field to edit it.
This is my original implementation UGUI (initialValue is called storedValue in this version), this does work but I have to create a new drawer for each type. I want to achieve the same functionality but have it just draw the correct field type without me needing to specify, plus I do like the UIToolkit’s simpler syntax for adding fields too.
using UnityEditor;
using UnityEngine;
using HR.Utilities.Variables;
public abstract class CustomVariableDrawer<T> : PropertyDrawer
{
protected abstract T GetValueToEdit(SerializedProperty property);
protected abstract void ApplyValue(SerializedProperty property, T newValue);
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// Adjust height as needed
return (EditorGUIUtility.singleLineHeight*2) +EditorGUIUtility.standardVerticalSpacing;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Display the ScriptableObject field
EditorGUI.PropertyField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), property, label);
// Move the position down for the value field
position.y += EditorGUIUtility.singleLineHeight+EditorGUIUtility.standardVerticalSpacing;
if(property.objectReferenceValue == null)
{
return;
}
// Get the value to edit
T valueToEdit = GetValueToEdit(property);
// Edit the value using the appropriate EditorGUI method
T newValue = EditValue(position, valueToEdit);
// Apply the new value if changed
if (GUI.changed)
{
ApplyValue(property, newValue);
property.serializedObject.ApplyModifiedProperties();
Undo.RecordObject(property.objectReferenceValue, "Change Variable Value");
EditorUtility.SetDirty(property.objectReferenceValue);
}
}
protected abstract T EditValue(Rect position, T valueToEdit);
}
[CustomPropertyDrawer(typeof(FloatVariable))]
public class FloatVariableDrawer : CustomVariableDrawer<float>
{
protected override float GetValueToEdit(SerializedProperty property)
{
return ((FloatVariable)property.objectReferenceValue).storedValue;
}
protected override void ApplyValue(SerializedProperty property, float newValue)
{
((FloatVariable)property.objectReferenceValue).storedValue = newValue;
}
protected override float EditValue(Rect position, float valueToEdit)
{
return EditorGUI.FloatField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), " ", valueToEdit);
}
}
[CustomPropertyDrawer(typeof(IntVariable))]
public class IntVariableDrawer : CustomVariableDrawer<int>
{
protected override int GetValueToEdit(SerializedProperty property)
{
return ((IntVariable)property.objectReferenceValue).storedValue;
}
protected override void ApplyValue(SerializedProperty property, int newValue)
{
((IntVariable)property.objectReferenceValue).storedValue = newValue;
}
protected override int EditValue(Rect position, int valueToEdit)
{
return EditorGUI.IntField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), " ", valueToEdit);
}
}
[CustomPropertyDrawer(typeof(StringVariable))]
public class StringVariableDrawer : CustomVariableDrawer<string>
{
protected override string GetValueToEdit(SerializedProperty property)
{
return ((StringVariable)property.objectReferenceValue).storedValue;
}
protected override void ApplyValue(SerializedProperty property, string newValue)
{
((StringVariable)property.objectReferenceValue).storedValue = newValue;
}
protected override string EditValue(Rect position, string valueToEdit)
{
return EditorGUI.TextField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), " ", valueToEdit);
}
}
[CustomPropertyDrawer(typeof(BoolVariable))]
public class BoolVariableDrawer : CustomVariableDrawer<bool>
{
protected override bool GetValueToEdit(SerializedProperty property)
{
return ((BoolVariable)property.objectReferenceValue).storedValue;
}
protected override void ApplyValue(SerializedProperty property, bool newValue)
{
((BoolVariable)property.objectReferenceValue).storedValue = newValue;
}
protected override bool EditValue(Rect position, bool valueToEdit)
{
return EditorGUI.Toggle(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), " ", valueToEdit);
}
}