Files
Rhyculator/Assets/Script/not using/GridManager_MainScene.cs
T
2026-10-24 11:55:22 +08:00

167 lines
5.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
public class GridManager_MainScene : MonoBehaviour
{
public GameObject cellPrefab;
public int rows = 6;
public int columns = 5;
public GridColor[,] gridCells;
// Hold 參數
private bool isHolding = false;
private float holdTimer = 0f;
private float holdTarget = 1f;
private bool holdingCell = false;
private bool holdTriggered = false;
private int holdX, holdY;
public HoldHintSpawner holdProgressManager;
public string settingSceneName = "SettingScene";
public string accountSceneName = "AccountScene";
void Start()
{
float baseWidth = 1080f;
float cellBase = 180f;
float spaceBase = 20f;
float heightBase = 90f;
float scale = Screen.width / baseWidth;
float cellSize = cellBase * scale;
float spacing = spaceBase * scale;
float heightOffset = heightBase * scale;
gridCells = new GridColor[columns, rows];
RectTransform rt = GetComponent<RectTransform>();
if (rt == null) rt = gameObject.AddComponent<RectTransform>();
rt.anchorMin = new Vector2(0.5f, 0f);
rt.anchorMax = new Vector2(0.5f, 0f);
rt.pivot = new Vector2(0.5f, 0f);
float totalWidth = columns * cellSize + (columns - 1) * spacing;
float startX = -totalWidth / 2f;
float startY = heightOffset;
for (int i = 0; i < rows * columns; i++)
{
GameObject cellObj = Instantiate(cellPrefab, transform);
int x = i % columns;
int y = i / columns;
RectTransform cellRT = cellObj.GetComponent<RectTransform>();
cellRT.anchorMin = new Vector2(0f, 0f);
cellRT.anchorMax = new Vector2(0f, 0f);
cellRT.pivot = new Vector2(0f, 0f);
cellRT.sizeDelta = new Vector2(cellSize, cellSize);
float posX = startX + x * (cellSize + spacing);
float posY = startY + y * (cellSize + spacing);
cellRT.anchoredPosition = new Vector2(posX, posY);
GridColor cellColor = cellObj.GetComponent<GridColor>();
cellColor.cellX = x;
cellColor.cellY = y;
gridCells[x, y] = cellColor;
}
}
void Update()
{
DetectTouch();
// 按住 → 開始倒數
if (isHolding)
{
holdTimer += Time.deltaTime;
if (!holdTriggered && holdTimer >= holdTarget)
{
holdTriggered = true;
Debug.Log("成功");
}
}
}
void DetectTouch()
{
if (Touchscreen.current == null) return;
foreach (var touch in Touchscreen.current.touches)
{
UnityEngine.InputSystem.TouchPhase phase = touch.phase.ReadValue();
Vector2 touchPos = touch.position.ReadValue();
// ------------------------------------
// 若正在長按,不再判斷是否還在格子內
// ------------------------------------
if (holdingCell)
{
if (phase == UnityEngine.InputSystem.TouchPhase.Ended || phase == UnityEngine.InputSystem.TouchPhase.Canceled)
{
// 放開 → 結束
isHolding = false;
holdingCell = false;
holdTimer = 0f;
holdTriggered = false;
holdProgressManager.StopProgressAt(holdX, holdY);
}
return; // 已經在長按 → 不再找其他格子
}
// -------------------------------------------------------
// 如果沒有在 holding,這裡才會尋找目前手指所在格子
// -------------------------------------------------------
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < columns; x++)
{
var cell = gridCells[x, y];
if (RectTransformUtility.RectangleContainsScreenPoint(
cell.GetComponent<RectTransform>(), touchPos))
{
cell.FlashWhite();
// =============== 原本功能 ===============
if (phase == UnityEngine.InputSystem.TouchPhase.Began)
{
if (x == 1 && y == 5) SceneManager.LoadScene(accountSceneName);
if (x == 2 && y == 5) SceneManager.LoadScene(settingSceneName);
if (x == 3 && y == 5) Application.OpenURL("https://sthkm3.org");
}
// =============== Hold 觸發格子 (2,2) ===============
if (x == 2 && y == 2 && phase == UnityEngine.InputSystem.TouchPhase.Began)
{
// 開始長按序列
isHolding = true;
holdingCell = true;
holdTriggered = false;
holdTimer = 0f;
holdX = x;
holdY = y;
holdProgressManager.StartProgressAt(x, y, holdTarget);
}
}
}
}
}
}
}