using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.UI; using UnityEngine.UI; // 一個畫面(主選單、選歌、遊玩…);同時只有一個,畫面內容放在 App.Top,格子是共用的 public abstract class Page { protected App App => App.Instance; protected Grid Grid => App.Instance.Grid; protected RectTransform Top => App.Instance.Top; public virtual bool UsesGrid => true; public virtual void Enter() { } public virtual void Exit() { } // 每幀:處理觸控與邏輯(在格子判定過期之前) public virtual void Tick(List touches) { } // 每幀:格子更新後(生成新音符) public virtual void LateTick() { } // 返回鍵(對話框都關閉時) public virtual void Back() => App.Go(new MainPage()); } // 整個遊戲只有一個空場景:啟動時由這裡建立畫布、格子與第一個畫面 // 版面(畫布寬固定 1080): // ┌ 安全區域 ┐ // │ Top │ 畫面內容(封面、標題、分數…),高度 = 剩下的空間 // │ Grid 5x6 │ 靠下;畫面太扁時縮小 // │ Footer │ UID / 版本 public class App : MonoBehaviour { public static App Instance { get; private set; } public Grid Grid { get; private set; } public RectTransform Top { get; private set; } public RectTransform Overlay { get; private set; } // 對話框、結算 public RawImage Background { get; private set; } // 曲包影片 public AudioSource Music { get; private set; } public GridInput Input { get; private set; } public Page Current { get; private set; } private const float FooterHeight = 90f; private const float GridGap = 30f; private const float MinTop = 460f; private RectTransform safe; private Text uidText; private Rect lastSafe; private Vector2Int lastSize; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void Boot() { // 幀率上限 = 螢幕支援的最高更新率(Android 上 targetFrameRate = -1 代表 30fps) QualitySettings.vSyncCount = 0; double max = UnityEngine.Screen.currentResolution.refreshRateRatio.value; foreach (var r in UnityEngine.Screen.resolutions) if (r.refreshRateRatio.value > max) max = r.refreshRateRatio.value; Application.targetFrameRate = Mathf.Max(60, Mathf.RoundToInt((float)max)); UnityEngine.Screen.sleepTimeout = SleepTimeout.NeverSleep; PackStorage.Init(); var go = new GameObject("App"); DontDestroyOnLoad(go); go.AddComponent(); } void Awake() { Instance = this; Music = gameObject.AddComponent(); Music.playOnAwake = false; BuildEventSystem(); BuildCanvas(); Input = new GridInput(Grid); Library.Create(); Library.Changed += () => (Current as ILibraryListener)?.LibraryChanged(); Library.DownloadRequested += code => { if (!(Current is PlayPage) && !(Current is IntroPage)) Go(new ChoosePage(code)); else pendingDownload = code; }; } void Start() { Go(new IntroPage()); } void BuildEventSystem() { var es = new GameObject("EventSystem", typeof(EventSystem), typeof(InputSystemUIInputModule)); DontDestroyOnLoad(es); } void BuildCanvas() { var root = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); DontDestroyOnLoad(root); var canvas = root.GetComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; var scaler = root.GetComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(1080, 1920); scaler.matchWidthOrHeight = 0f; // 黑底 + 影片背景 var bg = Ui.Image(root.transform, null, Ui.Bg); Ui.Stretch(bg.rectTransform); Background = Ui.Rect(root.transform, "Video").gameObject.AddComponent(); Background.raycastTarget = false; Background.enabled = false; Ui.Stretch(Background.rectTransform); safe = Ui.Rect(root.transform, "Safe"); Top = Ui.Rect(safe, "Top"); var gridHolder = Ui.Rect(safe, "GridHolder"); Ui.Stretch(gridHolder, 0, FooterHeight); Grid = Grid.Create(gridHolder, Ui.Font); var footer = Ui.Rect(safe, "Footer"); footer.anchorMin = Vector2.zero; footer.anchorMax = new Vector2(1, 0); footer.pivot = new Vector2(0.5f, 0); footer.sizeDelta = new Vector2(0, FooterHeight); uidText = Ui.Label(footer, "", 30, Ui.Muted, TextAnchor.MiddleLeft); Ui.Stretch(uidText.rectTransform, 60, 0, 60, 0); var ver = Ui.Label(footer, "version: Alpha " + Application.version, 30, Ui.Muted, TextAnchor.MiddleRight); Ui.Stretch(ver.rectTransform, 60, 0, 60, 0); Overlay = Ui.Stretch(Ui.Rect(root.transform, "Overlay")); } // 安全區域(瀏海、導覽列)與格子大小 void Layout() { var size = new Vector2Int(UnityEngine.Screen.width, UnityEngine.Screen.height); Rect sa = UnityEngine.Screen.safeArea; if (sa == lastSafe && size == lastSize) return; lastSafe = sa; lastSize = size; safe.anchorMin = new Vector2(sa.xMin / size.x, sa.yMin / size.y); safe.anchorMax = new Vector2(sa.xMax / size.x, sa.yMax / size.y); safe.offsetMin = safe.offsetMax = Vector2.zero; ApplyGridLayout(); } void ApplyGridLayout() { float unitsPerPixel = 1080f / Mathf.Max(1, UnityEngine.Screen.width); float safeH = lastSafe.height * unitsPerPixel; bool grid = Current == null || Current.UsesGrid; Grid.gameObject.SetActive(grid); float gridScale = Mathf.Clamp((safeH - FooterHeight - GridGap - MinTop) / Grid.Height, 0.6f, 1f); Grid.transform.localScale = Vector3.one * gridScale; float bottom = grid ? FooterHeight + Grid.Height * gridScale + GridGap : FooterHeight; Top.anchorMin = Vector2.zero; Top.anchorMax = Vector2.one; Top.offsetMin = new Vector2(0, bottom); Top.offsetMax = Vector2.zero; } private string pendingDownload; // 開場、遊玩中收到的下載連結,離開後處理 public void Go(Page page) { if (pendingDownload != null && page is MainPage or ChoosePage) { page = new ChoosePage(pendingDownload); pendingDownload = null; } Dialog.CloseAll(); Current?.Exit(); foreach (Transform child in Top) Destroy(child.gameObject); Grid.ClearAll(); Background.enabled = false; Input.IgnoreCurrentTouches(); Current = page; lastSize = Vector2Int.zero; // 重新套用版面(畫面可能不用格子) Layout(); RefreshFooter(); page.Enter(); } public void RefreshFooter() => uidText.text = "UID: " + Prefs.UidText(); void Update() { Layout(); // Android 的返回鍵:舊版 Input 一定會對應到 Escape;新版 Input System 在部分裝置收不到 var kb = Keyboard.current; bool back = UnityEngine.Input.GetKeyDown(KeyCode.Escape) || (kb != null && kb.escapeKey.wasPressedThisFrame); if (back && !Dialog.HandleBack()) Current?.Back(); var touches = Input.Poll(Dialog.AnyOpen); Current?.Tick(touches); if (Grid.isActiveAndEnabled) Grid.Tick(); Current?.LateTick(); } void OnApplicationPause(bool paused) { if (paused) (Current as IPausable)?.OnAppPause(); } void OnApplicationQuit() => PlayerPrefs.Save(); } // 曲包清單改變時要更新的畫面 public interface ILibraryListener { void LibraryChanged(); } // 切到背景時要暫停的畫面(遊玩) public interface IPausable { void OnAppPause(); }