-
Notifications
You must be signed in to change notification settings - Fork 2
Audio
VirtueSky edited this page Sep 8, 2024
·
5 revisions
Support play audio
Use Pool for spawning SoundComponents
(with AudioSource
attached)
- Create
SoundComponent
Prefab
- Attach
AudioManager
to scene to handle play sound FX and Music background (don't destroy)
- Create
SoundData
SoundData
containsAudioClip
, andloop
,volume
,fade music
parameters for customization
Right-click folder > Create
> Unity-Common
> Audio
> SoundData
or create from Magic 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
public SoundData soundFX;
public SoundData music;
private SoundCache _soundCache;
/// <summary>
/// Play sound fx
/// </summary>
public void PlaySoundFX()
{
_soundCache = AudioManager.PlaySfx(soundFX);
// or
_soundCache = soundFX.PlaySfx();
}
/// <summary>
/// Pause sound fx
/// </summary>
public void PauseSoundFX()
{
AudioManager.PauseSfx(_soundCache);
// or
_soundCache.PauseSfx();
}
/// <summary>
/// Resume sound fx
/// </summary>
public void ResumeSoundFX()
{
AudioManager.ResumeSfx(_soundCache);
// or
_soundCache.ResumeSfx();
}
/// <summary>
/// Finish sound fx
/// </summary>
public void FinishSoundFX()
{
AudioManager.FinishSfx(_soundCache);
// or
_soundCache.FinishSfx();
}
/// <summary>
/// Stop sound fx
/// </summary>
public void StopSoundFX()
{
AudioManager.StopSfx(_soundCache);
// or
_soundCache.StopSfx();
}
/// <summary>
/// Stop all sound fx
/// </summary>
public void StopAllSoundFX()
{
AudioManager.StopAllSfx();
}
/// <summary>
/// Play music
/// </summary>
public void PlayMusic()
{
AudioManager.PlayMusic(music);
// or
music.PlayMusic();
}
/// <summary>
/// Pause music
/// </summary>
public void PauseMusic()
{
AudioManager.PauseMusic();
}
/// <summary>
/// Resume music
/// </summary>
public void ResumeMusic()
{
AudioManager.ResumeMusic();
}
/// <summary>
/// Stop music
/// </summary>
public void StopMusic()
{
AudioManager.StopMusic();
}