- 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]>
203 lines
7.8 KiB
C#
203 lines
7.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem.EnhancedTouch;
|
|
using UnityEngine.UI;
|
|
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
|
|
|
|
// 設定:延遲校正、曲包資料夾位置、背景影片
|
|
// 延遲校正:播放節拍聲,玩家只聽聲音跟著點擊;取偏差中位數存成 AudioOffsetMs
|
|
public class SettingsPage : Page, ILibraryListener
|
|
{
|
|
private const double Bpm = 100;
|
|
private const int BeatCount = 16;
|
|
private const int WarmupBeats = 4;
|
|
private const double LeadIn = 1.0;
|
|
private double Interval => 60.0 / Bpm;
|
|
|
|
public override bool UsesGrid => false;
|
|
|
|
private Text offsetText, statusText, beatText, storageText;
|
|
private Button startButton, accessButton, videoButton;
|
|
private AudioClip clickTrack;
|
|
private bool running, hadAccess;
|
|
private readonly List<double> deltas = new List<double>();
|
|
private readonly HashSet<int> ignoredTouches = new HashSet<int>();
|
|
|
|
public override void Enter()
|
|
{
|
|
App.Music.Stop();
|
|
SongClock.Stop();
|
|
|
|
var back = Ui.Button(Top, "返回", () => App.Go(new MainPage()), 40);
|
|
Ui.Place((RectTransform)back.transform, new Vector2(0, 1), new Vector2(40, -30), new Vector2(200, 100));
|
|
var title = Ui.Label(Top, "設定", 64, Color.white);
|
|
Ui.Place(title.rectTransform, new Vector2(0.5f, 1), new Vector2(0, -30), new Vector2(600, 100));
|
|
|
|
var holder = Ui.Stretch(Ui.Rect(Top, "Body"), 40, 0, 40, 160);
|
|
var col = Ui.Scroll(holder, 26, 10);
|
|
|
|
Section(col, "延遲校正");
|
|
Ui.Label(col, "戴上平常玩的耳機,按「開始」後只聽聲音、跟著節拍點擊螢幕任意處\n前 4 拍為暖身不計分", 36, Ui.Muted);
|
|
offsetText = Ui.Label(col, "", 52, Ui.Accent);
|
|
beatText = Ui.Height(Ui.Label(col, "", 96, Color.white), 120);
|
|
statusText = Ui.Label(col, "", 36, Ui.Muted);
|
|
startButton = Ui.Height(Ui.Button(col, "開始", StartCalibration, 48, primary: true), 120);
|
|
var row = Ui.Height(Ui.Row(col), 110);
|
|
Ui.Button(row, "-5 ms", () => SetOffset(Offset - 5), 40);
|
|
Ui.Button(row, "重設", () => SetOffset(0), 40);
|
|
Ui.Button(row, "+5 ms", () => SetOffset(Offset + 5), 40);
|
|
Ui.Label(col, "正值 = 聲音比畫面晚(藍牙耳機通常為正)", 32, Ui.Muted);
|
|
|
|
Section(col, "曲包資料夾");
|
|
storageText = Ui.Label(col, "", 36, Ui.Muted);
|
|
accessButton = Ui.Height(Ui.Button(col, "允許「所有檔案存取權」", PackStorage.RequestSharedAccess, 42), 110);
|
|
Ui.Height(Ui.Button(col, "重新掃描", Library.Rescan, 42), 110);
|
|
|
|
Section(col, "畫面");
|
|
videoButton = Ui.Height(Ui.Button(col, "", ToggleVideo, 42), 110);
|
|
|
|
Ui.Label(col, $"\nRhyculator Alpha {Application.version}", 30, Ui.Muted);
|
|
|
|
EnhancedTouchSupport.Enable();
|
|
clickTrack = BuildClickTrack();
|
|
hadAccess = PackStorage.HasSharedAccess();
|
|
RefreshOffset();
|
|
LibraryChanged();
|
|
RefreshVideo();
|
|
}
|
|
|
|
static void Section(Transform parent, string name)
|
|
{
|
|
var t = Ui.Label(parent, name, 48, Color.white, TextAnchor.MiddleLeft);
|
|
t.fontStyle = FontStyle.Bold;
|
|
Ui.Height(t, 90);
|
|
}
|
|
|
|
public void LibraryChanged()
|
|
{
|
|
bool shared = PackStorage.HasSharedAccess();
|
|
string where = ChoosePage.DisplayPath(PackStorage.PackageDir);
|
|
storageText.text = $"<color=#FF66C4>{where}</color>\n目前 {Library.Songs.Count} 個曲包 · " +
|
|
(shared ? "已允許所有檔案存取權" : PackStorage.SupportsShared ? "未授權:曲包放在 app 自己的資料夾" : "");
|
|
accessButton.gameObject.SetActive(PackStorage.SupportsShared && !shared);
|
|
}
|
|
|
|
// ---------- 背景影片 ----------
|
|
|
|
void ToggleVideo()
|
|
{
|
|
Prefs.SetBool(Prefs.VideoBackground, !Prefs.GetBool(Prefs.VideoBackground, true));
|
|
RefreshVideo();
|
|
}
|
|
|
|
void RefreshVideo() => Ui.SetButtonText(videoButton, "曲包背景影片:" + (Prefs.GetBool(Prefs.VideoBackground, true) ? "開" : "關"));
|
|
|
|
// ---------- 延遲校正 ----------
|
|
|
|
static int Offset => PlayerPrefs.GetInt(Prefs.AudioOffsetMs, 0);
|
|
|
|
public override void Tick(List<GridTouch> touches)
|
|
{
|
|
// 從系統授權畫面回來:位置改變
|
|
bool access = PackStorage.HasSharedAccess();
|
|
if (access != hadAccess)
|
|
{
|
|
hadAccess = access;
|
|
Library.Rescan();
|
|
LibraryChanged();
|
|
}
|
|
|
|
if (!running) return;
|
|
double now = SongClock.Now;
|
|
int beat = (int)System.Math.Floor((now - LeadIn) / Interval) + 1;
|
|
beatText.text = beat <= 0 ? "準備…" : $"{Mathf.Min(beat, BeatCount)} / {BeatCount}";
|
|
|
|
double real = Time.realtimeSinceStartupAsDouble;
|
|
foreach (var t in Touch.activeTouches)
|
|
{
|
|
if (t.phase != UnityEngine.InputSystem.TouchPhase.Began || ignoredTouches.Contains(t.touchId)) continue;
|
|
double age = System.Math.Min(0.1, System.Math.Max(0, real - t.startTime));
|
|
if (CalibrationMath.TryBeatDelta(now - age, LeadIn, Interval, BeatCount, WarmupBeats, out double d))
|
|
deltas.Add(d);
|
|
}
|
|
|
|
if (now > LeadIn + (BeatCount - 0.5) * Interval + 0.3) Finish();
|
|
}
|
|
|
|
void StartCalibration()
|
|
{
|
|
deltas.Clear();
|
|
ignoredTouches.Clear();
|
|
// 開始前已按著的手指(按下「開始」的那一下)不計入
|
|
foreach (var t in Touch.activeTouches) ignoredTouches.Add(t.touchId);
|
|
|
|
double dspStart = AudioSettings.dspTime + 0.2;
|
|
App.Music.clip = clickTrack;
|
|
App.Music.loop = false;
|
|
App.Music.PlayScheduled(dspStart);
|
|
SongClock.Begin(dspStart, applyOffset: false);
|
|
|
|
running = true;
|
|
startButton.interactable = false;
|
|
statusText.text = "閉上眼睛,跟著聲音點擊螢幕任意處";
|
|
}
|
|
|
|
void Finish()
|
|
{
|
|
running = false;
|
|
SongClock.Stop();
|
|
App.Music.Stop();
|
|
startButton.interactable = true;
|
|
beatText.text = "";
|
|
|
|
int needed = (BeatCount - WarmupBeats) / 2;
|
|
if (deltas.Count < needed)
|
|
{
|
|
statusText.text = $"有效點擊太少({deltas.Count} 次),請再試一次";
|
|
return;
|
|
}
|
|
int ms = (int)System.Math.Round(CalibrationMath.Median(deltas) * 1000);
|
|
SetOffset(ms);
|
|
statusText.text = $"已套用:{deltas.Count} 次有效點擊,中位數 {ms:+0;-0;0} ms";
|
|
}
|
|
|
|
void SetOffset(int ms)
|
|
{
|
|
PlayerPrefs.SetInt(Prefs.AudioOffsetMs, Mathf.Clamp(ms, -500, 500));
|
|
PlayerPrefs.Save();
|
|
RefreshOffset();
|
|
}
|
|
|
|
void RefreshOffset() => offsetText.text = $"目前延遲補償:{Offset:+0;-0;0} ms";
|
|
|
|
// 整段節拍聲(每四拍第一拍較高音)
|
|
AudioClip BuildClickTrack()
|
|
{
|
|
int rate = AudioSettings.outputSampleRate;
|
|
double length = LeadIn + BeatCount * Interval + 0.5;
|
|
var data = new float[(int)(rate * length)];
|
|
int clickLen = (int)(rate * 0.04);
|
|
for (int b = 0; b < BeatCount; b++)
|
|
{
|
|
int start = (int)((LeadIn + b * Interval) * rate);
|
|
double freq = b % 4 == 0 ? 1760 : 1320;
|
|
for (int i = 0; i < clickLen && start + i < data.Length; i++)
|
|
{
|
|
double env = System.Math.Exp(-i / (rate * 0.008));
|
|
data[start + i] = (float)(System.Math.Sin(2 * System.Math.PI * freq * i / rate) * env * 0.8);
|
|
}
|
|
}
|
|
var clip = AudioClip.Create("CalibrationClicks", data.Length, 1, rate, false);
|
|
clip.SetData(data, 0);
|
|
return clip;
|
|
}
|
|
|
|
public override void Exit()
|
|
{
|
|
if (running) SongClock.Stop();
|
|
App.Music.Stop();
|
|
if (App.Music.clip == clickTrack) App.Music.clip = null;
|
|
if (clickTrack != null) Object.Destroy(clickTrack);
|
|
}
|
|
}
|