// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖 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; var groupNames = Settings.Instance.GetAllGroups(); foreach (var group in groupNames) { var tab = new TabListData { Name = group }; tabList.Add(tab); } Menu.OnTabChange += ChangeTab; } protected override void OnShow() { Settings.Instance.LoadAllSettings(); InputManager.OnUICanceled += OnUICanceled; Menu.SetTabs(tabList); UseBottomMenu(); } private void ChangeTab(int index) { Log.Info($"Change tab index={index}"); var tabListData = tabList[index]; _currentGroup = tabListData.Name; // _currentSettingsObject = tabListData.SettingsObject; 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.OnBack += Hide; BottomMenu.Use(); } 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); } } } private void SetListSelected(int selectedIndex) { if (selectedIndex >= 0 && selectedIndex < List.numItems) { List.selectedIndex = selectedIndex; List.ScrollToView(List.selectedIndex); } } protected override void OnHide() { base.OnHide(); BottomMenu.OnBack -= Hide; BottomMenu.UnUse(); InputManager.OnUICanceled -= OnUICanceled; } protected override void OnDestroy() { base.OnDestroy(); Menu.OnTabChange -= ChangeTab; } } }