/// <summary> /// Start /// </summary> protected void Start() { _sounds = GetSoundProps(); if (_sounds == null) { Debug.Log("[ERROR] You should define a SoundProp vector at SoundList... class."); return; } // Relate the array entries with the specified audioClip for (int i = 0; i < soundClips.Length; i++) { // Some sounds could be null (if they are not used in the level // but the have to keep the sound index for other level) if (soundClips[i] != null) { SoundProp sp = GetSoundPropByName(soundClips[i].name); if (sp != null) { sp.audioClip = soundClips[i]; } else { Debug.LogWarning(String.Format("Cannot find the sound {0} on the array list of sounds.", soundClips[i].name)); } } } }
/// <summary> /// Play an specific sound /// </summary> public static int PlaySound(Vector3 pos, int sndId) { if (fx == null) { return(-1); } SoundProp sp = soundList.GetSoundProp(sndId); if (sp != null) { // The specified sound should be marked as FX (the default value) if (sp.type == SndType.SND_FX) { int channeldIdx = getChannelIdx(sp); if (channeldIdx != -1) { playThisSoundOnSource(channeldIdx, sp, pos); return(channeldIdx); } else { Debug.Log("All audiosource are busy. Cannot play sound: " + sp.name); } } else { Debug.Log(String.Format("Trying to play a sound that is not a FX ({0})", sp.name)); } } return(-1); }
/// <summary> /// Should be called if sndId is changed dynamically /// </summary> public void RefreshSoundPropProperties() { if (soundList == null) { soundList = SoundManager.Instance.GetSoundList(); } if (soundList == null) { Debug.LogError("The soundList returned is null."); return; } sndIdInt = getSndIdInt(); SoundProp sp = GetSoundProp(sndIdInt); if (sp == null) { Debug.LogError(string.Format("Cannot find sndId: {0}", sndIdInt)); } else { gameObject.GetComponent <AudioSource>().clip = sp.audioClip; gameObject.GetComponent <AudioSource>().loop = sp.loop; gameObject.GetComponent <AudioSource>().pitch = sp.pitch; gameObject.GetComponent <AudioSource>().panStereo = sp.pan; gameObject.GetComponent <AudioSource>().minDistance = sp.minDistance; gameObject.GetComponent <AudioSource>().maxDistance = sp.maxDistance; gameObject.GetComponent <AudioSource>().volume = SoundManager.Instance.FxVolume * SoundManager.Instance.MasterVolume * (sp.volume / 100.0f); } }
public void PlayMusic(int sndId, bool fadeOutCurrent) { if (!SoundConfigParams.useGameMusic) { return; } SoundProp sp = GetSoundProp(sndId); if (sp != null) { currentMusicSndProp = sp; if (fadeOutCurrent && music.isPlaying) { FadeOutMusic(1, playMusicAfterFadeOut); } else { playMusicAfterFadeOut(); } } else { Debug.LogWarning(string.Format("Cannot find the music id: {0}", sndId)); } }
private static void playThisSoundOnSource(int idx, SoundProp sp, Vector3 pos) { fx[idx].Stop(); fx[idx].clip = sp.audioClip; fx[idx].loop = sp.loop; fx[idx].volume = FxVolume * MasterVolume * (sp.volume / 100.0f); fx[idx].transform.position = pos; fx[idx].Play(); }
/// <summary> /// Returns a channeld idx to play a sound. /// Could be: /// 1. An empty channel (not yet used) /// 2. An IDLE channel /// 3. A busy channel but with less priority /// 4. A busy channel with the same priority /// /// If there isn't a channel that satisfy these conditions, returns -1 /// /// </summary> /// <returns></returns> int getChannelIdx(SoundProp sp) { for (int i = 1; i < AS_COUNT; i++) { if (fx[i].clip != null) { // Found a audiosource that is not currently being played if (!fx[i].isPlaying) { //if (_fx[i].clip != sp.audioClip) { return(i); } } } else { return(i); } } // No audiosource idle. Find a busy audiosource with less priority than the new one for (int i = 0; i < AS_COUNT; i++) { if (fx[i].clip != null) { SoundProp prop = getSoundPropByName(fx[i].clip.name); if (sp.priority > prop.priority) { Debug.Log("Returning a used channel"); return(i); } } } // Try something with the same priority for (int i = 0; i < AS_COUNT; i++) { if (fx[i].clip != null) { SoundProp prop = getSoundPropByName(fx[i].clip.name); if (sp.priority == prop.priority) { //Debug.Log("Returning a used channel2"); return(i); } } } // Cannot find a suitable channel return(-1); }
/// <summary> /// Unity Start method /// </summary> protected void Start() { _sounds = GetSoundProps(); if (_sounds == null) { Debug.Log("[ERROR] You should define a SoundProp vector at SoundList... class."); return; } // New if (loadedFromResources) { soundClips = new AudioClip[_sounds.Length]; for (int i = 0; i < _sounds.Length; i++) { if (_sounds[i].type == SndType.SND_FX) { soundClips[i] = Resources.Load(string.Format("Sounds/{0}", _sounds[i].name)) as AudioClip; } else { soundClips[i] = Resources.Load(string.Format("Music/{0}", _sounds[i].name)) as AudioClip; } } } // Relate the array entries with the specified audioClip for (int i = 0; i < soundClips.Length; i++) { // Some sounds could be null (if they are not used in the level // but the have to keep the sound index for other level) if (soundClips[i] != null) { SoundProp sp = GetSoundPropByName(soundClips[i].name); if (sp != null) { sp.audioClip = soundClips[i]; } else { Debug.LogWarning(String.Format("Cannot find the sound {0} on the array list of sounds.", soundClips[i].name)); } } } }
public void FadeInMusic(int sndId, float inSecs, SoundManagerCallback cbfn) { if (!SoundConfigParams.useGameMusic) { return; } SoundProp sp = GetSoundProp(sndId); if (sp != null) { currentMusicSndProp = sp; // Set the position of the current camera in order to play the sound balanced if (Camera.main != null) { music.transform.position = Camera.main.transform.position; } music.clip = sp.audioClip; music.loop = sp.loop; music.volume = 0; music.Play(); FadeAudioSource fas = new FadeAudioSource(); fas.initialTime = Time.realtimeSinceStartup; fas.accumTime = 0; fas.initialVolume = 0; fas.targetVolume = MusicVolume * MasterVolume * (sp.volume / 100.0f); fas.audioSrc = music; fas.fadeInSecs = inSecs; if (cbfn != null) { fas.fnCb += cbfn; } else { fas.fnCb = null; } asFades.Add(fas); } }
/// <summary> /// Returns a channeld idx to play a sound. /// Could be: /// 1. An empty channel (not yet used) /// 2. An IDLE channel /// 3. A busy channel but with less priority /// 4. A busy channel with the same priority /// /// If there isn't a channel that satisfy these conditions, returns -1 /// /// </summary> /// <returns></returns> private static int getChannelIdx(SoundProp sp) { for (int i = 0; i < CHANNELS; i++) { if (fx[i].clip != null) { if (!fx[i].isPlaying) { // Found a audiosource that is not currently being played if (fx[i].clip != sp.audioClip) { return(i); } } } else { return(i); } } // No audiosource idle. Find a busy audiosource with less priority than the new one for (int i = 0; i < CHANNELS; i++) { SoundProp prop = soundList.GetSoundPropByName(fx[i].clip.name); if (sp.priority > prop.priority) { return(i); } } // Try something with the same priority for (int i = 0; i < CHANNELS; i++) { SoundProp prop = soundList.GetSoundPropByName(fx[i].clip.name); if (sp.priority == prop.priority) { return(i); } } // Cannot find a suitable channel return(-1); }
public void FadeInSound(int sndId, float inSecs, SoundManagerCallback cbfn) { //if (!ConfigParams.useGameMusic) return; SoundProp sp = GetSoundProp(sndId); if (sp != null) { int channeldIdx = getChannelIdx(sp); if (channeldIdx != -1) { // Set the position of the current camera in order to play the sound balanced if (Camera.main != null) { fx[channeldIdx].transform.position = Camera.main.transform.position; } fx[channeldIdx].clip = sp.audioClip; fx[channeldIdx].loop = sp.loop; fx[channeldIdx].volume = 0; fx[channeldIdx].Play(); FadeAudioSource fas = new FadeAudioSource(); fas.initialTime = Time.realtimeSinceStartup; fas.accumTime = 0; fas.initialVolume = 0; fas.targetVolume = FxVolume * MasterVolume * (sp.volume / 100.0f); fas.audioSrc = fx[channeldIdx]; fas.fadeInSecs = inSecs; if (cbfn != null) { fas.fnCb += cbfn; } else { fas.fnCb = null; } asFades.Add(fas); } } }
/// <summary> /// Play the background sound /// </summary> /// <param name="int"></param> public static void PlayBackground(int sndId) { SoundProp sp = soundList.GetSoundProp(sndId); if (sp != null) { currentBackgroundSndProp = sp; // Set the position of the current camera in order to play the sound balanced if (Camera.main != null) { background.transform.position = Camera.main.transform.position; } background.clip = sp.audioClip; background.loop = sp.loop; background.volume = BackgroundVolume * MasterVolume * (sp.volume / 100.0f); background.Play(); } }
public static void PlayMusic(int sndId, bool fadeOutCurrent) { if (instance == null) { return; } SoundProp sp = soundList.GetSoundProp(sndId); if (sp != null) { currentMusicSndProp = sp; if (fadeOutCurrent && music.isPlaying) { FadeOutMusic(1, playMusicAfterFadeOut); } else { playMusicAfterFadeOut(); } } }
SoundProp getSoundPropByName(string name) { if (soundList == null) { soundList = GetSoundList(); if (soundList == null) { return(null); } } foreach (SoundList list in soundList) { SoundProp prop = list.GetSoundPropByName(name); if (prop != null) { return(prop); } } return(null); }
public void play_prop_touch() { SoundProp.clip = prop_touch; SoundProp.Play(); }
public void play_prop_popup() { SoundProp.clip = prop_popup; SoundProp.Play(); }