Skip to content

Commit 798fc47

Browse files
committed
Add click sound for combobox/label/progressbar
1 parent 9e6d99a commit 798fc47

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Assets/Scripts/UI/GComboBox.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public class GComboBox : GComponent
2020
/// </summary>
2121
public GComponent dropdown;
2222

23+
/// <summary>
24+
/// Play sound when button is clicked.
25+
/// </summary>
26+
public NAudioClip sound;
27+
28+
/// <summary>
29+
/// Volume of the click sound. (0-1)
30+
/// </summary>
31+
public float soundVolumeScale;
32+
2333
protected GObject _titleObject;
2434
protected GObject _iconObject;
2535
protected GList _list;
@@ -47,6 +57,7 @@ public GComboBox()
4757
_items = new List<string>();
4858
_values = new List<string>();
4959
_popupDirection = PopupDirection.Auto;
60+
soundVolumeScale = 1;
5061
}
5162

5263
/// <summary>
@@ -440,6 +451,7 @@ override protected void ConstructExtension(ByteBuffer buffer)
440451
displayObject.onRollOut.Add(__rollout);
441452
displayObject.onTouchBegin.Add(__touchBegin);
442453
displayObject.onTouchEnd.Add(__touchEnd);
454+
displayObject.onClick.Add(__click);
443455
}
444456

445457
override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
@@ -500,6 +512,14 @@ override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
500512
iv = buffer.ReadShort();
501513
if (iv >= 0)
502514
_selectionController = parent.GetControllerAt(iv);
515+
516+
if (buffer.version >= 5)
517+
{
518+
str = buffer.ReadS();
519+
if (str != null)
520+
sound = UIPackage.GetItemAssetByURL(str) as NAudioClip;
521+
soundVolumeScale = buffer.ReadFloat();
522+
}
503523
}
504524

505525
public void UpdateDropdownList()
@@ -599,5 +619,11 @@ private void __touchEnd(EventContext context)
599619
SetCurrentState();
600620
}
601621
}
622+
623+
private void __click()
624+
{
625+
if (sound != null && sound.nativeClip != null)
626+
Stage.inst.PlayOneShotSound(sound.nativeClip, soundVolumeScale);
627+
}
602628
}
603629
}

Assets/Scripts/UI/GLabel.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
213213
else
214214
buffer.Skip(13);
215215
}
216+
217+
if (buffer.version >= 5)
218+
{
219+
string sound = buffer.ReadS();
220+
if (!string.IsNullOrEmpty(sound))
221+
{
222+
float volumeScale = buffer.ReadFloat();
223+
displayObject.onClick.Add(() =>
224+
{
225+
NAudioClip audioClip = UIPackage.GetItemAssetByURL(sound) as NAudioClip;
226+
if (audioClip != null && audioClip.nativeClip != null)
227+
Stage.inst.PlayOneShotSound(audioClip.nativeClip, volumeScale);
228+
});
229+
}
230+
else
231+
buffer.Skip(4);
232+
}
216233
}
217234
}
218235
}

Assets/Scripts/UI/GProgressBar.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,23 @@ override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
275275
if (buffer.version >= 2)
276276
_min = buffer.ReadInt();
277277

278+
if (buffer.version >= 5)
279+
{
280+
string sound = buffer.ReadS();
281+
if (!string.IsNullOrEmpty(sound))
282+
{
283+
float volumeScale = buffer.ReadFloat();
284+
displayObject.onClick.Add(() =>
285+
{
286+
NAudioClip audioClip = UIPackage.GetItemAssetByURL(sound) as NAudioClip;
287+
if (audioClip != null && audioClip.nativeClip != null)
288+
Stage.inst.PlayOneShotSound(audioClip.nativeClip, volumeScale);
289+
});
290+
}
291+
else
292+
buffer.Skip(4);
293+
}
294+
278295
Update(_value);
279296
}
280297

0 commit comments

Comments
 (0)