using System.Diagnostics; using TMPro; using UnityEngine; using UnityEngine.UI; namespace RocketUtils { public class DebugInfoBehaviour : MonoBehaviour { #if ROC_DEBUG_MODE || ROC_LOG_MODE private static Canvas _canvas; private static TextMeshProUGUI _debugLabel; private static TextMeshProUGUI _fpsLabel; private static TextMeshProUGUI _timeLabel; private static string labelString = "Rocket Debug"; private float _deltaTime; internal static bool DisableDebugLabels; internal static bool DrawDebugLabel; internal static bool DrawTimeLabel; internal static bool DrawFPSLabel; private void Start() { DontDestroyOnLoad(gameObject); CreateCanvasElementsIfNecessary(); } [Conditional("ROC_DEBUG_MODE")] public void ModifyDebugLabel(string textToAppend) { labelString += textToAppend; _debugLabel.text = labelString; } private void Update() { if (!DrawDebugLabel || DisableDebugLabels) return; if (DrawFPSLabel) { _deltaTime += (Time.unscaledDeltaTime - _deltaTime) * 0.1f; float fps = 1.0f / _deltaTime; _fpsLabel.text = $"{fps:00} FPS"; } if (DrawTimeLabel) { int seconds = (int)Time.time; int minute = (seconds / 60); int hour = (minute / 60); _timeLabel.text = $"Time : {hour:00}:{minute % 60:00}:{seconds % 60:00}"; } } private void CreateCanvasElementsIfNecessary() { if (_canvas != null) return; GameObject canvasObject = new GameObject("DebugCanvas"); canvasObject.transform.SetParent(transform); _canvas = canvasObject.AddComponent(); _canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvasObject.AddComponent(); canvasObject.AddComponent(); // Create Debug Label _debugLabel = CreateTextElement("DebugLabel", new Vector2(65, 25), 24, canvasObject.transform, new Vector2(0, 0)); _debugLabel.text = labelString; // Create FPS Label _fpsLabel = CreateTextElement("FPSLabel", new Vector2(10, -67.5f), 28, canvasObject.transform, new Vector2(0, 1)); // Create Time Label _timeLabel = CreateTextElement("TimeLabel", new Vector2(10, -100), 28, canvasObject.transform, new Vector2(0, 1)); TextMeshProUGUI CreateTextElement(string name, Vector2 anchoredPosition, int fontSize, Transform parent, Vector2 anchor) { GameObject textObject = new GameObject(name); textObject.transform.SetParent(parent); RectTransform rectTransform = textObject.AddComponent(); rectTransform.anchorMin = anchor; // Bottom left rectTransform.anchorMax = anchor; // Bottom left rectTransform.pivot = new Vector2(0, 0); rectTransform.anchoredPosition = anchoredPosition; TextMeshProUGUI text = textObject.AddComponent(); text.fontSize = fontSize; text.color = Color.white; return text; } } #endif } }