Initial import of Rhyculator
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
728bb0c657
315 files changed
+96030
No files matched your search
@@ -0,0 +1,84 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
// 畫面下方的短訊息(跨場景保留),例如「已匯入曲包」
|
||||
public class Toast : MonoBehaviour
|
||||
{
|
||||
private static Toast instance;
|
||||
|
||||
private readonly Queue<string> queue = new Queue<string>();
|
||||
private Text text;
|
||||
private CanvasGroup group;
|
||||
private Coroutine running;
|
||||
|
||||
public static void Show(string message)
|
||||
{
|
||||
Debug.Log("[Toast] " + message);
|
||||
if (instance == null) instance = Create();
|
||||
instance.queue.Enqueue(message);
|
||||
if (instance.running == null) instance.running = instance.StartCoroutine(instance.Run());
|
||||
}
|
||||
|
||||
private static Toast Create()
|
||||
{
|
||||
var root = new GameObject("Toast", typeof(Canvas), typeof(CanvasScaler), typeof(CanvasGroup));
|
||||
DontDestroyOnLoad(root);
|
||||
var canvas = root.GetComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvas.sortingOrder = 500;
|
||||
var scaler = root.GetComponent<CanvasScaler>();
|
||||
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
||||
scaler.referenceResolution = new Vector2(1080, 2400);
|
||||
scaler.matchWidthOrHeight = 0f;
|
||||
|
||||
var bg = new GameObject("Background", typeof(RectTransform)).AddComponent<Image>();
|
||||
bg.transform.SetParent(root.transform, false);
|
||||
bg.color = new Color(0f, 0f, 0f, 0.82f);
|
||||
bg.raycastTarget = false;
|
||||
var rt = bg.rectTransform;
|
||||
rt.anchorMin = rt.anchorMax = new Vector2(0.5f, 0f);
|
||||
rt.pivot = new Vector2(0.5f, 0f);
|
||||
rt.anchoredPosition = new Vector2(0, 260);
|
||||
rt.sizeDelta = new Vector2(960, 150);
|
||||
|
||||
var t = new GameObject("Text", typeof(RectTransform)).AddComponent<Text>();
|
||||
t.transform.SetParent(bg.transform, false);
|
||||
t.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
|
||||
t.fontSize = 38;
|
||||
t.color = Color.white;
|
||||
t.alignment = TextAnchor.MiddleCenter;
|
||||
t.horizontalOverflow = HorizontalWrapMode.Wrap;
|
||||
t.verticalOverflow = VerticalWrapMode.Overflow;
|
||||
t.raycastTarget = false;
|
||||
var trt = t.rectTransform;
|
||||
trt.anchorMin = Vector2.zero;
|
||||
trt.anchorMax = Vector2.one;
|
||||
trt.offsetMin = new Vector2(24, 8);
|
||||
trt.offsetMax = new Vector2(-24, -8);
|
||||
|
||||
var toast = root.AddComponent<Toast>();
|
||||
toast.text = t;
|
||||
toast.group = root.GetComponent<CanvasGroup>();
|
||||
toast.group.alpha = 0f;
|
||||
return toast;
|
||||
}
|
||||
|
||||
private IEnumerator Run()
|
||||
{
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
text.text = queue.Dequeue();
|
||||
group.alpha = 1f;
|
||||
yield return new WaitForSecondsRealtime(3f);
|
||||
for (float a = 1f; a > 0f; a -= Time.unscaledDeltaTime * 2f)
|
||||
{
|
||||
group.alpha = a;
|
||||
yield return null;
|
||||
}
|
||||
group.alpha = 0f;
|
||||
}
|
||||
running = null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user