AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ButtonSounds.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 UnityEngine;
5 
6 namespace HoloToolkit.Unity.Buttons
7 {
11  [RequireComponent(typeof(Button))]
12  public class ButtonSounds : MonoBehaviour
13  {
14  const float MinTimeBetweenSameClip = 0.1f;
15 
16  // Direct interaction clips
17  public AudioClip ButtonCanceled;
18  public AudioClip ButtonHeld;
19  public AudioClip ButtonPressed;
20  public AudioClip ButtonReleased;
21 
22  // State change clips
23  public AudioClip ButtonObservation;
24  public AudioClip ButtonObservationTargeted;
25  public AudioClip ButtonTargeted;
26 
27  private AudioSource audioSource;
28  private static string lastClipName;
29  private static float lastClipTime;
30 
31  void Start ()
32  {
33  Button button = GetComponent<Button>();
34  button.OnButtonCanceled += OnButtonCanceled;
35  button.OnButtonHeld += OnButtonHeld;
36  button.OnButtonPressed += OnButtonPressed;
37  button.OnButtonReleased += OnButtonReleased;
38  button.StateChange += StateChange;
39 
40  audioSource = GetComponent<AudioSource>();
41  }
42 
43  void StateChange(ButtonStateEnum newState)
44  {
45  switch (newState)
46  {
47  case ButtonStateEnum.Observation:
48  PlayClip(ButtonObservation);
49  break;
50 
51  case ButtonStateEnum.ObservationTargeted:
52  PlayClip(ButtonObservationTargeted);
53  break;
54 
55  case ButtonStateEnum.Targeted:
56  PlayClip(ButtonTargeted);
57  break;
58 
59  default:
60  break;
61  }
62  }
63 
64  void OnButtonCanceled(GameObject go)
65  {
66  PlayClip(ButtonCanceled);
67  }
68 
69  void OnButtonHeld(GameObject go)
70  {
71  PlayClip(ButtonHeld);
72  }
73 
74  void OnButtonPressed(GameObject go)
75  {
76  PlayClip(ButtonPressed);
77  }
78 
79  void OnButtonReleased (GameObject go)
80  {
81  PlayClip(ButtonReleased);
82  }
83 
84  void PlayClip (AudioClip clip)
85  {
86  if (clip != null)
87  {
88  // Don't play the clip if we're spamming it
89  if (clip.name == lastClipName && (lastClipTime - Time.realtimeSinceStartup) < MinTimeBetweenSameClip)
90  return;
91 
92  lastClipName = clip.name;
93  lastClipTime = Time.realtimeSinceStartup;
94  if (audioSource != null)
95  {
96  audioSource.PlayOneShot(clip);
97  }
98  else
99  {
100  AudioSource.PlayClipAtPoint(clip, transform.position);
101  }
102  }
103  }
104  }
105 }
A convenient way to play sounds in response to button actions / states
Definition: ButtonSounds.cs:12
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
Action< GameObject > OnButtonPressed
Event fired when tap interaction received.
Definition: Button.cs:66