Code Monkey home page Code Monkey logo

audiomanagerunity's Introduction

Unity AudioManager Singleton

I've been using this singleton for a couple of XR projects based in Unity, figured I should keep updating it here. I referenced a few existing audio manager scripts out there, particularly this logic from CodeMonkey.

How to Use

  1. Setup:

    • Create an empty AudioManager GameObject and attach the script to it.
    • Define sound clips in the SoundClips enum list.
    • Create an Audio Mixer according to your sounds and the way you like to setup your mix.
    • In the inspector, populate the audioClipArray with AudioClipToPlay instances, associating each sound clip with its corresponding audio clip and audio mixer group.
  2. Play Sounds:

    • Call PlaySound3D or PlaySound2D in a script attached to a GameObject to play 3D or 2D sounds, respectively.
    • Provide the appropriate parameters such as sound clip type, pitch, volume, spatial blend, spread, and loop flag.
  3. Stop Sounds:

    • Call StopSound to stop a currently playing sound based on the specified sound clip.
  4. Fade In/Out:

    • Use the FadeInSound and FadeOutSound methods for implementing sound fading.

Example Usage

// Play a 3D sound at the object's position with default settings
// Note - "ButtonClick" will be a sound from the SoundClips enum
AudioManager.PlaySound3D(AudioManager.SoundClips.ButtonClick, playerTransform, 1f, 1f, 0f, false, true, 1f);

// Play a 2D looping sound with custom pitch and volume
AudioManager.PlaySound2D(AudioManager.SoundClips.RoomAmbience, 0.8f, 0.5f, true, false, 0f);

// Stop the RoomAmbience sound
AudioManager.StopSound(AudioManager.SoundClips.RoomAmbience);

// Fade Out sound
StartCoroutine(FadeOutSound(AudioManager.SoundClips.RoomAmbience, 3f));

Capture 4

Future

I know this can be optimized even further, probably by using object pooling instead of creating a new GameObject for each sound, if you have a lot of sounds playing frequently. I'll try adding this in the future either as a separate script or integrated into the Audio Manager.

audiomanagerunity's People

Contributors

ashaydave avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.