What is the correct way to expose component fields?

Hello, I’m getting started with UI Elements trying to make a custom editor. Basically I just want to make some functionality available via buttons and show/hide some things selectively. It’s for a component that has some properties like a rect transform and an enum that I’d like to expose, so they can be set in the inspector just like normal. However, I’m having trouble with this seemingly simple task.

I watched the YouTube video with the turrets, and it seems like they’re using an IMGUI container to expose the fields? I find it hard to believe that with all the hype behind UIElements, we have to fall back to IMGUI for something as trivial as making a transform reference visible.

I tried using a BindableElement this way <BindableElement binding-path="myRectTransform"></BindableElement> (which likely isn’t the right way to do anything but I was unable to locate any sample code relevant to my objective) but it gives this error:

Field type UnityEngine.UIElements.BindableElement is not compatible with PPtr<$RectTransform> property "myRectTransform"
UnityEditor.InspectorWindow:RedrawFromNative()

Sorry for the newbie question, I’m sure the answer to this is very simple, I just haven’t been able to find it.

Figured it out: PropertyField

<UXML xmlns="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
    <VisualElement name="row" class="container">
        <ue:PropertyField binding-path="myComponentFieldName"></ue:PropertyField>
    </VisualElement>
</UXML>

As I expected, extremely simple. I simply didn’t know where to look or what to look for.
A small catch to note is that PropertyField is in the editor namespace instead of the engine, so the xmlns:ue="UnityEditor.UIElements" bit is necessary too.

2 Likes