Audio - VirtueSky/sunflower GitHub Wiki
flowchart LR
AudioManager["Audio Manager"] --Object pulling--> SoundComponent(["Sound Component<br>(bring audio source)"])
SoundData[("Sound Data<br> (contains audio clip)")] --> EventPlaySfx & EventPlayMusic
subgraph Event
EventPlaySfx{Play Sound Fx Event}
EventPlayMusic{Play Music Event}
end
EventPlaySfx & EventPlayMusic --Raise--> AudioManager
Use
- Use ObjectPooling to play audio
AudioManager
- Manage play, pause, resume, stop, finish, volume change of sound
- Attaches the
AudioManager
to the game object contained in the scene. The declarations in the AudioManager will be automatically generated and appended
- If AudioManager is missing declarations, you can reset it to get it back or open the Audio tab in
Magic Panel
to create and drag it in
SoundComponent
- Create a prefab
SoundComponent
SoundComponent contains an AudioSource to control (play) an AudioClip
- Drag the
SoundCompoent
into the createdSoundComponentPool
and attach it to theAudioManager
SoundComponentPool is responsible for spawning/despawning SoundComponent
SoundData
SoundData
containsAudioClip
, andloop
,volume
,fade music
parameters for customization- Create
SoundData
fromMagic Panel
- Note: If you drag more than one
AudioClip
into the list,SoundData
will return a randomAudioClip
in the list
Play Sound FX and Music Background
using UnityEngine;
using VirtueSky.Audio;
public class PlayAudio : MonoBehaviour
{
public PlayMusicEvent playMusicEvent;
public PauseMusicEvent pauseMusicEvent;
public StopMusicEvent stopMusicEvent;
public ResumeMusicEvent resumeMusicEvent;
public PlaySfxEvent playSfxEvent;
public PauseSfxEvent pauseSfxEvent;
public ResumeSfxEvent resumeSfxEvent;
public FinishSfxEvent finishSfxEvent;
public StopSfxEvent stopSfxEvent;
public StopAllSfxEvent stopAllSfxEvent;
public SoundData musicGame;
public SoundData soundPlayer;
private SoundCache sfxCache;
public void PlayMusicGame()
{
playMusicEvent.Raise(musicGame);
}
public void PauseMusicGame()
{
pauseMusicEvent.Raise();
}
public void ResumeMusicGame()
{
resumeMusicEvent.Raise();
}
public void StopMusicGame()
{
stopMusicEvent.Raise();
}
public void PlaySoundPlayer()
{
sfxCache = playSfxEvent.Raise(soundPlayer);
}
public void PauseSoundPlayer()
{
pauseSfxEvent.Raise(sfxCache);
}
public void ResumeSoundPlayer()
{
resumeSfxEvent.Raise(sfxCache);
}
public void FinishSoundPlayer()
{
finishSfxEvent.Raise(sfxCache);
}
public void StopSoundPlayer()
{
stopSfxEvent.Raise(sfxCache);
}
public void StopAllSoundFX()
{
stopAllSfxEvent.Raise();
}
}