// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖 using System.Collections.Generic; using UnityEngine; using NBC; using Unity.VisualScripting; namespace NBF { public partial class SettingPanel : UIPanel { public override string UIPackName => "Main"; public override string UIResName => "SettingPanel"; private List tabList = new List(); private string _currentGroup = ""; protected override void OnInit() { base.OnInit(); IsShowCursor = true; IsDontBack = true; var groupNames = Settings.Instance.GetAllGroups(); foreach (var group in groupNames) { var tab = new TabListData { Name = group }; tabList.Add(tab); } Menu.OnTabChange += ChangeTab; List.onClickItem.Set(OnClickListItem); } protected override void OnShow() { Settings.Instance.LoadAllSettings(); InputManager.OnUICanceled += OnUICanceled; Menu.SetTabs(tabList); UseBottomMenu(); } private void ChangeTab(int index) { if (index < 0) return; Log.Info($"Change tab index={index}"); var tabListData = tabList[index]; _currentGroup = tabListData.Name; ResetSettingList(); } private void ResetSettingList() { TextTitle.text = Lan.Get(_currentGroup); List.RemoveChildrenToPool(); if (string.IsNullOrEmpty(_currentGroup)) return; var options = Settings.Instance.GetOptionsByGroup(_currentGroup); foreach (var option in options) { if (List.AddItemFromPool() is SettingItem item) { item.SetData(option); } } } private void UseBottomMenu() { BottomMenu.Use(this); } private void OnApplySettings() { var options = Settings.Instance.GetOptionsByGroup(_currentGroup); foreach (var option in options) { option.Apply(); } } private void OnResetSettings() { var options = Settings.Instance.GetOptionsByGroup(_currentGroup); foreach (var option in options) { option.Reset(); } } private void OnUICanceled(string action) { if (action == InputDef.UI.SubPrev) { if (List.GetChildAt(List.selectedIndex) is SettingItem item) { item.OnPrev(); } } else if (action == InputDef.UI.SubNext) { if (List.GetChildAt(List.selectedIndex) is SettingItem item) { item.OnNext(); } } else if (action == InputDef.UI.Up) { SetListSelected(List.selectedIndex - 1); } else if (action == InputDef.UI.Down) { if (List.selectedIndex < 0) { SetListSelected(0); } else { SetListSelected(List.selectedIndex + 1); } } else if (action == InputDef.UI.Back) { OnBack(); } else if (action == InputDef.UI.Enter) { OnApplySettings(); } } private void SetListSelected(int selectedIndex) { if (selectedIndex >= 0 && selectedIndex < List.numItems) { List.selectedIndex = selectedIndex; List.ScrollToView(List.selectedIndex); } OnClickListItem(); } private void OnClickListItem() { var index = List.selectedIndex; if (index < 0) { return; } var settingItem = List.GetChildAt(index); if (settingItem is SettingItem item) { Introduce.TextName.text = Lan.Get(item.Option.Name); Introduce.TextIntroduce.text = Lan.Get(item.Option.Name); // Introduce. } } private void OnBack() { if (Settings.Instance.HaveNotAppleSettings()) { MessageBox.Show("还有未保存的信息", (b) => { if (b) { Hide(); } }); } else { Hide(); } } protected override void OnHide() { base.OnHide(); InputManager.OnUICanceled -= OnUICanceled; } protected override void OnDestroy() { base.OnDestroy(); Menu.OnTabChange -= ChangeTab; } } }