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("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(); 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(); 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