From ca94bc1c0f91f7d135edae6f86434e0f3a6f2451 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 25 Sep 2014 12:16:59 +0100 Subject: [PATCH] Set PlayMusic start time (via Ben Keenan) Renamed SetMusicVolume to SetAudioVolume --- Assets/Fungus/Audio/Commands/PlayMusic.cs | 6 +++++- Assets/Fungus/Audio/Commands/SetMusicVolume.cs | 2 +- Assets/Fungus/Audio/Scripts/MusicController.cs | 14 +++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Assets/Fungus/Audio/Commands/PlayMusic.cs b/Assets/Fungus/Audio/Commands/PlayMusic.cs index e8e6be14..d037a65a 100644 --- a/Assets/Fungus/Audio/Commands/PlayMusic.cs +++ b/Assets/Fungus/Audio/Commands/PlayMusic.cs @@ -10,12 +10,16 @@ namespace Fungus { public AudioClip musicClip; + [Tooltip("Time to begin playing in seconds. If the audio file is compressed, the time index may be inaccurate.")] + public float atTime; + public override void OnEnter() { MusicController musicController = MusicController.GetInstance(); if (musicController != null) { - musicController.PlayMusic(musicClip); + float startTime = Mathf.Max (0, atTime); + musicController.PlayMusic(musicClip, startTime); } Continue(); diff --git a/Assets/Fungus/Audio/Commands/SetMusicVolume.cs b/Assets/Fungus/Audio/Commands/SetMusicVolume.cs index a5abf57f..051fefe4 100644 --- a/Assets/Fungus/Audio/Commands/SetMusicVolume.cs +++ b/Assets/Fungus/Audio/Commands/SetMusicVolume.cs @@ -19,7 +19,7 @@ namespace Fungus MusicController musicController = MusicController.GetInstance(); if (musicController != null) { - musicController.SetMusicVolume(volume, fadeDuration); + musicController.SetAudioVolume(volume, fadeDuration); } Continue(); diff --git a/Assets/Fungus/Audio/Scripts/MusicController.cs b/Assets/Fungus/Audio/Scripts/MusicController.cs index beffa372..1ae4d4da 100644 --- a/Assets/Fungus/Audio/Scripts/MusicController.cs +++ b/Assets/Fungus/Audio/Scripts/MusicController.cs @@ -39,10 +39,12 @@ namespace Fungus * Plays game music using an audio clip. * One music clip may be played at a time. * @param musicClip The music clip to play + * @param atTime Time in the music clip to start at */ - public void PlayMusic(AudioClip musicClip) + public void PlayMusic(AudioClip musicClip, float atTime = 0) { audio.clip = musicClip; + audio.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK audio.Play(); } @@ -53,13 +55,13 @@ namespace Fungus { audio.Stop(); } - + /** * Fades the game music volume to required level over a period of time. * @param volume The new music volume value [0..1] * @param duration The length of time in seconds needed to complete the volume change. */ - public virtual void SetMusicVolume(float volume, float duration) + public virtual void SetAudioVolume(float volume, float duration) { iTween.AudioTo(gameObject, volume, 1f, duration); } @@ -74,5 +76,11 @@ namespace Fungus { audio.PlayOneShot(soundClip, volume); } + + public virtual void PlaySoundAtTime(AudioClip soundClip, float volume, float atTime) + { + audio.time = atTime; // This may not work BK + audio.PlayOneShot(soundClip, volume); + } } } \ No newline at end of file