UIEditor binding objects to elements in runtime

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;
            }
        }
    }

Hi!

When you say you want to use the editors control at runtime, I assume you mean to eventually build a player with them. If so, that is not currently possible in 2020.3, since the editor controls will not be available in a build. This is something we’ve started to address by moving editor controls to runtime.

The Bind method and the SerializedObject are also editor-only, so while this might work in play mode inside the editor, it will not work in a build. In fact, the build will not compile.

We are currently working towards supporting runtime bindings. We aim to have a system that is a lot more flexible than the editor bindings we have today, so that we can bind on more than the value of a control. This will be available in an upcoming version of Unity.

At the moment, at runtime, you will need to bind the data to the UI manually.

Thanks for the reply! I sort of discovered that myself but it is good to hear the technical reason. I should have guessed when I had to click the magical Editor Extension checkbox and all the cool UI elements appeared :smile:

Currently I want some basic items like sliders and float field inputs so I suppose I will look at generating custom uxml elements. unless someone can point me to a package or example that has this done already. looks like this is a good place to start. Using custom (C#) elements | UI Builder | 1.0.0-preview.18