using UnityEngine; using UnityEngine.Rendering; using UnityEngine.UIElements; using UnityUtils; // https://discussions.unity.com/t/uitoolkit-world-space-support-status/887441/22 // https://gist.github.com/katas94/7b220a591215efc36110860a0b1125eb public class WorldSpaceUIDocument : MonoBehaviour { #region Fields const string k_transparentShader = "Unlit/Transparent"; const string k_textureShader = "Unlit/Texture"; const string k_mainTex = "_MainTex"; static readonly int MainTex = Shader.PropertyToID(k_mainTex); [Header("Panel Configuration")] [Tooltip("Width of the panel in pixels.")] [SerializeField] int panelWidth = 1280; [Tooltip("Height of the panel in pixels.")] [SerializeField] int panelHeight = 720; [Tooltip("Scale of the panel (like zoom in a browser).")] [SerializeField] float panelScale = 1.0f; [Tooltip("Pixels per world unit. Determines the real-world size of the panel.")] [SerializeField] float pixelsPerUnit = 500.0f; [Header("Dependencies")] [Tooltip("Visual tree asset for this panel.")] [SerializeField] VisualTreeAsset visualTreeAsset; [Tooltip("PanelSettings prefab instance.")] [SerializeField] PanelSettings panelSettingsAsset; [Tooltip("RenderTexture prefab instance.")] [SerializeField] RenderTexture renderTextureAsset; MeshRenderer meshRenderer; UIDocument uiDocument; PanelSettings panelSettings; RenderTexture renderTexture; Material material; #endregion void Awake() { InitializeComponents(); BuildPanel(); } public void SetLabelText(string label, string text) { if (uiDocument.rootVisualElement == null) { uiDocument.visualTreeAsset = visualTreeAsset; } // Consider caching the label element for better performance uiDocument.rootVisualElement.Q