killapp/Assets/Scripts/UI/Pages/SelfPage/AutoLockSettingPage.cs

101 lines
4.0 KiB
C#
Raw Permalink Normal View History

using System.Collections;
using System.Collections.Generic;
using Kill.Managers;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Pages
{
/// <summary>
/// 自动锁定时间设置页
/// <para>点击选项立即生效并关闭页面,无需二次确认</para>
/// <para>UI 需挂载 backButton(返回按钮) 与 lockTimeButtons(选项按钮数组,按顺序对应 lockTimeOptions)</para>
/// </summary>
public class AutoLockSettingPage : MonoBehaviour
{
/// <summary>可选的自动锁定时间(秒),按按钮顺序一一对应;负数表示永不锁定</summary>
public int[] lockTimeOptions = new int[] { 180, 300, 600,1800, -1 };
/// <summary>选项按钮(数量需与 lockTimeOptions 一致)</summary>
public Button[] lockTimeButtons;
/// <summary>返回按钮(可选;UI 上有则挂载)</summary>
public Button backButton;
/// <summary>当前选中的索引</summary>
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);
}
}
/// <summary>选中某个锁定时间保存设置更新UI选中状态不退出页面</summary>
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<SelfPage>().OnSafetySettingBack);
Destroy(gameObject);
}
}
}