My first attempt at using UI Elements so I thought I’d start with something really simple - hiding a field based on a choice in another field.
I modified the custom inspector from the Tank example, adding an enum to TankScript.
This is what my research would indicate should work. However my call back is never triggered. I’ve tried with just a debug.log in there, a separate method instead of an inline lambda. I’ve tried changing the class parameter to ChangeEvent to various things (the docs are especially unhelpful on what this parameter should be).
The callback is never fired. Any ideas?
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[CustomEditor(typeof(TankScript))]
public class TankEditor : Editor
{
public override VisualElement CreateInspectorGUI()
{
var visualTree = Resources.Load("tank_inspector_uxml")
as VisualTreeAsset;
var ve = visualTree.CloneTree();
ve.Q("enum-field").RegisterCallback<ChangeEvent<TankScript.MyEnum>>(
evt => {
ve.Q("tank-size-field").visible = (
evt.newValue == TankScript.MyEnum.One);
});
return ve;
}
}
The only other relevant code is this:
using UnityEngine;
public class TankScript : MonoBehaviour
{
public string tankName = "Tank";
public float tankSize = 1;
public enum MyEnum {Zero,One}
public MyEnum enumField = MyEnum.Zero;
}
and
<UXML xmlns:ui="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
<ui:VisualElement name="row" class="container">
<ue:PropertyField binding-path="tankName" name="tank-name-field" />
<ue:PropertyField binding-path="tankSize" name="tank-size-field" />
<ue:PropertyField binding-path="enumField" name="enum-field" />
</ui:VisualElement>
</UXML>