Forum rules - please read before posting.

Using Alpha Thresholds for AC mouse hover detection with Unity UI Menus

Hello AC Community,

On my title screen I've attached a simple script to my New Game, Continue and Option buttons that only detects clicks when the pointer is over a non-transparent area of the button - the button graphics aren't rectangular, and we didn't want a user to be able to select an option when they weren't visually clicking on it. This works fine (I'll paste in the script below), and works for Unity's button highlighting too. However, ACs own mouseover detection still works as normal - while the buton doesn't highlight until I'm over the graphic, AC changes to a mouse over cursor as soon as it enters the transparent area; more importantly for me, it plays the menu's hover sound, or both the menus' hover sounds in the result of an overlapping transparent area, of which I have several.

Any ideas on how to fix this greatly appreciated!

(the click detection code below:)
Using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIClickDetection : MonoBehaviour
{

    public float AlphaThreshold = 0.1f;

    void Start()
    {
        this.GetComponent<Image>().alphaHitTestMinimumThreshold = AlphaThreshold;
    }
}

Comments

  • For the purposes of hover sounds / cursor changes, AC detects the active element by reading Unity's EventSystem.currentSelectedGameObject property.

    It sounds like this is being set despite the alpha threshold value - though whether this is a bug, or by design, would be down to Unity.

    I'd suggest unsetting the AC menu audio / cursor options, and try add custom Enter/Exit events for your various Buttons, as covered here.

    For example, to play a sound when hovering over a Button:

    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class CustomHoverSound : MonoBehaviour, IPointerEnterHandler
    {
    
        public AudioClip hoverClip;
    
        public void OnPointerEnter(PointerEventData eventData)
        {
            AC.KickStarter.sceneSettings.defaultSound.audioSource.PlayOneShot (hoverClip);
        }
    
    }
    

    Changing the AC cursor is a bit more tricky - let's see if the above is compatible with your UI first.

  • That works perfectly, thanks Chris - user feedback being consistent throughout the game is really important to us, so if it's possible to elaborate on the script above to get it working with the cursor, that would be fantastic!

  • It should be possible by updating the Main cursor's texture as hover enter/exit the area:

    using AC;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class CustomHoverSound : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {
    
        public AudioClip hoverClip;
        public Texture texture;
        private Texture defaultTexture;
    
        private void Awake ()
        {
            defaultTexture = KickStarter.cursorManager.pointerIcon.texture;
        }
    
        public void OnPointerEnter(PointerEventData eventData)
        {
            KickStarter.sceneSettings.defaultSound.audioSource.PlayOneShot (hoverClip);
            KickStarter.cursorManager.pointerIcon.ReplaceTexture (texture);
        }
    
        public void OnPointerExit(PointerEventData eventData)
        {
            KickStarter.cursorManager.pointerIcon.ReplaceTexture (defaultTexture);
        }
    
    }
    
  • Cool!

    That works exactly how it should now, thanks so much.

    For anyone playing along at home, don't forget, once you've added the script above to your buttons, uncheck "change cursor when over" in the AC Menu Manager for associated button, otherwise AC will still change it in the transparent areas.

  • A small but important wrinkle in this one:

    If the script has changed the cursor text to it's mouse-over variant when the scene changes, that change, which is to the default cursor texture, will persist throughout the rest of the game!

    How does one go about reverting to the (original) default texture in scenes where this functionality isn't required?

    Thanks!
    -M-

  • A separate script can set the cursor's default texture when the scene begins:

    using AC;
    using UnityEngine;
    
    public class SetDefaultTexture : MonoBehaviour
    {
    
        public Texture defaultTexture;
    
        private void Start ()
        {
            KickStarter.cursorManager.pointerIcon.ReplaceTexture (defaultTexture);
        }
    
    }
    
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.