SoFunction
Updated on 2025-03-08

unity How to determine which UI the mouse is on (two methods)

The first type

You can get the UI, and then judge whether it is the UI you want to click on based on the name

where parameter canvas is dragged into the canvas of this UI

 /// <summary>
        /// Get the mouse retention UI        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public GameObject GetOverUI(GameObject canvas)
        {
            PointerEventData pointerEventData = new PointerEventData();
             = ;
            GraphicRaycaster gr = <GraphicRaycaster>();
            List<RaycastResult> results = new List<RaycastResult>();
            (pointerEventData, results);
            if ( != 0)
            {
                return results[0].gameObject;
            }
            return null;
        }

The second type is simple

rect RectTransform for the UI to be judged

bool isUI = (rect, )

Supplement: In Unity, determine whether the mouse or finger is clicked on the UI (UGUI)

In the Unity scenario, sometimes both the UI and the game character need to respond to touch events. If you respond at the same time, it may affect the game character when clicking on the UI. So we need to make a judgment on what we clicked. Here we use the methods and ray detection methods provided by the UGUI system to determine whether to click on the UI:

The first method is to directly judge in Update:

void Update()
    {      
        //Judge whether to click on the UI        if ((0) || ( > 0 && (0).phase == ))
        {
            //Mobile terminal            if ( ==  ||
                         == )
            {
                int fingerId = (0).fingerId;
                if ((fingerId))
                {
                    ("Click to UI");                    
                }
            }
            //Other platforms            else
            {
                if (())
                {
                    ("Click to UI");                    
                }
            }
        }

The second method: ray detection

using ;
using ;
using UnityEngine;
using ; 
public class NewBehaviourScript : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //Mobile terminal        if ( ==  ||
                     == )
        {
            if ( > 0 && (0).phase == )
            {
                if (IsPointerOverGameObject((0).position))
                {
                    ("Click to UI");
                }
            }            
        }
        //Other platforms        else
        {
            if((0))
            {
                if (IsPointerOverGameObject())
                {
                    ("Click to UI");
                }
            }            
        }
    }
 
    /// <summary>
    /// Detect whether to click on the UI    /// </summary>
    /// <param name="mousePosition"></param>
    /// <returns></returns>
    private bool IsPointerOverGameObject(Vector2 mousePosition)
    {       
        //Create a click event        PointerEventData eventData = new PointerEventData();
         = mousePosition;
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        //Speed ​​a ray to the click position to detect whether the UI is clicked        (eventData, raycastResults);
        if ( > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.