RandomSoundOverTime.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace Demos
  12. {
  13. namespace TheLastParticle
  14. {
  15. // =================================
  16. // Classes.
  17. // =================================
  18. //[ExecuteInEditMode]
  19. [System.Serializable]
  20. public class RandomSoundOverTime : MonoBehaviour
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. public class RandomInt
  27. {
  28. int value;
  29. bool used;
  30. }
  31. // =================================
  32. // Variables.
  33. // =================================
  34. // ...
  35. public Vector2 timeRange = new Vector2(5.0f, 10.0f);
  36. public Vector2 volumeRange = new Vector2(0.5f, 1.0f);
  37. public Vector2 pitchRange = new Vector2(0.5f, 2.0f);
  38. float timer;
  39. float nextPlayTime;
  40. AudioSource audioSource;
  41. RandomSequence randomSequence;
  42. public AudioClip[] audioClips;
  43. // =================================
  44. // Functions.
  45. // =================================
  46. // ...
  47. void Awake()
  48. {
  49. }
  50. // ...
  51. void Start()
  52. {
  53. setNextPlayTime();
  54. audioSource = GetComponent<AudioSource>();
  55. randomSequence = new RandomSequence(audioClips.Length, 8);
  56. for (int i = 0; i < 128; i++)
  57. {
  58. //print(randomSequence.get());
  59. }
  60. }
  61. // ...
  62. void setNextPlayTime()
  63. {
  64. nextPlayTime = Random.Range(timeRange.x, timeRange.y);
  65. }
  66. // ...
  67. void Update()
  68. {
  69. timer += Time.deltaTime;
  70. if (timer >= nextPlayTime)
  71. {
  72. timer = 0.0f;
  73. setNextPlayTime();
  74. AudioClip ac = audioClips[randomSequence.get()];
  75. float volume = Random.Range(volumeRange.x, volumeRange.y);
  76. float pitch = Random.Range(pitchRange.x, pitchRange.y);
  77. audioSource.pitch = pitch;
  78. audioSource.PlayOneShot(ac, volume);
  79. }
  80. }
  81. // =================================
  82. // End functions.
  83. // =================================
  84. }
  85. // =================================
  86. // End namespace.
  87. // =================================
  88. }
  89. }
  90. }
  91. // =================================
  92. // --END-- //
  93. // =================================