/** * <summary>Serialises appropriate GameObject values into a string.</summary> * <returns>The data, serialised as a string</returns> */ public override string SaveData() { Sound sound = GetComponent <Sound>(); AudioSource audioSource = GetComponent <AudioSource>(); SoundData soundData = new SoundData(); soundData.objectID = constantID; if (sound.IsFadingOut()) { soundData.isPlaying = false; } else { soundData.isPlaying = sound.IsPlaying(); } soundData.isLooping = audioSource.loop; soundData.samplePoint = audioSource.timeSamples; soundData.relativeVolume = sound.relativeVolume; if (audioSource.clip != null) { soundData.clipID = AssetLoader.GetAssetInstanceID(audioSource.clip); } return(Serializer.SaveScriptData <SoundData> (soundData)); }
protected void PlayMoveSound(float speed) { if (slidePitchFactor > 0f) { float targetPitch = Mathf.Min(1f, speed * slidePitchFactor); moveSound.audioSource.pitch = Mathf.Lerp(moveSound.audioSource.pitch, targetPitch, Time.deltaTime * 3f); } if (speed > slideSoundThreshold) { if (!moveSound.IsPlaying()) { moveSound.Play(moveSoundClip, true); } } else if (moveSound.IsPlaying() && !moveSound.IsFading()) { moveSound.FadeOut(0.2f); } }
protected void PlayMoveSound(float speed, float trackValue) { if (speed > slideSoundThreshold) { moveSound.relativeVolume = (speed - slideSoundThreshold); // * 5f; moveSound.SetMaxVolume(); if (slidePitchFactor > 0f) { moveSound.GetComponent <AudioSource>().pitch = Mathf.Lerp(GetComponent <AudioSource>().pitch, Mathf.Min(1f, speed), Time.deltaTime * 5f); } } if (speed > slideSoundThreshold && !moveSound.IsPlaying()) // && trackValue > 0.02f && trackValue < 0.98f) { moveSound.relativeVolume = (speed - slideSoundThreshold); // * 5f; moveSound.Play(moveSoundClip, true); } else if (speed <= slideSoundThreshold && moveSound.IsPlaying() && !moveSound.IsFading()) { moveSound.FadeOut(0.2f); } }
/** * <summary>Plays an AudioClip on the default Sound prefab.</summary> * <param name = "audioClip">The AudioClip to play</param> * <param name = "doLoop">If True, the sound will loop</param> * <param name="avoidRestarting">If True, then the sound will not play if the same clip is already playing</param> */ public void PlayDefaultSound(AudioClip audioClip, bool doLoop, bool avoidRestarting = false) { if (audioClip == null) { return; } if (defaultSound == null) { ACDebug.Log("Cannot play audio '" + audioClip.name + "' since no Default Sound is defined in the scene - please assign one in the Scene Manager.", audioClip); return; } if (KickStarter.stateHandler.IsPaused() && !defaultSound.playWhilePaused) { ACDebug.LogWarning("Cannot play audio '" + audioClip.name + "' on Sound '" + defaultSound.gameObject.name + "' while the game is paused - check 'Play while game paused?' on the Sound component's Inspector.", defaultSound); } if (defaultAudioSource == null) { defaultAudioSource = defaultSound.GetComponent <AudioSource> (); } if (doLoop) { defaultAudioSource.clip = audioClip; defaultSound.Play(doLoop); } else if (avoidRestarting) { if (defaultAudioSource.clip == audioClip && defaultSound.IsPlaying()) { return; } defaultAudioSource.clip = audioClip; defaultSound.Play(false); } else { defaultSound.SetMaxVolume(); defaultAudioSource.PlayOneShot(audioClip); } }
override public float Run() { if (runtimeSound == null) { return(0f); } if (!isRunning) { isRunning = true; if (ignoreIfPlaying && (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn)) { if ((audioClip != null && runtimeSound.IsPlaying(audioClip)) || (audioClip == null && runtimeSound.IsPlaying())) { // Sound object is already playing the desired clip return(0f); } } if (audioClip && runtimeSound.GetComponent <AudioSource>()) { if (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn) { runtimeSound.GetComponent <AudioSource>().clip = audioClip; } } if (runtimeSound.soundType == SoundType.Music && (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn)) { Sound[] sounds = FindObjectsOfType(typeof(Sound)) as Sound[]; foreach (Sound sound in sounds) { sound.EndOld(SoundType.Music, runtimeSound); } } if (soundAction == SoundAction.Play) { runtimeSound.Play(loop); if (!loop && willWait) { return(defaultPauseTime); } } else if (soundAction == SoundAction.FadeIn) { if (fadeTime <= 0f) { runtimeSound.Play(loop); } else { runtimeSound.FadeIn(fadeTime, loop); if (!loop && willWait) { return(defaultPauseTime); } } } else if (soundAction == SoundAction.FadeOut) { if (fadeTime <= 0f) { runtimeSound.Stop(); } else { runtimeSound.FadeOut(fadeTime); if (willWait) { return(fadeTime); } } } else if (soundAction == SoundAction.Stop) { runtimeSound.Stop(); if (affectChildren) { foreach (Transform child in runtimeSound.transform) { if (child.GetComponent <Sound>()) { child.GetComponent <Sound>().Stop(); } } } } } else { if (soundAction == SoundAction.FadeOut) { isRunning = false; return(0f); } if (runtimeSound.IsPlaying()) { return(defaultPauseTime); } else { isRunning = false; } } return(0f); }
override public float Run() { if (soundObject) { if ((audioClip != null && soundObject.IsPlaying(audioClip)) || (audioClip == null && soundObject.IsPlaying())) { // Sound object is already playing the desired clip if (ignoreIfPlaying && (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn)) { return(0f); } } if (audioClip && soundObject.GetComponent <AudioSource>()) { if (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn) { soundObject.GetComponent <AudioSource>().clip = audioClip; } } if (soundObject.soundType == SoundType.Music && (soundAction == SoundAction.Play || soundAction == SoundAction.FadeIn)) { Sound[] sounds = FindObjectsOfType(typeof(Sound)) as Sound[]; foreach (Sound sound in sounds) { sound.EndOldMusic(soundObject); } } if (soundAction == SoundAction.Play) { soundObject.Play(loop); } else if (soundAction == SoundAction.FadeIn) { if (fadeTime == 0f) { soundObject.Play(loop); } else { soundObject.FadeIn(fadeTime, loop); } } else if (soundAction == SoundAction.FadeOut) { if (fadeTime == 0f) { soundObject.Stop(); } else { soundObject.FadeOut(fadeTime); } } else if (soundAction == SoundAction.Stop) { soundObject.Stop(); if (affectChildren) { foreach (Transform child in soundObject.transform) { if (child.GetComponent <Sound>()) { child.GetComponent <Sound>().Stop(); } } } } } return(0f); }