Hi! I’m trying to create a state machine editor for my game and I want to do it with UI Toolkit. I managed to get something on screen but I wonder if there is a better way to achieve the same thing. This is how I did it
-
Created a box representing a state in one UXML-file. Root VisualElement named “root”
-
Created an empty VisualElement-container in another UXML, and added it to a GameObject(named “UIContainer”) with a UIDocument. Root VisualElement named “container”.
-
Added the box UXML to a UIDocument in a different GameObject, made a prefab and discarded the GameObject.
-
Added this script to “UIContainer”
public class VisualScriptingManager : MonoBehaviour
{
public Transform baseBoxPrefab;
void Start()
{
var t = Instantiate(baseBoxPrefab, new Vector3(0, 0, 0), Quaternion.identity);
var r = t.GetComponent<UIDocument>().visualTreeAsset.CloneTree();
gameObject.GetComponent<UIDocument>().rootVisualElement.Q("container").Add(r.Q("root"));
DestroyImmediate(t.gameObject);
}
}
I’m sure there is a better way to do this. I’m still a Unity newbie, despite having used it on and off for years.
Bonus question: When the state box is rendered it is really small compared to what it looks like in the UI Builder. What’s that about?