- One empty scene; App builds the canvas, safe-area layout and a shared 5x6 Grid in code. Pages (Intro, Main, Choose, Play, Account, Settings) replace 5 scenes, 5 prefabs and the per-cell spawner/display components. - Packs: the game reads Rhyculator/package/ (with All files access) or the app folder, like a plugged-in USB stick. Zips stay where they are and are extracted to a cache keyed by name/size/mtime; folders are read directly. Scans run every second on a background thread. Old songs/ imports and inbox zips are migrated automatically. - Download packs by 8-char code from the new pack website, or via rhyculator://pack/<code> links; opened/shared zips go into the package folder. - Play: pause/resume/retry, auto-pause on background, combo and progress bar; judging and scoring unchanged. Choose page fits 9:16 screens; empty state, pack delete. Android back key handled everywhere. - Slimming: removed TextMesh Pro, unused 2D/Timeline/VisualScripting/etc. packages and modules, scene template, unused sprites; menu theme wav (20MB) -> ogg (2.3MB). Build profile replaced by explicit settings in BuildAndroid. - Tests: PackLibrary/PackReader/migration/serializer tests (39 passing). Co-Authored-By: Claude Opus 5.5 <[email protected]>
300 lines
12 KiB
C#
300 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
// 以程式建立 UI 的小工具(遊戲沒有預先擺好的場景與 prefab)
|
|
// 單位:畫布寬固定 1080
|
|
public static class Ui
|
|
{
|
|
public static readonly Color Bg = Color.black;
|
|
public static readonly Color Panel = new Color(0.09f, 0.09f, 0.11f);
|
|
public static readonly Color ButtonFill = new Color(0.16f, 0.16f, 0.19f);
|
|
public static readonly Color Line = new Color(0.33f, 0.33f, 0.36f);
|
|
public static readonly Color TextColor = new Color(0.93f, 0.93f, 0.95f);
|
|
public static readonly Color Muted = new Color(0.6f, 0.6f, 0.64f);
|
|
public static readonly Color Accent = new Color(1f, 0.4f, 0.77f);
|
|
public static readonly Color Blue = new Color(0.25f, 0.64f, 1f);
|
|
public static readonly Color Danger = new Color(1f, 0.38f, 0.38f);
|
|
// 難度顏色:[0] 未用、easy、normal、hard
|
|
public static readonly Color[] LevelColors =
|
|
{
|
|
Color.white, new Color(0.49f, 0.84f, 0.49f), new Color(1f, 0.31f, 0.31f), new Color(0.71f, 0.39f, 1f),
|
|
};
|
|
|
|
private static Font font;
|
|
public static Font Font => font != null ? font : (font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf"));
|
|
|
|
private static Sprite rounded;
|
|
// 圓角矩形(9-slice),按鈕、輸入框、對話框共用
|
|
public static Sprite Rounded
|
|
{
|
|
get
|
|
{
|
|
if (rounded != null) return rounded;
|
|
const int size = 64, r = 20;
|
|
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp };
|
|
var px = new Color32[size * size];
|
|
for (int y = 0; y < size; y++)
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
float dx = Mathf.Max(0, Mathf.Max(r - x - 0.5f, x + 0.5f - (size - r)));
|
|
float dy = Mathf.Max(0, Mathf.Max(r - y - 0.5f, y + 0.5f - (size - r)));
|
|
float a = Mathf.Clamp01(r - Mathf.Sqrt(dx * dx + dy * dy) + 0.5f);
|
|
px[y * size + x] = new Color32(255, 255, 255, (byte)(a * 255));
|
|
}
|
|
tex.SetPixels32(px);
|
|
tex.Apply(false, true);
|
|
rounded = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), 100, 0,
|
|
SpriteMeshType.FullRect, new Vector4(r, r, r, r));
|
|
return rounded;
|
|
}
|
|
}
|
|
|
|
// ---------- 基本元件 ----------
|
|
|
|
public static RectTransform Rect(Transform parent, string name = "Rect")
|
|
{
|
|
var go = new GameObject(name, typeof(RectTransform));
|
|
go.transform.SetParent(parent, false);
|
|
return (RectTransform)go.transform;
|
|
}
|
|
|
|
public static RectTransform Stretch(RectTransform rt, float left = 0, float bottom = 0, float right = 0, float top = 0)
|
|
{
|
|
rt.anchorMin = Vector2.zero;
|
|
rt.anchorMax = Vector2.one;
|
|
rt.offsetMin = new Vector2(left, bottom);
|
|
rt.offsetMax = new Vector2(-right, -top);
|
|
return rt;
|
|
}
|
|
|
|
// 以錨點(0~1)放置:pivot 與錨點相同,pos 為偏移
|
|
public static RectTransform Place(RectTransform rt, Vector2 anchor, Vector2 pos, Vector2 size)
|
|
{
|
|
rt.anchorMin = rt.anchorMax = rt.pivot = anchor;
|
|
rt.anchoredPosition = pos;
|
|
rt.sizeDelta = size;
|
|
return rt;
|
|
}
|
|
|
|
public static Text Label(Transform parent, string text, int size, Color color, TextAnchor align = TextAnchor.MiddleCenter)
|
|
{
|
|
var t = Rect(parent, "Text").gameObject.AddComponent<Text>();
|
|
t.font = Font;
|
|
t.text = text;
|
|
t.fontSize = size;
|
|
t.color = color;
|
|
t.alignment = align;
|
|
t.horizontalOverflow = HorizontalWrapMode.Wrap;
|
|
t.verticalOverflow = VerticalWrapMode.Overflow;
|
|
t.raycastTarget = false;
|
|
t.supportRichText = true;
|
|
return t;
|
|
}
|
|
|
|
public static Image Image(Transform parent, Sprite sprite, Color color)
|
|
{
|
|
var img = Rect(parent, "Image").gameObject.AddComponent<Image>();
|
|
img.sprite = sprite;
|
|
img.color = color;
|
|
img.raycastTarget = false;
|
|
if (sprite == Rounded) img.type = UnityEngine.UI.Image.Type.Sliced;
|
|
return img;
|
|
}
|
|
|
|
public static Button Button(Transform parent, string label, UnityAction onClick, int fontSize = 44, bool primary = false)
|
|
{
|
|
var img = Image(parent, Rounded, primary ? new Color(0.45f, 0.17f, 0.34f) : ButtonFill);
|
|
img.raycastTarget = true;
|
|
img.name = "Button";
|
|
var btn = img.gameObject.AddComponent<Button>();
|
|
var colors = btn.colors;
|
|
colors.highlightedColor = Color.white;
|
|
colors.pressedColor = new Color(0.7f, 0.7f, 0.7f);
|
|
colors.disabledColor = new Color(0.5f, 0.5f, 0.5f, 0.6f);
|
|
btn.colors = colors;
|
|
if (onClick != null) btn.onClick.AddListener(onClick);
|
|
|
|
var outline = Image(img.transform, Rounded, primary ? Accent : Line);
|
|
outline.fillCenter = false;
|
|
Stretch(outline.rectTransform);
|
|
var t = Label(img.transform, label, fontSize, TextColor);
|
|
Stretch(t.rectTransform, 16, 4, 16, 4);
|
|
return btn;
|
|
}
|
|
|
|
public static void SetButtonText(Button b, string text) => b.GetComponentInChildren<Text>().text = text;
|
|
|
|
public static InputField Input(Transform parent, string placeholder, InputField.ContentType type = InputField.ContentType.Standard, int fontSize = 42)
|
|
{
|
|
var bg = Image(parent, Rounded, new Color(0.05f, 0.05f, 0.06f));
|
|
bg.raycastTarget = true;
|
|
bg.name = "Input";
|
|
var outline = Image(bg.transform, Rounded, Line);
|
|
outline.fillCenter = false;
|
|
Stretch(outline.rectTransform);
|
|
|
|
var text = Label(bg.transform, "", fontSize, TextColor, TextAnchor.MiddleLeft);
|
|
text.supportRichText = false;
|
|
text.horizontalOverflow = HorizontalWrapMode.Overflow;
|
|
Stretch(text.rectTransform, 28, 4, 28, 4);
|
|
var ph = Label(bg.transform, placeholder, fontSize, new Color(0.45f, 0.45f, 0.5f), TextAnchor.MiddleLeft);
|
|
Stretch(ph.rectTransform, 28, 4, 28, 4);
|
|
|
|
var field = bg.gameObject.AddComponent<InputField>();
|
|
field.textComponent = text;
|
|
field.placeholder = ph;
|
|
field.contentType = type;
|
|
field.lineType = InputField.LineType.SingleLine;
|
|
field.caretColor = Accent;
|
|
field.selectionColor = new Color(1f, 0.4f, 0.77f, 0.35f);
|
|
field.caretWidth = 3;
|
|
return field;
|
|
}
|
|
|
|
// 垂直排列容器(表單、清單用):子物件以 LayoutElement 指定高度
|
|
public static RectTransform Column(Transform parent, float spacing = 24, int padding = 0, TextAnchor align = TextAnchor.UpperCenter)
|
|
{
|
|
var rt = Rect(parent, "Column");
|
|
var v = rt.gameObject.AddComponent<VerticalLayoutGroup>();
|
|
v.spacing = spacing;
|
|
v.padding = new RectOffset(padding, padding, padding, padding);
|
|
v.childAlignment = align;
|
|
v.childControlWidth = v.childControlHeight = true;
|
|
v.childForceExpandWidth = true;
|
|
v.childForceExpandHeight = false;
|
|
return rt;
|
|
}
|
|
|
|
public static RectTransform Row(Transform parent, float spacing = 24)
|
|
{
|
|
var rt = Rect(parent, "Row");
|
|
var h = rt.gameObject.AddComponent<HorizontalLayoutGroup>();
|
|
h.spacing = spacing;
|
|
h.childAlignment = TextAnchor.MiddleCenter;
|
|
h.childControlWidth = h.childControlHeight = true;
|
|
h.childForceExpandWidth = true;
|
|
h.childForceExpandHeight = true;
|
|
return rt;
|
|
}
|
|
|
|
public static T Height<T>(T c, float height) where T : Component
|
|
{
|
|
var le = c.GetComponent<LayoutElement>() ?? c.gameObject.AddComponent<LayoutElement>();
|
|
le.minHeight = le.preferredHeight = height;
|
|
return c;
|
|
}
|
|
|
|
// 可捲動的內容區(放在 parent 內填滿);回傳要放內容的 Column
|
|
public static RectTransform Scroll(Transform parent, float spacing = 24, int padding = 24)
|
|
{
|
|
var view = Image(parent, null, new Color(0, 0, 0, 0));
|
|
view.raycastTarget = true; // 讓拖曳可以捲動
|
|
view.name = "Scroll";
|
|
Stretch(view.rectTransform);
|
|
view.gameObject.AddComponent<RectMask2D>();
|
|
var scroll = view.gameObject.AddComponent<ScrollRect>();
|
|
scroll.horizontal = false;
|
|
scroll.movementType = ScrollRect.MovementType.Clamped;
|
|
scroll.scrollSensitivity = 40;
|
|
|
|
var content = Column(view.transform, spacing, padding);
|
|
content.anchorMin = new Vector2(0, 1);
|
|
content.anchorMax = new Vector2(1, 1);
|
|
content.pivot = new Vector2(0.5f, 1);
|
|
content.offsetMin = content.offsetMax = Vector2.zero;
|
|
content.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
|
scroll.viewport = view.rectTransform;
|
|
scroll.content = content;
|
|
return content;
|
|
}
|
|
}
|
|
|
|
// 對話框:半透明遮罩 + 置中面板(標題、內文、自訂內容、按鈕列)
|
|
// 開著時格子不接受觸控;返回鍵關閉最上層(cancellable 時)
|
|
public class Dialog
|
|
{
|
|
private static readonly List<Dialog> open = new List<Dialog>();
|
|
public static bool AnyOpen => open.Count > 0;
|
|
|
|
public readonly RectTransform root;
|
|
public readonly RectTransform body; // 面板內的 Column,可加入自訂內容
|
|
public readonly Text message;
|
|
public bool cancellable = true;
|
|
public event Action Closed;
|
|
private RectTransform buttons;
|
|
|
|
public Dialog(Transform layer, string title, string text = null)
|
|
{
|
|
var dim = Ui.Image(layer, null, new Color(0, 0, 0, 0.75f));
|
|
dim.raycastTarget = true;
|
|
dim.name = "Dialog";
|
|
root = Ui.Stretch(dim.rectTransform);
|
|
|
|
var panel = Ui.Image(root, Ui.Rounded, Ui.Panel);
|
|
panel.raycastTarget = true;
|
|
var prt = panel.rectTransform;
|
|
prt.anchorMin = prt.anchorMax = prt.pivot = new Vector2(0.5f, 0.5f);
|
|
prt.sizeDelta = new Vector2(940, 0);
|
|
var outline = Ui.Image(prt, Ui.Rounded, Ui.Line);
|
|
outline.fillCenter = false;
|
|
Ui.Stretch(outline.rectTransform);
|
|
outline.gameObject.AddComponent<LayoutElement>().ignoreLayout = true;
|
|
|
|
body = Ui.Column(prt, 28, 48);
|
|
var fitter = panel.gameObject.AddComponent<ContentSizeFitter>();
|
|
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
|
// 面板高度跟著內容:讓 Column 的偏好高度傳到面板
|
|
var v = panel.gameObject.AddComponent<VerticalLayoutGroup>();
|
|
v.childControlHeight = v.childControlWidth = true;
|
|
v.childForceExpandHeight = false;
|
|
|
|
if (!string.IsNullOrEmpty(title))
|
|
Ui.Label(body, title, 56, Ui.TextColor).fontStyle = FontStyle.Bold;
|
|
message = Ui.Label(body, text ?? "", 40, Ui.Muted);
|
|
message.gameObject.SetActive(!string.IsNullOrEmpty(text));
|
|
open.Add(this);
|
|
}
|
|
|
|
public Button AddButton(string label, Action onClick, bool primary = false)
|
|
{
|
|
if (buttons == null)
|
|
{
|
|
buttons = Ui.Row(body, 24);
|
|
Ui.Height(buttons, 120);
|
|
}
|
|
var b = Ui.Button(buttons, label, () => onClick?.Invoke(), 44, primary);
|
|
return b;
|
|
}
|
|
|
|
// 按鈕:關閉對話框後執行
|
|
public Button AddClose(string label, Action then = null, bool primary = false)
|
|
=> AddButton(label, () => { Close(); then?.Invoke(); }, primary);
|
|
|
|
public bool IsOpen => root != null;
|
|
|
|
public void Close()
|
|
{
|
|
if (root == null) return;
|
|
open.Remove(this);
|
|
UnityEngine.Object.Destroy(root.gameObject);
|
|
Closed?.Invoke();
|
|
}
|
|
|
|
// 返回鍵:關閉最上層可取消的對話框;有關閉任何對話框時回傳 true
|
|
public static bool HandleBack()
|
|
{
|
|
if (open.Count == 0) return false;
|
|
var top = open[open.Count - 1];
|
|
if (top.cancellable) top.Close();
|
|
return true;
|
|
}
|
|
|
|
public static void CloseAll()
|
|
{
|
|
for (int i = open.Count - 1; i >= 0; i--) open[i].Close();
|
|
}
|
|
}
|