using System; using System.Collections.Generic; using System.Linq; using Sirenix.OdinInspector; using UnityEngine; using UnityEngine.UIElements; namespace Reset.Core.Tools{ public class DebugOverlayDrawer : MonoBehaviour{ [ShowInInspector] public Dictionary> Pages = new(); // Page and value names [ShowInInspector] public Dictionary values = new(); // Value name and value value (lol) public string pageNamePrefix = "Debug Page: "; public VisualTreeAsset template; [ShowInInspector] public UIDocument root; public GameObject canvasRoot; public GameObject currentPage; public enum DebugOverlayAnchor{ TopLeft, TopRight, BottomLeft, BottomRight, Center } void Awake(){ if (Instance != null) { Destroy(this); } Instance = this; } public static DebugOverlayDrawer Instance; public static void AddOnOverlay(string pageName, string sourceName, Vector3 position){ // Make sure the page exists. If it does, use it. If not, create it GameObject thisPage = null; bool alreadyExisted = false; // This checks for a gameobject with the page name. Sets it if it finds it foreach (var page in Instance.Pages) { if (page.Key.name == $"{Instance.pageNamePrefix}{pageName}") { thisPage = page.Key; alreadyExisted = true; break; } } // Otherwise, this will make a new one and add it if (thisPage == null) { thisPage = new GameObject(name: $"{Instance.pageNamePrefix}{pageName}"); Instance.Pages.Add(thisPage, new List()); thisPage.transform.SetParent(Instance.canvasRoot.transform); thisPage.AddComponent(); } // Warning for the page being null for whatever reason if (thisPage == null) { Debug.LogError( $"Debug Overlay Manager never found a page named: {sourceName}. It failed to create one and aborted."); return; } // Now, iterate through this page and see if the variable is already tracked if (alreadyExisted && Instance.Pages[thisPage].Contains(sourceName)) { return; } // Since it doesn't exist, add it to the list of tracked variables Instance.Pages[thisPage].Add(sourceName); // Then create a new on-screen element for this variable VisualElement thisElement = (Instance.template.Instantiate()); thisPage.GetComponent().rootVisualElement.Add(thisElement); thisElement.style.alignSelf = new StyleEnum(Align.Center); // Set it's label ((Label)thisElement.Query("Label").First()).text = sourceName; Instance.values.Add($"{pageName}/{sourceName}", (Label)thisElement.Query("Value")); } public static void ChangeValue(string pageName, string sourceName, string newValue){ Instance.values[$"{pageName}/{sourceName}"].text = newValue; } } }