How does Unity determine whether the mouse is on which UI (two methods)

  • The first

     可以得到UI,再根据名字判断是不是自己自己要点击的UI
     其中参数canvas拖入此UI的canvas
    
    	/// <summary>
        /// 获取鼠标停留处UI
        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public GameObject GetOverUI(GameObject canvas)
        {
          
          
            PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
            pointerEventData.position = Input.mousePosition;
            GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
            List<RaycastResult> results = new List<RaycastResult>();
            gr.Raycast(pointerEventData, results);
            if (results.Count != 0)
            {
          
          
                return results[0].gameObject;
            }
            return null;
        }
    
    
  • the second

     rect 为要判断的那个UI的RectTransform
    
    bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)
    

    Supplement: Judging whether the mouse or finger is clicked on the UI in Unity (UGUI)

    In the Unity scene, sometimes both the UI and the game character need to respond to touch events. If they respond at the same time, the game character may be affected when the UI is clicked. So we need to make a judgment on what we clicked.
    Here we use the method of UGUI system and the way of ray detection to judge whether to click on the UI:

  • The first method is to judge directly in Update:

    void Update()
        {
          
                
            //判断是否点击UI
            if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
            {
          
          
                //移动端
                if (Application.platform == RuntimePlatform.Android ||
                            Application.platform == RuntimePlatform.IPhonePlayer)
                {
          
          
                    int fingerId = Input.GetTouch(0).fingerId;
                    if (EventSystem.current.IsPointerOverGameObject(fingerId))
                    {
          
          
                        Debug.Log("点击到UI");                    
                    }
                }
                //其它平台
                else
                {
          
          
                    if (EventSystem.current.IsPointerOverGameObject())
                    {
          
          
                        Debug.Log("点击到UI");                    
                    }
                }
            }
    
    
  • The second way: ray detection

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems; 
    public class NewBehaviourScript : MonoBehaviour
    {
          
          
        // Update is called once per frame
        void Update()
        {
          
          
            //移动端
            if (Application.platform == RuntimePlatform.Android ||
                        Application.platform == RuntimePlatform.IPhonePlayer)
            {
          
          
                if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
          
          
                    if (IsPointerOverGameObject(Input.GetTouch(0).position))
                    {
          
          
                        Debug.Log("点击到UI");
                    }
                }            
            }
            //其它平台
            else
            {
          
          
                if(Input.GetMouseButtonDown(0))
                {
          
          
                    if (IsPointerOverGameObject(Input.mousePosition))
                    {
          
          
                        Debug.Log("点击到UI");
                    }
                }            
            }
        }
     
        /// <summary>
        /// 检测是否点击UI
        /// </summary>
        /// <param name="mousePosition"></param>
        /// <returns></returns>
        private bool IsPointerOverGameObject(Vector2 mousePosition)
        {
          
                 
            //创建一个点击事件
            PointerEventData eventData = new PointerEventData(EventSystem.current);
            eventData.position = mousePosition;
            List<RaycastResult> raycastResults = new List<RaycastResult>();
            //向点击位置发射一条射线,检测是否点击UI
            EventSystem.current.RaycastAll(eventData, raycastResults);
            if (raycastResults.Count > 0)
            {
          
          
                return true;
            }
            else
            {
          
          
                return false;
            }
        }
    }
    

Guess you like

Origin blog.csdn.net/qq_39735878/article/details/127243559