Hi there, I am using unity 2020.3 and I am building a drawing app, still very new to Unity. I would like to use some of the visual elements from the UI editor during runtime using the UIBuilder (which is awesome btw).
I understand you can’t yet bind items directly but I am curious how to work around this.
For example. if I want to use the Vector3 field to control the position of an object and update when the object is moved. the following seems to work to move the position but how can I update the field when the object moves? is there a version of unity available that has runtime binding of objects?
public class UIController : MonoBehaviour
{
GameObject _selectedItem;
AppController _controller;
Vecotor3Field positionField;
void Awake()
{
var root = GetComponent<UIDocument>().rootVisualElement;
positionField = root.Q<Vector3Field>("position-field");
positionField.RegisterCallback<ChangeEvent<Vector3>>((evt) =>
{
_selectedItem.transform.position = positionField.value;
});
void Update()
{
if (_controller.GetSelectedObject() != _selectedItem)
{
_selectedItem = _controller.GetSelectedItem();
positionField.value = _selectedItem.transform.position;
//doesn't seem to do anything
positionField.Bind(new SerializedObject(currentItem));
}
//maybe something like:
if (!positionField.focus)
{ // doesn't work.. but how to tell if field is focused?
positionField.value = _selected.transform.position;
}
}
}