Hi, I’ve been working hard these days developing a Utility AI system, I’ve created many things and one of them is a tool for editing the node-based agent consideration algorithm and I came to the conclusion that creating separate UI elements for each node type is a pain in the ass, so I decided to create one node and generate visual elements based on the model, except for Serailization which only works with unity types, I didn’t find any other ways to create visual elements for nodes. So I created my own, but does it really have to be this way, I’m sure that unity has tools to create appearance for different types as it does in ScriptableObject or MonoBehavior
public static class EditorUtilities
{
public static VisualElement CreateVisualContent(object target)
{
var root = new VisualElement();
target.GetType().GetFields().ToList().ForEach(field =>
{
Debug.Log(field);
if(field.FieldType == typeof(float))
{
var floatField = new FloatField(field.Name);
floatField.value = (float)field.GetValue(target);
floatField.RegisterValueChangedCallback(evt =>
{
field.SetValue(target, evt.newValue);
});
root.Add(floatField);
}
if(field.FieldType == typeof(string))
{
var textField = new TextField(field.Name);
textField.value = (string)field.GetValue(target);
textField.RegisterValueChangedCallback(evt =>
{
field.SetValue(target, evt.newValue);
});
root.Add(textField);
}
if(field.FieldType.IsEnum)
{
var enumField = new EnumField(field.Name, (Enum)field.GetValue(target));
enumField.RegisterValueChangedCallback(evt =>
{
field.SetValue(target, evt.newValue);
});
root.Add(enumField);
}
});
return root;
}
}