EzSoundCore

PRs Welcome License: MIT

EzSoundCore is a Unity package that provides a simple and efficient way to manage sound effects and music playback in your game.

View in DocFx

Features

  • Easy-to-use methods for playing sounds
  • Provides a quick and easy way to manage multiple audio sources
  • Support for audio mixer snapshots
  • Flexible utility for generating unique IDs from audio clips

Installation

Inside the Unity Editor using the Package Manager:

  • Click the (+) button in the Package Manager and select "Add package from git URL" (requires Unity 2019.4 or later).
  • Paste the git URL of this package into the input box: https://github.com/ebukaracer/EzSoundCore.git#upm
  • Click Add to install the package.
  • If your project uses Assembly Definitions, make sure to add a reference to this package under Assembly Definition References.

Dependencies

Name Version Package URL How to Install from Package Manager
EzUtilities.Common 1.0.0+ https://github.com/ebukaracer/EzUtilities.git#common Select Install package from git URL and paste the package url inside the box

Setup

After installation, use the menu options in the following order:

  • Racer > EzSoundCore > Import Elements to load the prebuilt elements of this package, including the SoundCore prefab
  • Racer > EzSoundCore > Setup > Add SoundCore Prefab to Scene to add the SoundCore GameObject required for playing sounds.
  • Optionally use Racer > EzSoundCore > Setup > Create AudioClipRegistry to generate a registry ScriptableObject for various audio clip operations.

Usage Examples

Firstly, ensure that the SoundCore prefab or a GameObject containing the component is present in the scene. Thereafter, you can use SoundCore.Instance to access the singleton instance of that class.

Playing Sound by Default

using UnityEngine;
using Racer.EzSoundCore.Core;

public class DefaultUsage : MonoBehaviour
{
    [SerializeField] private AudioClip sfxClip;
    [SerializeField] private AudioClip musicClip;

    private void Start()
    {
        // Play a sound effect
        SoundCore.Instance.PlaySfx(sfxClip, volumeScale: 0.8f);

        // Play music
        SoundCore.Instance.PlayMusic(musicClip);
    }
}

Playing Sound Using ClipID

In the AudioClipRegistry located in the Resources folder, use the available buttons to search for audio clips within your project. For each clip you find, generate an enum entry. Afterwards, you can play sounds using the ClipID parameter, as illustrated below:

using UnityEngine;
using Racer.EzSoundCore.Core;

public class ClipIDUsage : MonoBehaviour
{
    [SerializeField] private AudioClip sfxClip;
    [SerializeField] private AudioClip musicClip;

    private void Start()
    {
    		// Play a sound effect using ClipID(if generated)  
    		SoundCore.Instance.PlaySfx(ClipID.Explosion, volumeScale: 0.8f);  
    	  
    		// Play music using ClipID (if generated) 
    		SoundCore.Instance.PlayMusic(ClipID.BackgroundMusic);
    }
}

Other Operations

using UnityEngine;
using Racer.EzSoundCore.Core;

public class OtherOperations : MonoBehaviour
{
    private void Start()
    {
        // Enable all audio sources
        SoundCore.Instance.EnableAllSources(enable: true, ignoreIndex: true);

        // Transition to a specific audio mixer snapshot
        SoundCore.Instance.MuteAudioMixer(snapshotIndex: 0, timeToReach: 0.5f);
    }
}

Samples and Best Practices

  • In the case of any updates to newer versions, use the menu option: Racer > EzSoundCore > Import Elements (force)
  • Optionally import this package's demo from the package manager's Samples tab.
  • To remove this package completely (leaving no trace), navigate to: Racer > EzSoundCore > Remove package

Contributing

Contributions are welcome! Please open an issue or submit a pull request.