IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SoundPlayOneshot.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Play one-shot sounds as opposed to continuos/looping ones
4 //
5 //=============================================================================
6 
7 using UnityEngine;
8 using System.Collections;
9 
10 namespace Valve.VR.InteractionSystem
11 {
12  //-------------------------------------------------------------------------
13  public class SoundPlayOneshot : MonoBehaviour
14  {
15  public AudioClip[] waveFiles;
16  private AudioSource thisAudioSource;
17 
18  public float volMin;
19  public float volMax;
20 
21  public float pitchMin;
22  public float pitchMax;
23 
24  public bool playOnAwake;
25 
26 
27  //-------------------------------------------------
28  void Awake()
29  {
30  thisAudioSource = GetComponent<AudioSource>();
31 
32  if ( playOnAwake )
33  {
34  Play();
35  }
36  }
37 
38 
39  //-------------------------------------------------
40  public void Play()
41  {
42  if ( thisAudioSource != null && thisAudioSource.isActiveAndEnabled && !Util.IsNullOrEmpty( waveFiles ) )
43  {
44  //randomly apply a volume between the volume min max
45  thisAudioSource.volume = Random.Range( volMin, volMax );
46 
47  //randomly apply a pitch between the pitch min max
48  thisAudioSource.pitch = Random.Range( pitchMin, pitchMax );
49 
50  // play the sound
51  thisAudioSource.PlayOneShot( waveFiles[Random.Range( 0, waveFiles.Length )] );
52  }
53  }
54 
55 
56  //-------------------------------------------------
57  public void Pause()
58  {
59  if ( thisAudioSource != null )
60  {
61  thisAudioSource.Pause();
62  }
63  }
64 
65 
66  //-------------------------------------------------
67  public void UnPause()
68  {
69  if ( thisAudioSource != null )
70  {
71  thisAudioSource.UnPause();
72  }
73  }
74  }
75 }