visualTreeAsset
The VisualTreeAsset automatically loaded into the root visual element.
阅读时间1 分钟最后更新于 2 天前
Definition
- Type: Property
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
public VisualTreeAsset visualTreeAsset { get; set; }
Remarks
This property is optional. If it's null, the root visual element is an empty container, and you can add elements to it from a UI reload callback. Changing this property rebuilds the UI and invokes the callbacks registered with PanelRenderer.RegisterUIReloadCallback.
The following example leaves this property empty and builds the UI from C# code in a UI reload callback.
Examples
using UnityEngine;using UnityEngine.UIElements;public class SimpleRuntimeUI : MonoBehaviour{ Button button; Toggle toggle; int clickCount = 0; int uiVersion = 0; void Awake() { GetComponent<PanelRenderer>().RegisterUIReloadCallback(OnUIReload); } void OnDestroy() { GetComponent<PanelRenderer>().UnregisterUIReloadCallback(OnUIReload); } private void OnUIReload(PanelRenderer panelRenderer, VisualElement root, int version) { // The version number only changes when the UI actually reloads, // so this checks prevents duplicated elements. if (uiVersion == version) return; uiVersion = version; button = new Button() { text = "Click me!" }; root.Add(button); button.RegisterCallback<ClickEvent>(PrintClickMessage); toggle = new Toggle() { text = "Show click count" }; root.Add(toggle); var input = new TextField() { label = "Type something:" }; root.Add(input); input.RegisterCallback<ChangeEvent<string>>(InputMessage); } private void PrintClickMessage(ClickEvent evt) { ++clickCount; Debug.Log($"{"button"} was clicked!" + (toggle.value ? " Count: " + clickCount : "")); } public static void InputMessage(ChangeEvent<string> evt) { Debug.Log($"{evt.newValue} -> {evt.target}"); }}