hch
1 天以前 d21ab089026bfe22df1e5c36ef2d66554957c188
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//--------------------------------------------------------
//    [Author]:           玩个游戏
//    [  Date ]:           Tuesday, October 31, 2017
//--------------------------------------------------------
using UnityEngine;
using UnityEngine.EventSystems;
 
using System;
 
 
//关联按钮,其中一个亮起,其他按下,文字颜色对应变更
//数据更新通过SelectBtn更新 或者 GroupButtonExManager.SelectButton
//使用步骤 1.序列化GroupButtonEx类型的按钮,2:初始需设置SelectBtn()选中哪个按钮,其他和按钮使用方法一致
public class GroupButtonEx : ButtonEx
{
    [SerializeField] GroupButtonExManager m_Manager; // 按钮组管理器引用
    public GroupButtonExManager manager
    {
        get { return m_Manager; }
        set
        {
            if (m_Manager != value)
            {
                // 从旧管理器移除并添加到新管理器
                if (m_Manager != null)
                {
                    m_Manager.RemoveButton(this);
                }
 
                m_Manager = value;
 
                if (m_Manager != null)
                {
                    m_Manager.AddButton(this);
                }
            }
        }
    }
 
    [NonSerialized] TitleBtnState m_State = TitleBtnState.Normal;
    public TitleBtnState state
    {
        get { return m_State; }
        set
        {
            if (m_State != value)
            {
                m_State = value;
            }
        }
    }
 
    [SerializeField] ImageEx m_SelectIcon;
    public ImageEx selectIcon
    {
        get { return this.m_SelectIcon; }
        set { this.m_SelectIcon = value; }
    }
 
    [SerializeField] ImageEx m_UnSelectIcon;
    public ImageEx unSelectIcon
    {
        get { return this.m_UnSelectIcon; }
        set { this.m_UnSelectIcon = value; }
    }
 
    [SerializeField] TextEx m_Title;
    public TextEx title
    {
        get { return this.m_Title; }
        set { this.m_Title = value; }
    }
 
    [SerializeField] UIEffectPlayer m_SelectEffect;  //选中特效
    public UIEffectPlayer selectEffect
    {
        get { return m_SelectEffect; }
        set { m_SelectEffect = value; }
    }
 
 
    protected override void Awake()
    {
        base.Awake();
        // 确保已添加到管理器
        if (m_Manager != null)
        {
            m_Manager.AddButton(this);
        }
    }
 
    protected override void OnDestroy()
    {
        if (m_Manager != null)
        {
            m_Manager.RemoveButton(this);
        }
        base.OnDestroy();
    }
 
    public override void OnPointerClick(PointerEventData eventData)
    {
        base.OnPointerClick(eventData);
        SelectBtn();
    }
 
    // 选中当前按钮,只处理刷新显示
    public void SelectBtn(bool forceRefresh = false)
    {
        if (m_State == TitleBtnState.Click && !forceRefresh)
            return;
 
        // 通过管理器处理选中逻辑
        if (m_Manager != null)
        {
            m_Manager.SelectButton(this);
        }
 
    }
 
    // 更新按钮状态
    public void UpdateButtonState()
    {
        // 更新图标显示
        if (m_SelectIcon != null)
            m_SelectIcon.SetActive(m_State == TitleBtnState.Click);
 
        if (m_UnSelectIcon != null)
            m_UnSelectIcon.SetActive(m_State != TitleBtnState.Click);
 
        // 更新文字颜色
        if (m_Title != null && m_Manager != null)
        {
            m_Title.color = m_Manager.GetTextColorForState(m_State);
        }
 
        if (m_SelectEffect != null)
        {
            if (m_State == TitleBtnState.Click)
            {
                //选中,显示特效
                m_SelectEffect.Play();
            }
            else
            {
                m_SelectEffect.Stop();
            }
        }
    }
}