Created
June 25, 2012 00:51
-
-
Save tapuo/2985765 to your computer and use it in GitHub Desktop.
Sound manager for Unity3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UnityEngine | |
import System.Collections | |
class Sound (MonoBehaviour): | |
public soundObject as GameObject # It have only AudioSource component. | |
# | |
public seTable as (AudioClip) | |
public bgmTable as (AudioClip) | |
# | |
private seIndex as Hash = {} | |
private seAudioSourceTable as (AudioSource) | |
private Fse as (bool) | |
private seWait as (single) | |
# | |
private bgmIndex as Hash = {} | |
private bgmAudioSourceTable as (AudioSource) | |
# | |
private seSize as int = 0 | |
# | |
private currentBGM as int = -1 | |
def Awake(): | |
# se setup | |
sesize as int = len(seTable) | |
seAudioSourceTable = array(AudioSource, sesize) | |
Fse = array(bool, sesize) | |
seWait = array(single, sesize) | |
for i in range(sesize): | |
se as GameObject = Instantiate(soundObject, transform.position, Quaternion.identity); | |
seas as AudioSource = se.GetComponent[of AudioSource]() | |
seas.clip = seTable[i] | |
seAudioSourceTable[i] = seas | |
seIndex[seTable[i].name] = i | |
seWait[i] = 0.0F | |
Fse[i] = false | |
seSize = len(seTable) | |
seTable = null | |
# bgm setup | |
bgmsize as int = len(bgmTable) | |
bgmAudioSourceTable = array(AudioSource, bgmsize) | |
for i in range(bgmsize): | |
bgm as GameObject = Instantiate(soundObject, transform.position, Quaternion.identity); | |
bgmas as AudioSource = bgm.GetComponent[of AudioSource]() | |
bgmas.clip = bgmTable[i] | |
bgmas.loop = true | |
bgmAudioSourceTable[i] = bgmas | |
bgmIndex[bgmTable[i].name] = i | |
bgmTable = null | |
def PlaySE(name as string): | |
Fse[seIndex[name]] = true | |
def _PlayBGM(name as string, d as single) as IEnumerator: | |
yield WaitForSeconds(d) | |
if currentBGM >= 0: | |
bgmAudioSourceTable[currentBGM].Stop() | |
idx as int = bgmIndex[name] | |
currentBGM = idx | |
bgmAudioSourceTable[idx].Play() | |
def PlayBGM(name as string, d as single): | |
StartCoroutine(_PlayBGM(name, d)) | |
def _PlayBGMwithIntro(intro as string, name as string, d as single) as IEnumerator: | |
yield WaitForSeconds(d) | |
if currentBGM >= 0: | |
bgmAudioSourceTable[currentBGM].Stop() | |
idx as int = bgmIndex[intro] | |
bgmAudioSourceTable[idx].loop = false | |
bgmAudioSourceTable[idx].Play() | |
idx = bgmIndex[name] | |
currentBGM = idx | |
aus as AudioSource = bgmAudioSourceTable[idx] | |
aus.Play(aus.clip.frequency * aus.clip.length) | |
def PlayBGMwithIntro(intro as string, name as string, d as single): | |
StartCoroutine(_PlayBGMwithIntro(intro, name, d)) | |
def _StopBGM(d as single) as IEnumerator: | |
yield WaitForSeconds(d) | |
if currentBGM >= 0: | |
bgmAudioSourceTable[currentBGM].Stop() | |
currentBGM = -1 | |
def StopBGM(d as single): | |
StartCoroutine(_StopBGM(d)) | |
def Update (): | |
dt as single = Time.deltaTime | |
for i as int in range(seSize): | |
seWait[i] += dt | |
if Fse[i] and seWait[i] > 0.0625F: | |
seAudioSourceTable[i].Play() | |
seWait[i] = 0.0F | |
Fse[i] = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment