So this post is kind of two fold.
First from a user standpoint, I feel there is a large piece missing from UI Builder. In uGUI you could wire up your events directly in the inspector. Shouldn’t this functionality also exist in UIBuilder? Or is the underlying system so different that this isn’t practical?
Which leads to the second point, trying to understand HOW the underlying system works. I have made a simple inspector using UI Builder. It has some property fields and a couple buttons for now. Nothing amazing. Now I want to hook those buttons up to do something. I can’t seem to figure it out though. I tried some code that seems like it should work based on the docs, but doesn’t appear to do anything. It doesn’t error, or throw any warnings, it just does nothing when I click on the button.
[CustomEditor(typeof(Poisson))]
public class PoissonEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/PoissonInspector.uxml");
var root = visualTree.CloneTree();
root.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/PoissonInspector.uss"));
root.Query<Button>("sample").First().RegisterCallback<MouseDownEvent>(SamplePoints);
return root;
}
private void SamplePoints(MouseDownEvent evt)
{
Debug.Log("Mouse Down");
Poisson p = target as Poisson;
if (p != null)
{
p.CreatePoints();
}
}
}
For reference here is the super basic uxml as well:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<ui:VisualElement style="flex-grow: 1;">
<Style src="PoissonInspector.uss" />
<uie:PropertyField name="density" view-data-key="density" binding-path="density" label="Point Density" />
<uie:PropertyField name="minRadius" binding-path="minRadius" label="Minimum Point Radius" />
<uie:PropertyField name="maxRadius" binding-path="maxRadius" label="Maximum Point Radius" />
<uie:PropertyField name="size" binding-path="size" label="Area" />
<uie:PropertyField name="prefab" binding-path="prefab" label="Prefab to Spawn" />
<uie:PropertyField name="image" binding-path="image" label="Image Influence" />
<ui:Button text="Generate Points" name="sample" binding-path="sample-points" />
</ui:VisualElement>
</ui:UXML>