134 lines
4.6 KiB
C#
134 lines
4.6 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);
|
||
|
||
// SelectBG 颜色:Passive=false 时恢复 prefab 上配置的原色,Passive=true 时强制灰
|
||
// 首次 Init 时缓存 prefab 原色,确保使用方可在 Inspector 自定义底色(音乐室等场景)
|
||
private Image _bgImage;
|
||
private Color _bgOrigColor;
|
||
private bool _bgColorCached;
|
||
|
||
private void EnsureBGColorCached()
|
||
{
|
||
if (_bgColorCached) return;
|
||
if (SelectBG != null)
|
||
{
|
||
_bgImage = SelectBG.GetComponent<Image>();
|
||
if (_bgImage != null) _bgOrigColor = _bgImage.color;
|
||
}
|
||
_bgColorCached = true;
|
||
}
|
||
|
||
public void Init(uint defaultIndex)
|
||
{
|
||
_selectedIndex = defaultIndex;
|
||
_firstShow = false;
|
||
|
||
// SelectBG 颜色按 Passive 切换
|
||
EnsureBGColorCached();
|
||
if (_bgImage != null)
|
||
_bgImage.color = Passive ? _gray : _bgOrigColor;
|
||
|
||
//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()
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|