AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CompoundButtonSounds.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using HoloToolkit.Unity;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.Buttons
8 {
12  [RequireComponent(typeof(CompoundButton))]
13  public class CompoundButtonSounds : ProfileButtonBase<ButtonSoundProfile>
14  {
15  const float MinTimeBetweenSameClip = 0.1f;
16 
17  [SerializeField]
18  private AudioSource audioSource;
19  private static string lastClipName;
20  private static float lastClipTime;
21  private ButtonStateEnum lastState = ButtonStateEnum.Disabled;
22 
23  private void Start ()
24  {
25  Button button = GetComponent<Button>();
26  button.OnButtonCanceled += OnButtonCanceled;
27  button.OnButtonHeld += OnButtonHeld;
28  button.OnButtonPressed += OnButtonPressed;
29  button.OnButtonReleased += OnButtonReleased;
30  button.StateChange += StateChange;
31 
32  audioSource = GetComponent<AudioSource>();
33  }
34 
35  private void StateChange(ButtonStateEnum newState)
36  {
37  // Don't play the same state multiple times
38  if (lastState == newState)
39  return;
40 
41  lastState = newState;
42 
43  // Don't play sounds for inactive buttons
44  if (!gameObject.activeSelf || !gameObject.activeInHierarchy)
45  return;
46 
47  if (Profile == null)
48  {
49  Debug.LogError("Sound profile was null in button " + name);
50  return;
51  }
52 
53  switch (newState)
54  {
55  case ButtonStateEnum.Observation:
56  PlayClip(Profile.ButtonObservation, Profile.ButtonObservationVolume);
57  break;
58 
59  case ButtonStateEnum.ObservationTargeted:
60  PlayClip(Profile.ButtonObservationTargeted, Profile.ButtonObservationTargetedVolume);
61  break;
62 
63  case ButtonStateEnum.Targeted:
64  PlayClip(Profile.ButtonTargeted, Profile.ButtonTargetedVolume);
65  break;
66 
67  default:
68  break;
69  }
70  }
71 
72  private void OnButtonCanceled(GameObject go)
73  {
74  PlayClip(Profile.ButtonCanceled, Profile.ButtonCanceledVolume);
75  }
76 
77  private void OnButtonHeld(GameObject go)
78  {
79  PlayClip(Profile.ButtonHeld, Profile.ButtonHeldVolume);
80  }
81 
82  private void OnButtonPressed(GameObject go)
83  {
84  PlayClip(Profile.ButtonPressed, Profile.ButtonPressedVolume);
85  }
86 
87  private void OnButtonReleased (GameObject go)
88  {
89  PlayClip(Profile.ButtonReleased, Profile.ButtonReleasedVolume);
90  }
91 
92  private void PlayClip (AudioClip clip, float volume)
93  {
94  if (clip != null)
95  {
96  // Don't play the clip if we're spamming it
97  if (clip.name == lastClipName && (Time.realtimeSinceStartup - lastClipTime) < MinTimeBetweenSameClip)
98  {
99  return;
100  }
101 
102  lastClipName = clip.name;
103  lastClipTime = Time.realtimeSinceStartup;
104  if (audioSource != null)
105  {
106  audioSource.PlayOneShot(clip, volume);
107  }
108  else
109  {
110  AudioSource.PlayClipAtPoint(clip, transform.position, volume);
111  }
112  }
113  }
114 
115 #if UNITY_EDITOR
116  [UnityEditor.CustomEditor(typeof(CompoundButtonSounds))]
117  public class CustomEditor : MRTKEditor { }
118 #endif
119  }
120 }
ButtonStateEnum
State enum for buttons.
Action< GameObject > OnButtonHeld
Event fired when hold interaction initiated.
Definition: Button.cs:81
Action< ButtonStateEnum > StateChange
Event to receive button state change.
Definition: Button.cs:61
Action< GameObject > OnButtonCanceled
Event fired when button interaction canceled.
Definition: Button.cs:86
Action< GameObject > OnButtonReleased
Event fired when released interaction received.
Definition: Button.cs:71
A convenient way to play sounds in response to button actions / states
Ensures a consistent profile field in compound buttons scripts which use a profile ...
Action< GameObject > OnButtonPressed
Event fired when tap interaction received.
Definition: Button.cs:66