Guest User

Untitled

a guest
Nov 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import UnityEngine
  2. import System.Collections
  3.  
  4. class Sound (MonoBehaviour):
  5. public soundObject as GameObject # It have only AudioSource component.
  6. #
  7. public seTable as (AudioClip)
  8. public bgmTable as (AudioClip)
  9. #
  10. private seIndex as Hash = {}
  11. private seAudioSourceTable as (AudioSource)
  12. private Fse as (bool)
  13. private seWait as (single)
  14. #
  15. private bgmIndex as Hash = {}
  16. private bgmAudioSourceTable as (AudioSource)
  17. #
  18. private seSize as int = 0
  19. #
  20. private currentBGM as int = -1
  21.  
  22.  
  23. def Awake():
  24. # se setup
  25. sesize as int = len(seTable)
  26. seAudioSourceTable = array(AudioSource, sesize)
  27. Fse = array(bool, sesize)
  28. seWait = array(single, sesize)
  29. for i in range(sesize):
  30. se as GameObject = Instantiate(soundObject, transform.position, Quaternion.identity);
  31. seas as AudioSource = se.GetComponent[of AudioSource]()
  32. seas.clip = seTable[i]
  33. seAudioSourceTable[i] = seas
  34. seIndex[seTable[i].name] = i
  35. seWait[i] = 0.0F
  36. Fse[i] = false
  37. seSize = len(seTable)
  38. seTable = null
  39. # bgm setup
  40. bgmsize as int = len(bgmTable)
  41. bgmAudioSourceTable = array(AudioSource, bgmsize)
  42. for i in range(bgmsize):
  43. bgm as GameObject = Instantiate(soundObject, transform.position, Quaternion.identity);
  44. bgmas as AudioSource = bgm.GetComponent[of AudioSource]()
  45. bgmas.clip = bgmTable[i]
  46. bgmas.loop = true
  47. bgmAudioSourceTable[i] = bgmas
  48. bgmIndex[bgmTable[i].name] = i
  49. bgmTable = null
  50.  
  51. def PlaySE(name as string):
  52. Fse[seIndex[name]] = true
  53.  
  54. def _PlayBGM(name as string, d as single) as IEnumerator:
  55. yield WaitForSeconds(d)
  56. if currentBGM >= 0:
  57. bgmAudioSourceTable[currentBGM].Stop()
  58. idx as int = bgmIndex[name]
  59. currentBGM = idx
  60. bgmAudioSourceTable[idx].Play()
  61.  
  62. def PlayBGM(name as string, d as single):
  63. StartCoroutine(_PlayBGM(name, d))
  64.  
  65. def _PlayBGMwithIntro(intro as string, name as string, d as single) as IEnumerator:
  66. yield WaitForSeconds(d)
  67. if currentBGM >= 0:
  68. bgmAudioSourceTable[currentBGM].Stop()
  69. idx as int = bgmIndex[intro]
  70. bgmAudioSourceTable[idx].loop = false
  71. bgmAudioSourceTable[idx].Play()
  72. idx = bgmIndex[name]
  73. currentBGM = idx
  74. aus as AudioSource = bgmAudioSourceTable[idx]
  75. aus.Play(aus.clip.frequency * aus.clip.length)
  76.  
  77. def PlayBGMwithIntro(intro as string, name as string, d as single):
  78. StartCoroutine(_PlayBGMwithIntro(intro, name, d))
  79.  
  80. def _StopBGM(d as single) as IEnumerator:
  81. yield WaitForSeconds(d)
  82. if currentBGM >= 0:
  83. bgmAudioSourceTable[currentBGM].Stop()
  84. currentBGM = -1
  85.  
  86. def StopBGM(d as single):
  87. StartCoroutine(_StopBGM(d))
  88.  
  89. def Update ():
  90. dt as single = Time.deltaTime
  91. for i as int in range(seSize):
  92. seWait[i] += dt
  93. if Fse[i] and seWait[i] > 0.0625F:
  94. seAudioSourceTable[i].Play()
  95. seWait[i] = 0.0F
  96. Fse[i] = false
Add Comment
Please, Sign In to add comment