UI To Block Raycasts

Does anyone know how to prevent the OnMouseOver() from firing? I tried adding a 2d box collider but it didn’t stay with the button I made. this is my simple tester script

public class HoverTest : MonoBehaviour {

bool hover = false;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if(hover) {
            renderer.material.color = Color.yellow;
            hover = false;    //reset for next frame
        }
        else {
            renderer.material.color = Color.white;
        }
    }
   
    void OnMouseOver() {
        hover = true;
       
    }
}

Its the same with a simple raycast using

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
        if(Physics.Raycast(ray, out hitInfo)) {
        if(hitInfo.transform == transform) {
            hover = true;
            }
        }

I’ve been trying to figure this out as well…

In the old/current GUI, you can keep tabs on the “hotcontrol” to see if the mouse is over a GUI component in conjunction with the raycast hit test.

I’m not finding anything that looks like it fits the bill here.

So this led me on the right path: Raycast into GUI? - Unity Engine - Unity Discussions

Not sure if its what you were looking for, but I was simply trying to avoid ray casting on mouse click when intending to click a ui button.

In order to call the method, you need to grab the EventSystem component that was added into your scene when you created the canvas.

I guess to prevent the OnMouseOver… you could check in the first line…

private UnityEngine.EventSystems.EventSystem _eventSystem;

void Start () {
    _eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
}

void OnMouseOver () {
    if(_eventSystem.IsPointerOverEventSystemObject()) {
        // we're over a UI element... peace out
        return;
    }
}
2 Likes

I believe you can use EventSystemManager.currentSystem instead of finding and storing the EventSystem in Start().

3 Likes

Ah - didn’t realize there was a singleton. Good find!

Be sure to also check out the CanvasGroup component if you wish to block raycasts, either to the background scene or other UI compnents.
Will G gave a pretty good demonstration of it in his UI introduction video

thanks @tstpierre_nss ! That should work with raycasts too. In the demo ui code, I found this implementing the IPointerClickHandler interface.

public void OnPointerClick(PointerEventData data)
    {
        if (GetComponent<Renderer>() != null)
            GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
        else if (GetComponent<Light>() != null)
            GetComponent<Light>().color = new Color(Random.value, Random.value, Random.value, 1.0f);
    }

Which seems to replace OnMouseDown.

1 Like

For future reference, it is now

UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()

Yes, there is the new ICanvasRaycastFilter interface where you can define how your object reacts to the above call. We updated all of the primitive controls in the UIExtensions project to make use of this.

So now you can override if the IsPointerOverGameObject will return true or not per object.

1 Like