Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

VisualElementReference<T0>

class in UnityEngine.UIElements

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Represents a reference to a VisualElement in a PanelRenderer.

This example shows how to use VisualElementReference to reference an element in a UXML file that is loaded by a <see cref="PanelRenderer" />.

using UnityEngine;
using UnityEngine.UIElements;

public class VisualElementReference_Example : MonoBehaviour { // Set via the inspector public VisualElementReference<VisualElement> elementReference = new VisualElementReference<VisualElement>(); public Color customColor = Color.red;

void Start() { elementReference.referenceResolved += SetupButton; }

void SetupButton(VisualElement ve) { ve.style.backgroundColor = customColor; } }

The following example configures paths to reference elements in a nested UXML structure. Root UXML:

<ui:UXML xmlns:ui="UnityEngine.UIElements" editor-extension-mode="False">
    <ui:Template name="VisualElementReference_ExampleTemplate" src="./VisualElementReference_ExampleTemplate.uxml"/>
    <ui:VisualElement name="my-element" authoring-id="123"/>
    <ui:Button text="Button" name="my-button" authoring-id="-5"/>
    <ui:Instance template="VisualElementReference_ExampleTemplate" name="instance1" authoring-id="1"/>
    <ui:Instance template="VisualElementReference_ExampleTemplate" name="instance2" authoring-id="2"/>
</ui:UXML>

Template UXML:

<ui:UXML xmlns:ui="UnityEngine.UIElements" editor-extension-mode="False">
    <ui:VisualElement name="template-element" authoring-id="1"/>
    <ui:Button text="Button" name="template-button" authoring-id="101"/>
</ui:UXML>
using UnityEngine;
using UnityEngine.UIElements;

public class VisualElementReference_ExampleNested : MonoBehaviour { public VisualElementReference<Button> templateInstance1Button = new VisualElementReference<Button>(); public VisualElementReference<VisualElement> elementReference = new VisualElementReference<VisualElement>();

void Start() { var pr = GetComponent<PanelRenderer>();

// 101 is the AuthoringId of the Button inside the template instance with AuthoringId 1 templateInstance1Button.SetReference(pr, new AuthoringIdPath(1, 101)); templateInstance1Button.referenceResolved += SetupButtonReference; templateInstance1Button.referenceUnloaded += TeardownButtonReference;

// 123 is the AuthoringId of a VisualElement in the root UXML file elementReference.SetReference(pr, new AuthoringIdPath(123)); elementReference.referenceResolved += SetupElementReference; }

void SetupButtonReference(Button button) { button.clicked += OnButtonClick; }

void TeardownButtonReference(Button button) { button.clicked -= OnButtonClick; }

void OnButtonClick() { Debug.Log("Button inside template instance clicked!"); }

void SetupElementReference(VisualElement ve) { ve.Add(new Label { text = "Element Reference Resolved!" }); } }

Properties

Property Description
authoringPath The path to the referenced element. SetReference
panelRenderer The provider used to resolve the reference. SetReference

Public Methods

Method Description
Equals Indicates whether the current object is equal to another element reference.
SetReference Sets the reference to point to the given document and path.

Events

Event Description
referenceResolved Callback invoked when the reference is resolved from the document. When you add a callback, if the reference is already resolved, the callback is immediately invoked.
referenceUnloaded Invoked when the referenced object is unloaded. This occurs when the document is destroyed, such as when a live reload occurs after the VisualTreeAsset changes. At this point, all references are invalid and should be cleared.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961