added: debug overlay functional
This commit is contained in:
97
Assets/Scripts/Core/Tools/DebugOverlayDrawer.cs
Normal file
97
Assets/Scripts/Core/Tools/DebugOverlayDrawer.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
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<GameObject, List<string>> Pages = new(); // Page and value names
|
||||
[ShowInInspector]
|
||||
public Dictionary<object, Label> 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<string>());
|
||||
thisPage.transform.SetParent(Instance.canvasRoot.transform);
|
||||
thisPage.AddComponent<UIDocument>();
|
||||
}
|
||||
|
||||
// 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<UIDocument>().rootVisualElement.Add(thisElement);
|
||||
thisElement.style.alignSelf = new StyleEnum<Align>(Align.Center);
|
||||
|
||||
// Set it's label
|
||||
((Label)thisElement.Query<VisualElement>("Label").First()).text = sourceName;
|
||||
|
||||
|
||||
Instance.values.Add($"{pageName}/{sourceName}", (Label)thisElement.Query<VisualElement>("Value"));
|
||||
|
||||
}
|
||||
|
||||
public static void ChangeValue(string pageName, string sourceName, string newValue){
|
||||
Instance.values[$"{pageName}/{sourceName}"].text = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Core/Tools/DebugOverlayDrawer.cs.meta
Normal file
2
Assets/Scripts/Core/Tools/DebugOverlayDrawer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bef2d32c8ac26b643851a4c8f18dacca
|
||||
10
Assets/Scripts/Core/Tools/DebugOverlayEntry.cs
Normal file
10
Assets/Scripts/Core/Tools/DebugOverlayEntry.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Reset.Core.Tools{
|
||||
public class DebugOverlayEntry{
|
||||
public string page;
|
||||
public string trackedName;
|
||||
public string trackedValue;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Core/Tools/DebugOverlayEntry.cs.meta
Normal file
3
Assets/Scripts/Core/Tools/DebugOverlayEntry.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e168fc1f77fe443b97595b004e80af7e
|
||||
timeCreated: 1756152238
|
||||
Reference in New Issue
Block a user