HKMBFZ/Assets/Scripts/Szz_Scripts/UI/181/MenuPageManager.cs

116 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuPageManager : UIPageBtnEventBase
{
public List<Button> buttons = new List<Button>();
public List<GameObject> showPages=new List<GameObject>();
private int currentIndex = 0;
public Color selectedColor = Color.yellow;
public Color unselectedColor = Color.black;
private GameObject currectSelectPage;
public override void OnPageUpClick()
{
base.OnPageUpClick();
SelectPrevious();
}
public override void OnPageDownClick()
{
base.OnPageDownClick();
SelectNext();
}
public override void OnF5Click()
{
BackMain();
}
public override void OnEnterBtnClick()
{
base.OnEnterBtnClick();
///打开对应的界面
GetCurrentButton().onClick?.Invoke();
}
/// <summary>
/// 切换到下一个按钮
/// </summary>
public void SelectNext()
{
if (buttons.Count == 0) return;
currentIndex = (currentIndex + 1) % buttons.Count;
UpdateButtonOutlines();
}
/// <summary>
/// 切换到上一个按钮
/// </summary>
public void SelectPrevious()
{
if (buttons.Count == 0) return;
currentIndex--;
if (currentIndex < 0) currentIndex = buttons.Count - 1;
UpdateButtonOutlines();
}
public void ShowPage()
{
if (currectSelectPage != null)
currectSelectPage.SetActive(false);
currectSelectPage =showPages.Find(x => x.name == GetCurrentButton().name);
currectSelectPage.SetActive(true);
}
public void BackMain()
{
currectSelectPage.SetActive(false);
}
/// <summary>
/// 更新按钮的 Outline 颜色
/// </summary>
private void UpdateButtonOutlines()
{
for (int i = 0; i < buttons.Count; i++)
{
Outline outline = buttons[i].GetComponent<Outline>();
if (outline == null)
{
// 如果没有 Outline 组件则添加
outline = buttons[i].gameObject.AddComponent<Outline>();
}
outline.effectColor = (i == currentIndex) ? selectedColor : unselectedColor;
}
}
/// <summary>
/// 获取当前选中的按钮
/// </summary>
public Button GetCurrentButton()
{
if (buttons.Count == 0) return null;
return buttons[currentIndex];
}
/// <summary>
/// 设置当前选择的按钮索引
/// </summary>
public void SetCurrentIndex(int index)
{
if (index < 0 || index >= buttons.Count) return;
currentIndex = index;
UpdateButtonOutlines();
}
}