TH1/My project/Assets/Scripts/UI/ChooseUI/ChooseUISettingBarController.cs
2025-07-10 17:54:39 +08:00

120 lines
3.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using Logic.CrashSight;
public class ChooseUISettingBarController : MonoBehaviour
{
public string buttonGroupName = "ButtonGroup";
private Transform buttonGroup;
private List<Button> buttons = new List<Button>();
private Button selectedButton;
// 高亮与默认颜色
private readonly Color selectedColor = new Color32(0x00, 0x00, 0x00, 112); // #000000, alpha 112
private readonly Color normalColor = new Color32(0x00, 0x00, 0x00, 1); // #000000, alpha 1
void Start()
{
selectedButton = null;
buttonGroup = transform.Find(buttonGroupName);
if (buttonGroup == null)
{
LogSystem.LogError($"ButtonGroup '{buttonGroupName}' not found under {gameObject.name}");
return;
}
// 查找所有按钮并绑定点击事件
foreach (Transform child in buttonGroup)
{
Button btn = child.GetComponent<Button>();
if (btn != null)
{
buttons.Add(btn);
btn.onClick.AddListener(() => OnButtonClicked(btn));
Image bgImage = btn.transform.Find("BG")?.GetComponent<Image>();
if (bgImage != null)
{
bgImage.color = (btn == selectedButton) ? selectedColor : normalColor;
}
}
}
// 设置默认选项
if (buttons.Count > 0)
{
int deft = buttons.Count - 1;
//地图默认 18x18
if (gameObject.name == "MapSize" && deft >= 3)
deft = 3;
//玩家默认 8
if (gameObject.name == "EnemyCount" && deft >= 6)
deft = 6;
//难度默认 LUNATIC
if (gameObject.name == "Difficulty" && deft >= 3)
deft = 3;
//模式默认 征服模式
if (gameObject.name == "Mode" && deft >= 1)
deft = 1;
OnButtonClicked(buttons[deft],true);
}
}
private void OnButtonClicked(Button clickedButton, bool specialWay = false)
{
//mapSize目前设置为不可点击
if (gameObject.name == "MapSize" && !specialWay)
return;
selectedButton = clickedButton;
foreach (Button btn in buttons)
{
Image bgImage = btn.transform.Find("BG")?.GetComponent<Image>();
if (bgImage == null) continue;
//如果是选中的btn
if(btn == selectedButton )
{
//修改按钮颜色
bgImage.color = selectedColor;
}
//如果不是选中的btn
else
{
//修改按钮颜色
bgImage.color = normalColor;
}
}
//通知ChooseUI发生了一次选择,更新选择界面的显示
UIManager.Instance.GameUI.ChooseUI.SelectUpdate();
}
//获取选择选项的name
public string GetSelectedButtonName()
{
return selectedButton != null ? selectedButton.name : "";
}
//将一个name的选项选中
public bool SetSelectedByButtonName(string buttonName)
{
foreach (var but in buttons)
{
if (but.gameObject.name == buttonName)
{
OnButtonClicked(but,true);
return true;
}
}
return false;
}
}