Browse Source

Added audio commands and new audio example room.

- PlayGameMusic()
- StopGameMusic()
- SetMusicVolume()
- PlaySound()
master
chrisgregan 11 years ago
parent
commit
e003e844d2
  1. BIN
      Assets/Fungus/Prefabs/Game.prefab
  2. 93
      Assets/Fungus/Scripts/Commands.cs
  3. 36
      Assets/Fungus/Scripts/Room.cs
  4. 5
      Assets/FungusExample/Audio.meta
  5. BIN
      Assets/FungusExample/Audio/effect.wav
  6. 12
      Assets/FungusExample/Audio/effect.wav.meta
  7. BIN
      Assets/FungusExample/Audio/music.mp3
  8. 12
      Assets/FungusExample/Audio/music.mp3.meta
  9. BIN
      Assets/FungusExample/Scenes/Example.unity
  10. 65
      Assets/FungusExample/Scripts/AudioRoom.cs
  11. 8
      Assets/FungusExample/Scripts/AudioRoom.cs.meta
  12. 7
      Assets/FungusExample/Scripts/MenusRoom.cs

BIN
Assets/Fungus/Prefabs/Game.prefab

Binary file not shown.

93
Assets/Fungus/Scripts/Commands.cs

@ -570,4 +570,97 @@ namespace Fungus
});
}
}
// Plays a music clip
public class PlayMusicCommand : CommandQueue.Command
{
AudioClip audioClip;
public PlayMusicCommand(AudioClip _audioClip)
{
if (_audioClip == null)
{
Debug.LogError("Audio clip must not be null.");
return;
}
audioClip = _audioClip;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance();
game.audio.clip = audioClip;
game.audio.Play();
if (onComplete != null)
{
onComplete();
}
}
}
// Stops a music clip
public class StopMusicCommand : CommandQueue.Command
{
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance();
game.audio.Stop();
if (onComplete != null)
{
onComplete();
}
}
}
// Fades music volume to required level over a period of time
public class SetMusicVolumeCommand : CommandQueue.Command
{
float musicVolume;
float duration;
public SetMusicVolumeCommand(float _musicVolume, float _duration)
{
musicVolume = _musicVolume;
duration = _duration;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance();
iTween.AudioTo(game.gameObject, musicVolume, 1f, duration);
if (onComplete != null)
{
onComplete();
}
}
}
// Plays a sound effect once
public class PlaySoundCommand : CommandQueue.Command
{
AudioClip audioClip;
float volume;
public PlaySoundCommand(AudioClip _audioClip, float _volume)
{
audioClip = _audioClip;
volume = _volume;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance();
game.audio.PlayOneShot(audioClip, volume);
if (onComplete != null)
{
onComplete();
}
}
}
}

36
Assets/Fungus/Scripts/Room.cs

@ -364,5 +364,41 @@ namespace Fungus
{
commandQueue.AddCommand(new FadeToViewCommand(targetView, duration));
}
// Plays game music using an audio clip
public void PlayGameMusic(AudioClip audioClip)
{
commandQueue.AddCommand(new PlayMusicCommand(audioClip));
}
// Stops playing game music
public void StopGameMusic()
{
commandQueue.AddCommand(new StopMusicCommand());
}
// Sets music volume immediately
public void SetMusicVolume(float musicVolume)
{
commandQueue.AddCommand(new SetMusicVolumeCommand(musicVolume, 0f));
}
// Fades music volume to required level over a period of time
public void SetMusicVolume(float musicVolume, float duration)
{
commandQueue.AddCommand(new SetMusicVolumeCommand(musicVolume, duration));
}
// Plays a sound effect once
public void PlaySound(AudioClip audioClip)
{
commandQueue.AddCommand(new PlaySoundCommand(audioClip, 1f));
}
// Plays a sound effect once, at the specified volume
public void PlaySound(AudioClip audioClip, float volume)
{
commandQueue.AddCommand(new PlaySoundCommand(audioClip, volume));
}
}
}

5
Assets/FungusExample/Audio.meta

@ -0,0 +1,5 @@
fileFormatVersion: 2
guid: 23916e2f1deff4e31a36b6e75aed0906
folderAsset: yes
DefaultImporter:
userData:

BIN
Assets/FungusExample/Audio/effect.wav

Binary file not shown.

12
Assets/FungusExample/Audio/effect.wav.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5446370c736a84b6d9d6a9bb6f4d4521
AudioImporter:
serializedVersion: 4
format: 0
quality: .5
stream: 1
3D: 0
forceToMono: 0
useHardware: 0
loopable: 0
userData:

BIN
Assets/FungusExample/Audio/music.mp3

Binary file not shown.

12
Assets/FungusExample/Audio/music.mp3.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 81d2b469d3efe4fd9838d3a4f5165bee
AudioImporter:
serializedVersion: 4
format: 0
quality: .5
stream: 2
3D: 0
forceToMono: 0
useHardware: 0
loopable: 0
userData:

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

65
Assets/FungusExample/Scripts/AudioRoom.cs

@ -0,0 +1,65 @@
using UnityEngine;
using System.Collections;
using Fungus;
public class AudioRoom : Room
{
public Room menuRoom;
public AudioClip musicClip;
public AudioClip effectClip;
void OnEnter()
{
if (GetFlag("music"))
{
AddOption("Stop the music", StopMusic);
if (GetFlag("quiet") == false)
{
AddOption("Shhh! Make it quieter", MakeQuiet);
}
}
else
{
AddOption("Play some music", StartMusic);
}
AddOption("Play a sound effect", PlaySound);
AddOption("Back to menu", MainMenu);
Choose("We are the music makers, and we are the dreamers of dreams.");
}
void StartMusic()
{
PlayGameMusic(musicClip);
SetMusicVolume(1f);
SetFlag("music", true);
Call(OnEnter);
}
void StopMusic()
{
StopGameMusic();
SetFlag("music", false);
SetFlag("quiet", false);
Call(OnEnter);
}
void PlaySound()
{
PlaySound(effectClip, 1f);
Call(OnEnter);
}
void MakeQuiet()
{
SetFlag("quiet", true);
SetMusicVolume(0.25f, 1f);
Call(OnEnter);
}
void MainMenu()
{
MoveToRoom(menuRoom);
}
}

8
Assets/FungusExample/Scripts/AudioRoom.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cf05e131525e5483ebe1ab66557e8824
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

7
Assets/FungusExample/Scripts/MenusRoom.cs

@ -7,12 +7,14 @@ public class MenusRoom : Room
public Room pagesRoom;
public Room viewsRoom;
public Room spritesRoom;
public Room audioRoom;
void OnEnter()
{
AddOption("1. Writing a story with Pages", MoveToWriting);
AddOption("2. Controlling the camera with Views", MoveToViews);
AddOption("3. Using Sprites and Animations", MoveToAnimations);
AddOption("4. Playing music and sound effects", MoveToAudio);
Choose("Choose an example");
}
@ -30,4 +32,9 @@ public class MenusRoom : Room
{
MoveToRoom(spritesRoom);
}
void MoveToAudio()
{
MoveToRoom(audioRoom);
}
}

Loading…
Cancel
Save