Skip to content
VirtueSky edited this page Sep 4, 2024 · 7 revisions

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

audio

  • 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

Screenshot 2024-09-04 173633

SoundComponent

  • Create a prefab SoundComponent

SoundComponent contains an AudioSource to control (play) an AudioClip

soundcom

  • Drag the SoundCompoent into the created SoundComponentPool and attach it to the AudioManager

SoundComponentPool is responsible for spawning/despawning SoundComponent

sound pool

SoundData

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

sound_data

  • 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 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();
    }
}
Clone this wiki locally