Search Unity

UI To Block Raycasts

Discussion in 'UGUI & TextMesh Pro' started by judah4, Aug 20, 2014.

  1. judah4

    judah4

    Joined:
    Feb 6, 2011
    Posts:
    256
    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
    Code (CSharp):
    1. public class HoverTest : MonoBehaviour {
    2.  
    3. bool hover = false;
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.         if(hover) {
    12.             renderer.material.color = Color.yellow;
    13.             hover = false;    //reset for next frame
    14.         }
    15.         else {
    16.             renderer.material.color = Color.white;
    17.         }
    18.     }
    19.    
    20.     void OnMouseOver() {
    21.         hover = true;
    22.        
    23.     }
    24. }
    Its the same with a simple raycast using
    Code (CSharp):
    1. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2.     RaycastHit hitInfo;
    3.         if(Physics.Raycast(ray, out hitInfo)) {
    4.         if(hitInfo.transform == transform) {
    5.             hover = true;
    6.             }
    7.         }
     
  2. tstpierre_nss

    tstpierre_nss

    Joined:
    Jul 3, 2012
    Posts:
    118
    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.
     
  3. tstpierre_nss

    tstpierre_nss

    Joined:
    Jul 3, 2012
    Posts:
    118
    So this led me on the right path: http://forum.unity3d.com/threads/raycast-into-gui.263397/#post-1742031

    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.
     
  4. tstpierre_nss

    tstpierre_nss

    Joined:
    Jul 3, 2012
    Posts:
    118
    I guess to prevent the OnMouseOver... you could check in the first line...


    Code (csharp):
    1.  
    2. private UnityEngine.EventSystems.EventSystem _eventSystem;
    3.  
    4. void Start () {
    5.     _eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
    6. }
    7.  
    8. void OnMouseOver () {
    9.     if(_eventSystem.IsPointerOverEventSystemObject()) {
    10.         // we're over a UI element... peace out
    11.         return;
    12.     }
    13. }
    14.  
     
    ben-rasooli and judah4 like this.
  5. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    I believe you can use EventSystemManager.currentSystem instead of finding and storing the EventSystem in Start().
     
    AndrewGrayGames, judah4 and Tim-C like this.
  6. tstpierre_nss

    tstpierre_nss

    Joined:
    Jul 3, 2012
    Posts:
    118
    Ah - didn't realize there was a singleton. Good find!
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    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
     
  8. judah4

    judah4

    Joined:
    Feb 6, 2011
    Posts:
    256
    thanks @tstpierre_nss ! That should work with raycasts too. In the demo ui code, I found this implementing the IPointerClickHandler interface.

    Code (CSharp):
    1. public void OnPointerClick(PointerEventData data)
    2.     {
    3.         if (GetComponent<Renderer>() != null)
    4.             GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
    5.         else if (GetComponent<Light>() != null)
    6.             GetComponent<Light>().color = new Color(Random.value, Random.value, Random.value, 1.0f);
    7.     }
    Which seems to replace OnMouseDown.
     
    Last edited: Aug 21, 2014
    Tim-C likes this.
  9. RedGolpe

    RedGolpe

    Joined:
    Sep 26, 2015
    Posts:
    2
    For future reference, it is now
    Code (csharp):
    1. UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()
     
  10. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    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.
     
    TrickyHandz likes this.