gorevan

Untitled

Dec 16th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. [System.Serializable]
  6. public class AudioData
  7. {
  8. public AudioClip audioClip;
  9.  
  10. [Range(0.01f, 1.0f)]
  11. public float volume;
  12. }
  13.  
  14. public class Procedural : MonoBehaviour
  15. {
  16. [Tooltip("List of AudioClip components. This list must be set to the sounds to be played.")]
  17. public List<AudioData> audioDataList = new List<AudioData>();
  18.  
  19. [Tooltip("The main AudioSource component to play the main loop.")]
  20. public AudioSource mainAudioSource;
  21.  
  22. [Tooltip("The main AudioClip loop.")]
  23. public AudioClip mainAudioClipLoop;
  24.  
  25. [Tooltip("The AudioSource component to play the AudioClips.")]
  26. public AudioSource audioSource;
  27.  
  28. [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
  29. public bool loopClips = false;
  30.  
  31. [Tooltip("Start playing sound immediately.")]
  32. public bool playNow = true;
  33.  
  34. void Start()
  35. {
  36. //if there is a main audio clip to be looped
  37. if (mainAudioClipLoop != null)
  38. {
  39. //make sure we have a reference to the AudioSource
  40. if (mainAudioSource == null)
  41. {
  42. mainAudioSource = gameObject.AddComponent<AudioSource>();
  43. }
  44. //play and loop the main audio clip
  45. mainAudioSource.loop = true;
  46. mainAudioSource.clip = mainAudioClipLoop;
  47. mainAudioSource.Play();
  48. }
  49.  
  50. // make sure we have a reference to the AudioSource
  51. if (audioSource == null)
  52. {
  53. audioSource = gameObject.AddComponent<AudioSource>();
  54. }
  55. audioSource.playOnAwake = false;
  56. audioSource.loop = false;
  57.  
  58. //make sure that alll the AudioClips in the audioDataList are valid by removing any null clips
  59. audioDataList.RemoveAll(item => item.audioClip == null);
  60.  
  61. if (playNow && (audioDataList.Count > 0))
  62. {
  63. PlaySequentialSounds();
  64. }
  65. }
  66.  
  67. public void PlaySequentialSounds()
  68. {
  69. StartCoroutine(PlaySounds());
  70. }
  71.  
  72. public void StopSounds()
  73. {
  74. StopCoroutine("PlaySounds");
  75. }
  76.  
  77. IEnumerator PlaySounds()
  78. {
  79. if (audioDataList.Count > 0)
  80. {
  81. //shuffle the list of sounds
  82. ShuffleSounds();
  83.  
  84. for (int i = 0; i < audioDataList.Count; i++)
  85. {
  86. //make sure the volume is set
  87. if (audioDataList[i].volume == 0)
  88. audioDataList[i].volume = 0.5f;
  89. audioSource.volume = audioDataList[i].volume;
  90. audioSource.PlayOneShot(audioDataList[i].audioClip);
  91.  
  92. //wait for the lenght of the clip to finish playing
  93. yield return new WaitForSeconds(audioDataList[i].audioClip.length);
  94. }
  95. }
  96. yield return null;
  97.  
  98. //if loopClips is set to true then call coroutine
  99. if (loopClips)
  100. {
  101. StartCoroutine(PlaySounds());
  102. }
  103. }
  104.  
  105. //Fisher-Yates Shuffle
  106. public void ShuffleSounds()
  107. {
  108. if (audioDataList.Count > 1)
  109. {
  110. for (int i = audioDataList.Count - 1; i > 0; --i)
  111. {
  112. int j = (int)UnityEngine.Random.Range(0, i);
  113. AudioData temp = audioDataList[i];
  114. audioDataList[i] = audioDataList[j];
  115. audioDataList[j] = temp;
  116. }
  117. }
  118. }
  119. }
Add Comment
Please, Sign In to add comment