I have been building a simple UI for my game using the UI Toolkit and UI Builder both at v1.0-preview.14. Building the UI was straightforward enough however I seem to be having a bit of trouble interacting with it from C#. Most code examples for the UIToolkit reference the UIDocument then use this to get the root VisualElement - something like what I have done here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class BattleUI : MonoBehaviour
{
[SerializeField]private UIDocument uiDocument;
private VisualElement container;
private Label scoreLabel;
// Start is called before the first frame update
void Start()
{
container = uiDocument.rootVisualElement.Q<VisualElement>("LeftColumn");
scoreLabel = container.Q<Label>("ScoreLabel");
GameCore.OnScoreChangedEvent += GameCore_OnScoreChanged;
}
private void GameCore_OnScoreChanged (object sender, GameCore.OnScoreChangedArgs e)
{
scoreLabel.text = e.newScore;
}
}
It seems that the type UnityEngine.UIElements.UIDocument cannot be resolved. I don’t know if this is some sort of namespace conflict or I am missing something obvious, however UIDocument is definitely listed as a class under UnityEngine.UIElements in the UIToolkit docs: | UI Toolkit | 1.0.0-preview.18 . Does anyone know what is causing this?