Forum rules - please read before posting.

Using Custom Script for Menu Element sounds?

[Unity 2019.4.34f1 - AC v1.74.5]

Hello!

Most of my Menus are Unity UI Menus, and most Elements have Custom Scripts to regulate their Click type. Therefore, in my Custom Scripts I tried using the OnSelect \ OnSubmit interfaces implementations to avoid having to manually set the hover and click sounds on Buttons through the Menu manager and avoid having to create a Sound object in each Scene.

But it seems like my script gets executed before AC's UISlotClick, which also plays sounds in its OnSelect implementation of the ISelectHandler interface and, even though the Click Sound and Hover Sound fields in the Element properties have no reference to any sound file it still seems to it it basically "cancel" my own AudioClip. Is there any way to "override" sound triggering in Menus?

I'm using Audio Mixers and I also tried handling my UI sounds on a different "channel" altogether, untouched by AC's SFX, but to no avail.

To be fair, though, the hover and click sounds don't actually seem to play even when I do use a Sound object in the Scene and have referenced an AudioClip in the Click Sound and Hover Sound fields, but for that I blame my hybrid implementation of the "legacy" Input System and the New Input System, so I didn't really investigate further.

Another reason for me to say that is that, if I simply create a new AC Scene, with a Unity UI not registered in AC, and that has buttons with just my own scripts attached, the sound and navigation works just fine.

As always, thanks in advance!

Comments

  • Hover/Click sounds are played through the Scene Manager's "Default Sound" field - AC connects the two automatically. If none is assigned, then audio won't play and you'll get a warning in the Console.

    If audio is played, then it does so using PlayOneShot, so I'm not sure how it might be "cancelling" or interfering with your own playback.

    If you're looking to override both the click behaviour and the audio of your Buttons, though, is there a need to connect them to AC? A UI Button component can still reference AC when clicked through a custom OnClick event, even if the Button itself is not linked to an AC Button element.

  • edited February 2022

    I apologize: I did actually set the Scene Manager's "Default Sound" field to refer to the Sound object, but I did forget to tick the "Play while game paused" box!

    And related to that, looking better inside the code I realized that indeed the UISlotClick should not be responsible, as it null checks the AudioClip before doing anything, but it's the fact that the Menu pauses the game when enabled. When the Menu doesn't pause the game, I can here sound from my implementation just fine.

    If you're looking to override both the click behaviour and the audio of your Buttons, though, is there a need to connect them to AC?

    You are, again, right. Thing is: I do still rely on AC's own Menu system for a lot of things that make my life easier in lots of ways :smile: it's more because of laziness and rapid iteration that I often end up wanting to have everything handled via script!

    I guess (I have yet to try) one easy middle-ground compromise would be to have a "singleton" Sound component that simply assigns itself to the AC.KickStarter.sceneSettings.defaultSound upon scene change. I would still have to manually assign sounds to each button, but it's at least half of the work!

    EDIT: the above workaround seems to be working? Hope I'm not celebrating to soon!

  • edited February 2022

    I've also just realized I could execute AC.KickStarter.sceneSettings.PlayDefaultSound(theAudioClipToPlay, false); from basically anywhere!

    This means I can include it in my OnSelect \ OnSubmit implementations.

  • FWIW, the Sound object's "Play while game paused?" option is really an alias of the AudioSource component's ignoreListenerPause option. If you were using your own AudioSource to play sound from while the game is paused, you'd have to set this manually to true.

  • Thanks a lot, Chris!

    This is extremely helpful as I was also completely unaware of ignoreListenerPause.

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.