// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖 using System; using System.Collections.Generic; using UnityEngine; using FairyGUI; using NBC; namespace NBF { public partial class CommonSubMenu : GComponent { public event Action OnTabChange; private void OnInited() { List.onClickItem.Add(OnClickItem); BtnPrev.onClick.Add(OnClickBtnPrev); BtnNext.onClick.Add(OnClickBtnNext); InputManager.OnUICanceled += OnUICanceled; } public override void Dispose() { InputManager.OnUICanceled -= OnUICanceled; base.Dispose(); } private void OnUICanceled(string action) { if (action == InputDef.UI.SubPrev) { OnClickBtnPrev(); } else if (action == InputDef.UI.SubNext) { OnClickBtnNext(); } } public void SetTabs(List subItems, int selectIndex = 0) { List.RemoveChildrenToPool(); var width = 0f; for (int i = 0; i < subItems.Count; i++) { var tabData = subItems[i]; var tabItem = List.AddItemFromPool().asButton; tabItem.title = Lan.Get(tabData.Name); width += tabItem.width; if (i > 0) { width += List.columnGap; } } Log.Info($"Set tab index={List.selectedIndex}"); List.selectedIndex = selectIndex; List.width = width; OnClickItem(); } private void OnClickItem() { OnTabChange?.Invoke(List.selectedIndex); } private void OnClickBtnPrev() { if (List.selectedIndex > 0) { List.selectedIndex -= 1; } else { List.selectedIndex = List.numItems - 1; } OnClickItem(); } private void OnClickBtnNext() { if (List.selectedIndex < List.numItems - 1) { List.selectedIndex += 1; } else { List.selectedIndex = 0; } OnClickItem(); } } }