1c6c9b2a70
Add Phase A greybox vertical slice: third-person movement, forest path, checkpoints, story triggers, and light ability gate.
87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace KirbyAdventures
|
|
{
|
|
public class DialogueUI : MonoBehaviour
|
|
{
|
|
public static DialogueUI Instance { get; private set; }
|
|
|
|
Text _text;
|
|
Image _panel;
|
|
float _hideAt;
|
|
|
|
public static DialogueUI EnsureExists()
|
|
{
|
|
if (Instance != null) return Instance;
|
|
var go = new GameObject("DialogueUI");
|
|
DontDestroyOnLoad(go);
|
|
return go.AddComponent<DialogueUI>();
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
Build();
|
|
}
|
|
|
|
void Build()
|
|
{
|
|
var canvas = gameObject.AddComponent<Canvas>();
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
canvas.sortingOrder = 50;
|
|
gameObject.AddComponent<CanvasScaler>().uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
|
gameObject.AddComponent<GraphicRaycaster>();
|
|
|
|
var panelGo = new GameObject("Panel");
|
|
panelGo.transform.SetParent(transform, false);
|
|
_panel = panelGo.AddComponent<Image>();
|
|
_panel.color = new Color(0.05f, 0.07f, 0.1f, 0.82f);
|
|
var prt = _panel.rectTransform;
|
|
prt.anchorMin = new Vector2(0.12f, 0.05f);
|
|
prt.anchorMax = new Vector2(0.88f, 0.22f);
|
|
prt.offsetMin = Vector2.zero;
|
|
prt.offsetMax = Vector2.zero;
|
|
|
|
var textGo = new GameObject("Quote");
|
|
textGo.transform.SetParent(panelGo.transform, false);
|
|
_text = textGo.AddComponent<Text>();
|
|
_text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
|
|
if (_text.font == null)
|
|
_text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
|
_text.fontSize = 26;
|
|
_text.alignment = TextAnchor.MiddleCenter;
|
|
_text.color = new Color(0.95f, 0.93f, 0.88f);
|
|
_text.horizontalOverflow = HorizontalWrapMode.Wrap;
|
|
_text.verticalOverflow = VerticalWrapMode.Overflow;
|
|
var trt = _text.rectTransform;
|
|
trt.anchorMin = new Vector2(0.04f, 0.1f);
|
|
trt.anchorMax = new Vector2(0.96f, 0.9f);
|
|
trt.offsetMin = Vector2.zero;
|
|
trt.offsetMax = Vector2.zero;
|
|
|
|
_panel.gameObject.SetActive(false);
|
|
}
|
|
|
|
public static void ShowTemporary(string quote, float seconds)
|
|
{
|
|
EnsureExists();
|
|
Instance.Show(quote, seconds);
|
|
}
|
|
|
|
public void Show(string quote, float seconds)
|
|
{
|
|
if (_text == null) return;
|
|
_text.text = quote;
|
|
_panel.gameObject.SetActive(true);
|
|
_hideAt = Time.unscaledTime + seconds;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (_panel != null && _panel.gameObject.activeSelf && Time.unscaledTime >= _hideAt)
|
|
_panel.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|