Forum rules - please read before posting.

How to have sound in custom menu using Unity UI prefab

Hi,
I'm not quite certain if this is a AC issue or not, but I've attempted something pretty simple and I don't know why its not working:

I've created a custom menu using a Unity UI prefab (linked canvas). Everything works well in terms of functionality, but when I try to add a simple AudioSource to the canvas prefab so that it plays on "awake", the sound refuses to play.

What am I missing?

Thanks!

Comments

  • Hi Chris,
    It's possible my initial question was unclear so here's a more specific question:

    I've created a new menu using this tutorial: https://adventurecreator.org/tutorials/creating-inventory-bar-unity-ui

    Now, let's say I added an animation (glow effect) when the inventory bar first appears. How do I add a sound to that?



  • When Unity UI menus are turned on and off, their root GameObject is enabled/disabled - as opposed to added to or removed from the scene.  It may be that this behaviour is not compatible with an AudioSource's Play On Awake option.

    The best way to play audio when a menu appears is to assign an ActionList when turn on asset file within that menu's properties.  Set the asset's When running field to Run In Background, and give it a single Sound: Play Action.

    If you don't specify a Sound object within that Action, it will rely on the Default Sound prefab that can be defined at the top of the Scene Manager.
  • What if the sound is not played when the menu appears but at a later point? Say I have the menu animating and I want to add a SFX?

    I guess I'm trying to understand how it could work a bit more broadly.
  • You can always prefix the Sound: Play Action with an Engine: Wait to delay it.  You can also invoke an ActionList at any time manually by calling its Interact() method.

    For greater control with respect to animation syncing, you could rely on Animation events to run an ActionList asset at a given time.

    Now, Unity's animation events can only be used to invoke methods on components attached to the same Animator object - while ActionLists can only exist as prefabs via separate asset files.  However, you can get around this by doing the following:
    1. Attach a Cutscene component to the same object as your Animator
    2. Set the Cutscene's Actions source field to Asset File
    3. Assign an ActionList asset that does the sound-handling in the Cutscene's ActionList asset field
    4. Create an animation event in your animation timeline that runs the Cutscene's Interact method.
  • Awesome! Thank you Chris!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.