using System.Collections; using System.Collections.Generic; using Kill.Managers; using UnityEngine; using UnityEngine.UI; namespace Kill.UI.Pages { /// /// 自动锁定时间设置页 /// 点击选项立即生效并关闭页面,无需二次确认 /// UI 需挂载 backButton(返回按钮) 与 lockTimeButtons(选项按钮数组,按顺序对应 lockTimeOptions) /// public class AutoLockSettingPage : MonoBehaviour { /// 可选的自动锁定时间(秒),按按钮顺序一一对应;负数表示永不锁定 public int[] lockTimeOptions = new int[] { 180, 300, 600,1800, -1 }; /// 选项按钮(数量需与 lockTimeOptions 一致) public Button[] lockTimeButtons; /// 返回按钮(可选;UI 上有则挂载) public Button backButton; /// 当前选中的索引 private int currentIndex = -1; void Start() { // 注册返回事件(物理/手势返回键) UIManager.Instance.RegisterBackAction(OnBack); // 初始化按钮 int currentLockTime = DataManager.Instance.lockTime; for (int i = 0; i < lockTimeButtons.Length; i++) { int index = i; int optionValue = i < lockTimeOptions.Length ? lockTimeOptions[i] : 0; // 负数档映射为永不锁定 int compareValue = optionValue < 0 ? DataManager.NEVER_LOCK : optionValue; bool isCurrent = compareValue == currentLockTime; if (isCurrent) currentIndex = index; Button btn = lockTimeButtons[i]; btn.onClick.RemoveAllListeners(); btn.onClick.AddListener(() => OnLockTimeSelected(index)); // 更新按钮显示状态:选中项显示图标且不可点 btn.interactable = !isCurrent; Transform icon = btn.transform.Find("icon"); if (icon != null) icon.gameObject.SetActive(isCurrent); } if (backButton != null) { backButton.onClick.RemoveAllListeners(); backButton.onClick.AddListener(OnBack); } } /// 选中某个锁定时间:保存设置,更新UI选中状态,不退出页面 void OnLockTimeSelected(int index) { if (index < 0 || index >= lockTimeOptions.Length) return; if (index == currentIndex) return; int option = lockTimeOptions[index]; // 负数视为永不锁定档 int seconds = option < 0 ? DataManager.NEVER_LOCK : option; DataManager.Instance.SetLockTime(seconds); // 选择新锁定时间视为一次活跃操作,重置倒计时起点 DataManager.Instance.RecordUnlock(); // 更新选中索引与UI选中状态 int previousIndex = currentIndex; currentIndex = index; if (previousIndex >= 0 && previousIndex < lockTimeButtons.Length) { Button prevBtn = lockTimeButtons[previousIndex]; prevBtn.interactable = true; Transform prevIcon = prevBtn.transform.Find("icon"); if (prevIcon != null) prevIcon.gameObject.SetActive(false); } if (index >= 0 && index < lockTimeButtons.Length) { Button curBtn = lockTimeButtons[index]; curBtn.interactable = false; Transform curIcon = curBtn.transform.Find("icon"); if (curIcon != null) curIcon.gameObject.SetActive(true); } } public void OnBack() { // 将返回事件还原为 SelfPage 的 OnBack,再销毁自身 UIManager.Instance.RegisterBackAction(GetComponentInParent().OnSafetySettingBack); Destroy(gameObject); } } }