/* * author: mark joshwel * date: 29/5/2024 * description: common menu script for hover and click sound effects on ui toolkit buttons */ using System; using UnityEngine; using UnityEngine.UIElements; /// /// common menu class for hover and click sound effects /// on ui toolkit buttons. /// override OnEnable() with the first call to base.OnEnable() or PostEnable(), /// and set the variable GameManager.DisplayState associatedState to the respective menu state /// public class CommonMenu : MonoBehaviour { /// /// associated display state with the menu for the game manager to filter out menus in a scene /// public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState; /// /// manager for the game state /// protected GameManager Game; /// /// the visual element object for the menu /// protected VisualElement UI; /// /// checks if The Menu (2022) was set up correctly /// /// throws an exception if UI, Game and Audio are not set private void Start() { if (associatedState == GameManager.DisplayState.UnassociatedState) throw new Exception("CommonMenu: associatedState not set"); if (Game == null) throw new Exception("CommonMenu: Game not set (was base.OnEnable() or PostEnable() called?)"); } /// /// override this class but call base.OnEnable() first. /// also set the associatedState variable to the respective menu state /// public virtual void OnEnable() { PostEnable(); } /// /// function to subscribe to mouse events and assign managers /// public void PostEnable() { // get audio manager singleton instance from the world UI = GetComponent().rootVisualElement; // Audio = AudioManager.Instance; Game = GameManager.Instance; // // subscribe to hover events UI.RegisterCallback(HoverListener); } /// /// function listener for PointerOverEvents and plays a hover sound if it's a button /// /// event from UIE callback public virtual void HoverListener(PointerOverEvent evt) { // check for button if (evt.target is Button) // play hover sound PlayHover(); } /// /// function listener for ClickEvents and plays a click sound if it's a button /// /// event from UIE callback public virtual void ClickListener(ClickEvent evt) { // check for button if (evt.target is Button) // play click sound PlayClick(); } /// /// generic decoupled function to play click sound /// public virtual void PlayClick() { // TODO: play click sound // Game.PlayOnSFXChannel(Audio.menuButtonClick); } /// /// generic decoupled function to play hover sound /// public virtual void PlayHover() { // TODO: play hover sound // Game.PlayOnSFXChannel(Audio.menuButtonHover); } }