How to play audio on usage?

When an item is used, I would like to play an audio clip. Before I get to writing custom code to accomplish this, what is the recommended way within the UIS framework?
 
Depending if you wish to have a generic sound for all items or a specific sound per action or per item then the process could be a bit different.

For a generic sound you could play the sound when the button is clicked. You can listen to the button click event to do so

For a sound per action you could write the code for it in the Item Action script itself.

For a sound per item, you can attach a Audio clip as an ItemDefinition attribute, fetch it in the item action and play it.

Item Actions are simple classes so you'll need a way to access an AudioSource from them somehow. The way I like to do this is with an AudioManager singleton which lets me spawn pooled Audio Sources at any point in space.

Looking at the UIS code I found an extremely simple static class called AudioManager that lets you play a sound anywhere... it doesn't pool the audio source gameobject though. you can use that for now to get started quickly and change it later with your own solution if you want.

This is completely unrelated to UIS but I found the audio manager structure the devs used in the Unity Open Projects was well thought out. Perhaps it may interest you (it may be overcomplicated depending on your use case though)
 
Be careful when changing source code, if you upgrade to a new UIS version your changes will be overridden.
It is recommended to write a custom base Item Action and have your other custom Item Action inherit that new base custom Item Action
 
Top