I have a Stat class that is something used in monobehaviours.
It has three members i want to draw. The stat class has a custom drawer.
Stat
{
float baseValue;
float TotalValue();
List modifiers;
}
Im using the override of CreatePropertyGUI. In this method i render “baseValue” like this:
var baseValueProp = property.FindPropertyRelative("baseValue");
baseValueField = new FloatField(name + "BaseValue: ");
baseValueField.BindProperty(baseValueProp);
This works. Next one is trickier since its a method. I solved it by adding an extra varible called serialisedCurrentValue which i update in the class itself. This also works so i can display the return value from the TotalValue() method like this:
var currentValueProp = property.FindPropertyRelative("serialisedCurrentValue");
currentValueLabel.BindProperty(currentValueProp);
Im not sure this is correct though. Having to have an extra variable in the class is annoying and if i would have to have many of them the class would be cluttered with ui stuff. Is this the way its intended?
The last one: List modifiers, i havent figured out yet. For each modifier i would like to show a label and a graphical status bar. But since i have to declare labels and add them to the visual element container in the CreatePropertyGUI method i dont understand how to keep the list dynamic.
Shoul i perhaps make a custom drawer for List ? In that case do i need to call some method inside of CreatePropertyGUI to trigger the custom drawer for List ?
I am reasonably sure there is something fundamental that i dont understand about how to use UI Toolkit and or serialisation but i cant figure it out so i appreciate a push in the right direction.
Here is the whole custom PropertyDrawer so far:
[CustomPropertyDrawer(typeof(FloatStat), true)]
public class FloatStatPropertyDrawer : PropertyDrawer
{
VisualElement Content;
FloatField baseValueField;
Label currentValueLabel = new Label();
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var container = new VisualElement();
var name = property.name;
var baseValueProp = property.FindPropertyRelative("baseValue");
baseValueField = new FloatField(name + "BaseValue: ");
baseValueField.BindProperty(baseValueProp);
var currentValueProp = property.FindPropertyRelative("serialisedCurrentValue");
currentValueLabel.BindProperty(currentValueProp);
var mods = property.FindPropertyRelative("mods");
var modis = property.FindPropertyRelative("modifiers");
if (mods != null)
{
for (int i = 0; i < mods.arraySize; i++)
{
SerializedProperty modifierProp = mods.GetArrayElementAtIndex(i);
var modValue = modifierProp.FindPropertyRelative("modValue").floatValue.ToString();
var description = modifierProp.FindPropertyRelative("Description").stringValue;
Debug.Log(modValue + description);
}
}
container.Add(currentValueLabel);
container.Add(baseValueField);
return container;
}
}