-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIButton.cs
More file actions
308 lines (261 loc) · 6.36 KB
/
UIButton.cs
File metadata and controls
308 lines (261 loc) · 6.36 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Similar to UIButtonColor, but adds a 'disabled' state based on whether the collider is enabled or not.
/// </summary>
[AddComponentMenu("NGUI/Interaction/Button")]
public class UIButton : UIButtonColor
{
/// <summary>
/// Current button that sent out the onClick event.
/// </summary>
static public UIButton current;
/// <summary>
/// Whether the button will highlight when you drag something over it.
/// </summary>
public bool dragHighlight = false;
/// <summary>
/// Name of the hover state sprite.
/// </summary>
public string hoverSprite;
/// <summary>
/// Name of the pressed sprite.
/// </summary>
public string pressedSprite;
/// <summary>
/// Name of the disabled sprite.
/// </summary>
public string disabledSprite;
/// <summary>
/// Name of the hover state sprite.
/// </summary>
public UnityEngine.Sprite hoverSprite2D;
/// <summary>
/// Name of the pressed sprite.
/// </summary>
public UnityEngine.Sprite pressedSprite2D;
/// <summary>
/// Name of the disabled sprite.
/// </summary>
public UnityEngine.Sprite disabledSprite2D;
/// <summary>
/// Whether the sprite changes will elicit a call to MakePixelPerfect() or not.
/// </summary>
public bool pixelSnap = false;
/// <summary>
/// Click event listener.
/// </summary>
public List<EventDelegate> onClick = new List<EventDelegate>();
// Cached value
[System.NonSerialized] UISprite mSprite;
[System.NonSerialized] UI2DSprite mSprite2D;
[System.NonSerialized] string mNormalSprite;
[System.NonSerialized] UnityEngine.Sprite mNormalSprite2D;
/// <summary>
/// Whether the button should be enabled.
/// </summary>
public override bool isEnabled
{
get
{
if (!enabled) return false;
Collider col = collider;
if (col && col.enabled) return true;
Collider2D c2d = GetComponent<Collider2D>();
return (c2d && c2d.enabled);
}
set
{
if (isEnabled != value)
{
Collider col = collider;
if (col != null)
{
col.enabled = value;
SetState(value ? State.Normal : State.Disabled, false);
}
else
{
Collider2D c2d = GetComponent<Collider2D>();
if (c2d != null)
{
c2d.enabled = value;
SetState(value ? State.Normal : State.Disabled, false);
}
else enabled = value;
}
}
}
}
/// <summary>
/// Convenience function that changes the normal sprite.
/// </summary>
public string normalSprite
{
get
{
if (!mInitDone) OnInit();
return mNormalSprite;
}
set
{
if (mSprite != null && !string.IsNullOrEmpty(mNormalSprite) && mNormalSprite == mSprite.spriteName)
{
mNormalSprite = value;
SetSprite(value);
NGUITools.SetDirty(mSprite);
}
else
{
mNormalSprite = value;
if (mState == State.Normal) SetSprite(value);
}
}
}
/// <summary>
/// Convenience function that changes the normal sprite.
/// </summary>
public UnityEngine.Sprite normalSprite2D
{
get
{
if (!mInitDone) OnInit();
return mNormalSprite2D;
}
set
{
if (mSprite2D != null && mNormalSprite2D == mSprite2D.sprite2D)
{
mNormalSprite2D = value;
SetSprite(value);
NGUITools.SetDirty(mSprite);
}
else
{
mNormalSprite2D = value;
if (mState == State.Normal) SetSprite(value);
}
}
}
/// <summary>
/// Cache the sprite we'll be working with.
/// </summary>
protected override void OnInit ()
{
base.OnInit();
mSprite = (mWidget as UISprite);
mSprite2D = (mWidget as UI2DSprite);
if (mSprite != null) mNormalSprite = mSprite.spriteName;
if (mSprite2D != null) mNormalSprite2D = mSprite2D.sprite2D;
}
/// <summary>
/// Set the initial state.
/// </summary>
protected override void OnEnable ()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
mInitDone = false;
return;
}
#endif
if (isEnabled)
{
if (mInitDone)
{
if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
{
OnHover(UICamera.selectedObject == gameObject);
}
else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse)
{
OnHover(UICamera.hoveredObject == gameObject);
}
else SetState(State.Normal, false);
}
}
else SetState(State.Disabled, true);
}
/// <summary>
/// Drag over state logic is a bit different for the button.
/// </summary>
protected override void OnDragOver ()
{
if (isEnabled && (dragHighlight || UICamera.currentTouch.pressed == gameObject))
base.OnDragOver();
}
/// <summary>
/// Drag out state logic is a bit different for the button.
/// </summary>
protected override void OnDragOut ()
{
if (isEnabled && (dragHighlight || UICamera.currentTouch.pressed == gameObject))
base.OnDragOut();
}
/// <summary>
/// Call the listener function.
/// </summary>
protected virtual void OnClick ()
{
if (current == null && isEnabled)
{
current = this;
EventDelegate.Execute(onClick);
current = null;
}
}
/// <summary>
/// Change the visual state.
/// </summary>
public override void SetState (State state, bool immediate)
{
base.SetState(state, immediate);
if (mSprite != null)
{
switch (state)
{
case State.Normal: SetSprite(mNormalSprite); break;
case State.Hover: SetSprite(hoverSprite); break;
case State.Pressed: SetSprite(pressedSprite); break;
case State.Disabled: SetSprite(disabledSprite); break;
}
}
else if (mSprite2D != null)
{
switch (state)
{
case State.Normal: SetSprite(mNormalSprite2D); break;
case State.Hover: SetSprite(hoverSprite2D); break;
case State.Pressed: SetSprite(pressedSprite2D); break;
case State.Disabled: SetSprite(disabledSprite2D); break;
}
}
}
/// <summary>
/// Convenience function that changes the sprite.
/// </summary>
protected void SetSprite (string sp)
{
if (mSprite != null && !string.IsNullOrEmpty(sp) && mSprite.spriteName != sp)
{
mSprite.spriteName = sp;
if (pixelSnap) mSprite.MakePixelPerfect();
}
}
/// <summary>
/// Convenience function that changes the sprite.
/// </summary>
protected void SetSprite (UnityEngine.Sprite sp)
{
if (sp != null && mSprite2D != null && mSprite2D.sprite2D != sp)
{
mSprite2D.sprite2D = sp;
if (pixelSnap) mSprite2D.MakePixelPerfect();
}
}
}