- 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]>
227 lines
8.0 KiB
C#
227 lines
8.0 KiB
C#
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<GridTouch> 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<App>();
|
||
}
|
||
|
||
void Awake()
|
||
{
|
||
Instance = this;
|
||
Music = gameObject.AddComponent<AudioSource>();
|
||
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>();
|
||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||
var scaler = root.GetComponent<CanvasScaler>();
|
||
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<RawImage>();
|
||
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();
|
||
}
|