2025-09-03 15:43:47 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
|
|
|
|
public class DebugUIMessage : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
private static DebugUIMessage instance;
|
|
|
|
|
|
private Text messageText;
|
|
|
|
|
|
private Canvas canvas;
|
|
|
|
|
|
private Coroutine hideCoroutine;
|
|
|
|
|
|
|
|
|
|
|
|
public static void Show(string message, float duration = 2f)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (instance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var go = new GameObject("DebugUIMessage");
|
|
|
|
|
|
instance = go.AddComponent<DebugUIMessage>();
|
|
|
|
|
|
instance.SetupUI();
|
|
|
|
|
|
DontDestroyOnLoad(go);
|
|
|
|
|
|
}
|
|
|
|
|
|
instance.ShowMessage(message, duration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SetupUI()
|
|
|
|
|
|
{
|
|
|
|
|
|
canvas = new GameObject("DebugUICanvas").AddComponent<Canvas>();
|
|
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
|
|
|
|
canvas.sortingOrder = 9999;
|
|
|
|
|
|
canvas.gameObject.transform.SetParent(transform);
|
|
|
|
|
|
var textGO = new GameObject("DebugUIText");
|
|
|
|
|
|
textGO.transform.SetParent(canvas.transform);
|
|
|
|
|
|
messageText = textGO.AddComponent<Text>();
|
|
|
|
|
|
messageText.alignment = TextAnchor.MiddleCenter;
|
2025-09-03 16:55:21 +02:00
|
|
|
|
// Try to load a custom font from Resources/Fonts/DebugFont.ttf
|
|
|
|
|
|
Font customFont = Resources.Load<Font>("Fonts/DebugFont");
|
|
|
|
|
|
if (customFont != null)
|
|
|
|
|
|
messageText.font = customFont;
|
|
|
|
|
|
else
|
|
|
|
|
|
messageText.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
|
|
|
|
|
|
messageText.fontSize = 16;
|
2025-09-03 15:43:47 +02:00
|
|
|
|
messageText.color = Color.yellow;
|
2025-09-03 16:55:21 +02:00
|
|
|
|
var outline = textGO.AddComponent<Outline>();
|
|
|
|
|
|
outline.effectColor = Color.black;
|
|
|
|
|
|
outline.effectDistance = new Vector2(2, -2);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
var rect = messageText.GetComponent<RectTransform>();
|
2025-09-03 16:55:21 +02:00
|
|
|
|
rect.anchorMin = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
rect.anchorMax = new Vector2(0.5f, 0.5f);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
rect.pivot = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
rect.anchoredPosition = Vector2.zero;
|
2025-09-03 16:55:21 +02:00
|
|
|
|
rect.sizeDelta = new Vector2(400, 40);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
messageText.text = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ShowMessage(string message, float duration)
|
|
|
|
|
|
{
|
|
|
|
|
|
messageText.text = message;
|
|
|
|
|
|
messageText.enabled = true;
|
|
|
|
|
|
if (hideCoroutine != null)
|
|
|
|
|
|
StopCoroutine(hideCoroutine);
|
|
|
|
|
|
hideCoroutine = StartCoroutine(HideAfterSeconds(duration));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator HideAfterSeconds(float seconds)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(seconds);
|
|
|
|
|
|
messageText.text = "";
|
|
|
|
|
|
messageText.enabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|