Skip to content
VirtueSky edited this page Sep 8, 2024 · 5 revisions

What

Support play audio

Use Pool for spawning SoundComponents (with AudioSource attached)

Use

  • Create SoundComponent Prefab

Screenshot 2024-05-17 105818

  • Attach AudioManager to scene to handle play sound FX and Music background (don't destroy)

Screenshot 2024-05-17 105718

  • Create SoundData SoundData contains AudioClip, and loop, volume, fade music parameters for customization

Right-click folder > Create > Unity-Common > Audio > SoundData or create from Magic Panel

Screenshot 2024-05-17 110442

Screenshot 2024-05-17 111105

  • Note: If you drag more than one AudioClip into the list, SoundData will return a random AudioClip 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();
    }
Clone this wiki locally