forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompoundButtonSounds.cs
120 lines (100 loc) · 3.78 KB
/
CompoundButtonSounds.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using HoloToolkit.Unity;
using UnityEngine;
namespace HoloToolkit.Unity.Buttons
{
/// <summary>
/// A convenient way to play sounds in response to button actions / states
/// </summary>
[RequireComponent(typeof(CompoundButton))]
public class CompoundButtonSounds : ProfileButtonBase<ButtonSoundProfile>
{
const float MinTimeBetweenSameClip = 0.1f;
[SerializeField]
private AudioSource audioSource;
private static string lastClipName;
private static float lastClipTime;
private ButtonStateEnum lastState = ButtonStateEnum.Disabled;
private void Start ()
{
Button button = GetComponent<Button>();
button.OnButtonCanceled += OnButtonCanceled;
button.OnButtonHeld += OnButtonHeld;
button.OnButtonPressed += OnButtonPressed;
button.OnButtonReleased += OnButtonReleased;
button.StateChange += StateChange;
audioSource = GetComponent<AudioSource>();
}
private void StateChange(ButtonStateEnum newState)
{
// Don't play the same state multiple times
if (lastState == newState)
return;
lastState = newState;
// Don't play sounds for inactive buttons
if (!gameObject.activeSelf || !gameObject.activeInHierarchy)
return;
if (Profile == null)
{
Debug.LogError("Sound profile was null in button " + name);
return;
}
switch (newState)
{
case ButtonStateEnum.Observation:
PlayClip(Profile.ButtonObservation, Profile.ButtonObservationVolume);
break;
case ButtonStateEnum.ObservationTargeted:
PlayClip(Profile.ButtonObservationTargeted, Profile.ButtonObservationTargetedVolume);
break;
case ButtonStateEnum.Targeted:
PlayClip(Profile.ButtonTargeted, Profile.ButtonTargetedVolume);
break;
default:
break;
}
}
private void OnButtonCanceled(GameObject go)
{
PlayClip(Profile.ButtonCanceled, Profile.ButtonCanceledVolume);
}
private void OnButtonHeld(GameObject go)
{
PlayClip(Profile.ButtonHeld, Profile.ButtonHeldVolume);
}
private void OnButtonPressed(GameObject go)
{
PlayClip(Profile.ButtonPressed, Profile.ButtonPressedVolume);
}
private void OnButtonReleased (GameObject go)
{
PlayClip(Profile.ButtonReleased, Profile.ButtonReleasedVolume);
}
private void PlayClip (AudioClip clip, float volume)
{
if (clip != null)
{
// Don't play the clip if we're spamming it
if (clip.name == lastClipName && (Time.realtimeSinceStartup - lastClipTime) < MinTimeBetweenSameClip)
{
return;
}
lastClipName = clip.name;
lastClipTime = Time.realtimeSinceStartup;
if (audioSource != null)
{
audioSource.PlayOneShot(clip, volume);
}
else
{
AudioSource.PlayClipAtPoint(clip, transform.position, volume);
}
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(CompoundButtonSounds))]
public class CustomEditor : MRTKEditor { }
#endif
}
}