/// /// <summary> /// Switch the background music, fading in and out. /// </summary> /// <param name="soundClip"> clip to play. </param> /// <param name="fadeInTime"> time to fade in. </param> /// <param name="fadeOutTime"> time to fade out. </param> /// <param name="loop"> loop music? </param> public void SwitchBackgroundMusic( AudioClip soundClip, float fadeInTime, float fadeOutTime, bool loop = true) { if (mSwitchingBGM) { return; } /* Check if the audio clip good to play. */ if (soundClip == null) { return; } /** * Try to get the fade sound component. * * Since we have multiple component have */ if (mJCSFadeSound == null) { mJCSFadeSound = this.GetComponent <JCS_FadeSound>(); if (mJCSFadeSound == null) { mJCSFadeSound = this.gameObject.AddComponent <JCS_FadeSound>(); } } // get the background music audio source. AudioSource bgmAudioSource = GetBGMAudioSource(); // check if loop bgmAudioSource.loop = loop; // set the audio source. mJCSFadeSound.SetAudioSource(bgmAudioSource); // active the fade sound in effect. mJCSFadeSound.FadeOut( 0, /* Fade in the sound base on the setting. */ fadeInTime); this.mRealSoundFadeOutTime = fadeOutTime; this.mSwitchingBGM = true; this.mDoneFadingOut = false; this.mCurrentBGM = soundClip; }
private void Start() { var sceneS = JCS_SceneSettings.instance; // NOTE(jenchieh): get the fade out time base on the scene setting // and scene manager specific. float fadeoutTime = sceneS.GetSceneFadeInTimeBaseOnSetting(); switch (mSwitchSceneType) { case JCS_SwitchSceneType.BLACK_SCREEN: { // get the current screen color. mBlackScreen.LocalColor = sceneS.SCREEN_COLOR; mBlackScreen.FadeOut(fadeoutTime); } break; case JCS_SwitchSceneType.SLIDE_SCREEN: { mBlackSlideScreen.StartSlideOut(mAlign, fadeoutTime); } break; } // Only need to fade BGM when BGM is not switch between scene. var soundS = JCS_SoundSettings.instance; var soundM = JCS_SoundManager.instance; if (!soundS.KEEP_BGM_SWITCH_SCENE) { if (soundS.SMOOTH_SWITCH_SOUND_BETWEEN_SCENE) { // get the component. if (mJCSFadeSound == null) { mJCSFadeSound = this.gameObject.AddComponent <JCS_FadeSound>(); } // set the background audio source. mJCSFadeSound.SetAudioSource( soundM.GetBGMAudioSource()); // active the fade sound in effect. mJCSFadeSound.FadeIn( soundS.GetBGM_Volume(), /* Fade in the sound base on the setting. */ soundS.GetSoundFadeInTimeBaseOnSetting()); } } else { // If the keep bgm is true, we disable it once everytime a // scene is loaded. // // ATTENTION(jenchieh): This should be place for the last use // of the variable 'KEEP_BGM_SWITCH_SCENE'. soundS.KEEP_BGM_SWITCH_SCENE = false; } // the game is loaded start the game agian JCS_GameManager.instance.GAME_PAUSE = false; }