public static bool PlaySound(string se_name, bool se_flag = true) { if (ResourcesLoader.ContainsKey(se_name) == false) { return(false); } if (se_flag) { if (SoundPlayerObj == null) { SoundPlayerObj = new GameObject("SoundPlayer"); SoundAudioSource = SoundPlayerObj.AddComponent <AudioSource>(); } SoundAudioSource.PlayOneShot(ResourcesLoader.GetObjectHandle(se_name)); } else { if (BGMPlayerObj == null) { BGMPlayerObj = new GameObject("BGMPlayer"); BGMAudioSource = BGMPlayerObj.AddComponent <AudioSource>(); BGMAudioSource.clip = ResourcesLoader.GetObjectHandle(se_name); BGMAudioSource.volume = 1.0f; BGMAudioSource.loop = true; BGMAudioSource.Play(); } else { if (BGMAudioSource) { if (BGMAudioSource.isPlaying) { BGMAudioSource.Stop(); } else { BGMAudioSource.Play(); } } else { BGMAudioSource = BGMPlayerObj.AddComponent <AudioSource>(); if (BGMAudioSource) { BGMAudioSource.clip = ResourcesLoader.GetObjectHandle(se_name); BGMAudioSource.volume = 1.0f; BGMAudioSource.loop = true; BGMAudioSource.Play(); } } } } return(true); }
//------------------------------------------------------------------------ /*! * @brief BGM停止 * @param[in] bool (fade) フェード * @param[in] float (sec) 時間(秒) */ //------------------------------------------------------------------------ public void StopBGM(bool fade, float sec) { // 再生中の全てのオーディオソースに対し、停止命令を出す BGMAudioSource audio = null; for (int i = 0; i < m_BGMAudioSource.Length; i++) { audio = m_BGMAudioSource[i]; // 再生中の物を停止していく if (audio.playState != BGMPlayState.BGM_STATE_PLAYING && audio.playState != BGMPlayState.BGM_STATE_FADEIN) { continue; } // 停止 audio.Stop(sec); } }
public static IEnumerator SetFadeTimer(float start_val, float end_val, float end_time) { FadeTimer = new CFadeTimer(start_val, end_val, end_time); while (true) { float t = FadeTimer.CalcTime(); BGMAudioSource.volume = t; if (t <= 0.0f) { BGMAudioSource.Stop(); yield break; } else { yield return(new WaitForSeconds(Time.deltaTime)); } } }