| //-------------------------------------------------------- | 
| //    [Author]:           玩个游戏 | 
| //    [  Date ]:           Tuesday, October 31, 2017 | 
| //-------------------------------------------------------- | 
| using UnityEngine; | 
| using System.Collections; | 
| using System.Collections.Generic; | 
|   | 
| using System; | 
| using Cysharp.Threading.Tasks; | 
|   | 
| /// <summary> | 
| /// 按钮组管理器,负责管理GroupButtonEx组件的组关系和状态切换 | 
| /// 暂用于预制体中的设置,如果是动态增删按钮需重测试逻辑 | 
| /// </summary> | 
| public class GroupButtonExManager : MonoBehaviour | 
| { | 
|     // 按钮组列表 | 
|     private List<GroupButtonEx> m_Buttons = new List<GroupButtonEx>(); | 
|   | 
|     [SerializeField] Color m_SelectedTextColor = UIHelper.GetUIColor(TextColType.titleSelectColor); // 选中状态文字颜色 | 
|     public Color selectedTextColor { | 
|         get { return m_SelectedTextColor; } | 
|         set {  | 
|             m_SelectedTextColor = value; | 
|         } | 
|     } | 
|      | 
|     [SerializeField] Color m_NormalTextColor = UIHelper.GetUIColor(TextColType.titleUnSelectColor); // 未选中状态文字颜色 | 
|     public Color normalTextColor { | 
|         get { return m_NormalTextColor; } | 
|         set {  | 
|             m_NormalTextColor = value; | 
|         } | 
|     } | 
|   | 
|   | 
|     void OnEnable() | 
|     { | 
|         ExecuteNextFrame(); | 
|     } | 
|   | 
|     protected async void ExecuteNextFrame() | 
|     { | 
|         await UniTask.DelayFrame(1); | 
|         UpdateAllButtonsState(); | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 将按钮添加到组 | 
|     /// </summary> | 
|     /// <param name="button">要添加的按钮</param> | 
|     public void AddButton(GroupButtonEx button) | 
|     { | 
|         if (button == null) | 
|             return; | 
|   | 
|         // 如果按钮不在组中,添加到组 | 
|         if (!m_Buttons.Contains(button)) | 
|         { | 
|             m_Buttons.Add(button); | 
|         } | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 从组中移除按钮 | 
|     /// </summary> | 
|     /// <param name="button">要移除的按钮</param> | 
|     public void RemoveButton(GroupButtonEx button) | 
|     { | 
|         if (button == null) | 
|             return; | 
|   | 
|         m_Buttons.Remove(button); | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 选中指定按钮,并取消其他按钮的选中状态 | 
|     /// </summary> | 
|     /// <param name="button">要选中的按钮</param> | 
|     public void SelectButton(GroupButtonEx button) | 
|     { | 
|         if (button == null) | 
|             return; | 
|   | 
|         button.state = TitleBtnState.Click; | 
|         // 取消其他按钮的选中状态 | 
|         foreach (var btn in m_Buttons) | 
|         { | 
|             if (btn != button) | 
|             { | 
|                 btn.state = TitleBtnState.Normal; | 
|             } | 
|         } | 
|   | 
|         UpdateAllButtonsState(); | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 获取组中的所有按钮 | 
|     /// </summary> | 
|     /// <returns>按钮列表</returns> | 
|     public List<GroupButtonEx> GetButtons() | 
|     { | 
|         return new List<GroupButtonEx>(m_Buttons); | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 获取组中当前选中的按钮 | 
|     /// </summary> | 
|     /// <returns>选中的按钮,如果没有则返回null</returns> | 
|     public GroupButtonEx GetSelectedButton() | 
|     { | 
|         foreach (var btn in m_Buttons) | 
|         { | 
|             if (btn.state == TitleBtnState.Click) | 
|                 return btn; | 
|         } | 
|   | 
|         return null; | 
|     } | 
|   | 
|     /// <summary> | 
|     /// 清除所有按钮 | 
|     /// </summary> | 
|     public void ClearButtons() | 
|     { | 
|         m_Buttons.Clear(); | 
|     } | 
|      | 
|     /// <summary> | 
|     /// 更新所有按钮的状态 | 
|     /// </summary> | 
|     private void UpdateAllButtonsState() | 
|     { | 
|         // SortBtns(); | 
|   | 
|         foreach (var btn in m_Buttons) | 
|         { | 
|             btn.UpdateButtonState(); | 
|         } | 
|     } | 
|   | 
|     bool sortyet = false; | 
|     public void SortBtns(bool forceSort = false) | 
|     { | 
|         if (m_Buttons.Count <= 0) | 
|             return; | 
|              | 
|         if (sortyet && !forceSort) | 
|             return; | 
|   | 
|         m_Buttons.Sort((a, b) => { return a.transform.GetSiblingIndex() - b.transform.GetSiblingIndex(); }); | 
|         sortyet = true; | 
|     } | 
|      | 
|     /// <summary> | 
|     /// 获取按钮状态对应的文本颜色 | 
|     /// </summary> | 
|     /// <param name="state">按钮状态</param> | 
|     /// <returns>对应的文本颜色</returns> | 
|     public Color GetTextColorForState(TitleBtnState state) | 
|     { | 
|         return state == TitleBtnState.Click ? m_SelectedTextColor : m_NormalTextColor; | 
|     } | 
| } |