115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
namespace TH1_UI.View.Outside
|
||
{
|
||
public class UIOutsideSelectOptionGroupMono : MonoBehaviour
|
||
{
|
||
|
||
|
||
public List<Button> options = new List<Button>();
|
||
public RectTransform SelectBG;
|
||
public bool Passive;
|
||
public Action<uint> OnOptionClicked;
|
||
|
||
public uint SelectedIndex => _selectedIndex;
|
||
|
||
private uint _selectedIndex;
|
||
private bool _firstShow;
|
||
private Color _blue = new Color32(65,59,107,255);
|
||
private Color _gray = new Color32(128,128,128,128);
|
||
public void Init(uint defaultIndex)
|
||
{
|
||
_selectedIndex = defaultIndex;
|
||
_firstShow = false;
|
||
//设置selectBG的颜色
|
||
var imgBG = SelectBG.GetComponent<Image>();
|
||
if(imgBG != null)
|
||
imgBG.color = Passive ? _gray : Color.white;
|
||
|
||
//Debug.Log($"[UIOutsideSelectOptionGroupMono.Init] Passive={Passive}, name={gameObject.name}");
|
||
|
||
// 先设置所有按钮颜色(Passive模式下全灰色,否则蓝色)
|
||
for (int i = 0; i < options.Count; i++)
|
||
{
|
||
var text = options[i].gameObject.GetComponentInChildren<TextMeshProUGUI>(true);
|
||
if (text != null)
|
||
text.color = Passive ? Color.gray : _blue;
|
||
|
||
// 禁用按钮交互
|
||
options[i].interactable = !Passive;
|
||
|
||
}
|
||
|
||
for (int i = 0; i < options.Count; i++)
|
||
{
|
||
options[i].onClick.RemoveAllListeners();
|
||
uint index = (uint)i;
|
||
if(!Passive)
|
||
options[i].onClick.AddListener(() =>
|
||
{
|
||
Select(index);
|
||
OnOptionClicked?.Invoke((uint)index);
|
||
});
|
||
if (i == defaultIndex)
|
||
Select((uint)i);
|
||
}
|
||
}
|
||
|
||
public void OnShow()
|
||
{
|
||
Debug.Log($"[OnShow] _firstShow={_firstShow}, Passive={Passive}, name={gameObject.name}");
|
||
if (_firstShow) return;
|
||
_firstShow = true;
|
||
Select(_selectedIndex);
|
||
}
|
||
public string GetSelectedString()
|
||
{
|
||
if (options.Count <= _selectedIndex) return "";
|
||
return options[(int)_selectedIndex].gameObject.GetComponent<TextMeshProUGUI>()?.text;
|
||
}
|
||
|
||
|
||
public void Select(uint index)
|
||
{
|
||
if (index >= options.Count) return;
|
||
Debug.Log($"[Select] index={index}, Passive={Passive}, name={gameObject.name}");
|
||
for(int i = 0;i < options.Count; i++)
|
||
if (i == index)
|
||
{
|
||
LayoutRebuilder.ForceRebuildLayoutImmediate(this.GetComponent<RectTransform>());
|
||
var pos = options[i].gameObject.GetComponent<RectTransform>().anchoredPosition;
|
||
var text = options[i].gameObject.GetComponentInChildren<TextMeshProUGUI>(true);
|
||
if(text != null) text.color = Color.white;
|
||
SelectBG.anchoredPosition = pos;
|
||
}
|
||
else
|
||
{
|
||
var text = options[i].gameObject.GetComponentInChildren<TextMeshProUGUI>(true);
|
||
if (text != null)
|
||
{
|
||
text.color = Passive ? Color.gray : _blue;
|
||
Debug.Log($"[Select] Button {i} Text set to {(Passive ? "Gray" : "Blue")}, actual={text.color}");
|
||
}
|
||
// 禁用按钮交互
|
||
if (Passive)
|
||
options[i].interactable = false;
|
||
}
|
||
_selectedIndex = index;
|
||
}
|
||
void Start()
|
||
{
|
||
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|