Trigger Sounds when opening/closing Dropdown

We’re currently adding audio to various UI events. The easy ones are those where there is an interface we can implement, like IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler. For having different sounds for checking/unchecking a Toggle, we use what I find a bit of a hack: We have a method OnValueChanged(bool) that we register with the Toggle component in OnEnable, de-register in OnDisable and play the different sounds in the method.

Dropdowns have one rather obvious event that needs a sound (and actually also animation) but I’m not quite sure how to handle this one. One possibility might be writing our own Dropdown that inherits from the Unity dropdown, where we override CreateDropDownList() and DestroyDropDownList(). Basically, this should work by calling base.Create/DestroyDropDownList() and then triggering our sounds.

But that, IMHO, is definitely quite a hack … so I was wondering: Is there a better way?

When you create a new Dropdown it comes preloaded with a lot of children UI elements that make up the actual Dropdown menu portion. OnValueChanged for the initial Dropdown only activates when you select a new value but these children UI elements have a little bit more going on with them:

  • Dropdown > Template > OnValueChanged triggers whenever you scroll the scrollbar.

  • Dropdown > Template > Viewport > Content > Item > OnValueChanged triggers whenever you open the dropdown, but for some reason it will trigger twice most of the time and as far as I can tell its random whether it does so.

  • Dropdown > Template > Scrollbar > OnValueChanged triggers whenever you open the dropdown OR scroll the scrollbar.

  • I couldn’t find a single OnValueChanged which triggers when you exit the dropdown either by selecting the same option or just pressing the initial button again.

The sound I’m working with works for both the scrollbar and the dropdown open but if you need them to be different I’m afraid there isn’t a good answer. I’m using Unity 5.6.0f3 so maybe what I can only assume is a bug with the Item OnValueChanges is fixed in the future. I’m not the most experienced Unity developer myself so I’m not entirely sure why these interactions work as they do. I merely experimented and wanted to give a weak answer so that a more seasoned Unity veteran could fill in the gaps.