Forum rules - please read before posting.

GUI button Hold Down?

Similar to having an on mouse over and on mouse exit timer on a trigger (ie to create a button down that keeps playing a sound as long as the player is holding down the button and when letting go it stops) - can that be done with a gui button?
So far I can start a sound and stop it when player press button again - but i want to have it when letting go. (I'm using AC gui)
Any direction would be much appreciated.

Comments

  • Your title mentions holding the button down - are you looking to have a sound play the whole time, or only when letting go?

    For pretty much any custom UI behaviour, I'd recommend using Unity UI. However, when using AC menus you can hook into the OnMouseOverMenu event to work out if the pointer is over a particular element:

    using UnityEngine;
    using AC;
    
    public class ClickUpSound : MonoBehaviour
    {
    
        public AudioClip audioToPlay;
    
        public string menuName;
        public string buttonName;
    
        private bool isOverButton;
    
        private void OnEnable () { EventManager.OnMouseOverMenu += OnMouseOverMenu; }
        private void OnDisable () { EventManager.OnMouseOverMenu -= OnMouseOverMenu; }
    
        private void OnMouseOverMenu (AC.Menu menu, MenuElement element, int slot)
        {
            isOverButton = (menu.title == menuName && element != null && element.title == buttonName);
        }
    
        private void Update ()
        {
            if (Input.GetMouseButtonUp (0) && isOverButton)
            {
                KickStarter.sceneSettings.PlayDefaultSound (audioToPlay, false);
            }
        }
    
    }
    
  • edited September 2021

    Thanks Chris.
    Yes, it's when holding down a sound will play - and when letting go it will stop.
    (When pressing a car horn)

  • Something more like this should do it:

    using UnityEngine;
    using AC;
    
    public class HoldButtonSound : MonoBehaviour
    {
    
        public AudioSource audioSource;
    
        public string menuName;
        public string buttonName;
    
        private bool isOverButton;
    
        private void OnEnable () { EventManager.OnMouseOverMenu += OnMouseOverMenu; }
        private void OnDisable () { EventManager.OnMouseOverMenu -= OnMouseOverMenu; }
    
        private void OnMouseOverMenu (AC.Menu menu, MenuElement element, int slot)
        {
            isOverButton = (menu.title == menuName && element != null && element.title == buttonName);
        }
    
        private void Update ()
        {
            if (Input.GetMouseButton (0) && isOverButton && KickStarter.stateHandler.IsInGameplay ())
            {
                if (!audioSource.isPlaying)
                {
                    audioSource.loop = true;
                    audioSource.Play ();
                }
            }
            else
            {
                audioSource.Stop ();
            }
        }
    
    }
    
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.