QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

查看: 4656|回复: 0

[C#/.NET] unity如何判断鼠标是否在哪个UI上(两种方法)

[复制链接]

等级头衔

积分成就    金币 : 2857
   泡泡 : 1516
   精华 : 6
   在线时间 : 1316 小时
   最后登录 : 2025-2-16

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2021-4-10 22:14:02 | 显示全部楼层 |阅读模式
第一种
( c$ z6 V. K; B! ]3 y       可以得到UI,再根据名字判断是不是自己自己要点击的UI,其中参数canvas拖入此UI的canvas- H) E$ ~6 m4 I3 a# R$ _1 f
/// <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;
        }
第二种& N  x- c# p- L! F) e  j
       rect 为要判断的那个UI的RectTransform% a7 m( u4 A" b1 P1 }
bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)
补充:Unity中判断鼠标或者手指是否点击在UI上(UGUI)
0 `- ~( d& A, C1 K4 x  L/ E% N& s- a- j6 P       在Unity场景中,有时UI和游戏角色都需要响应触摸事件,如果同时响应可能就会出现点击UI的时候影响到了游戏角色。所以我们需要对所点击到的东西做判断,这里使用UGUI系统自带的方法和射线检测的方式,判断是否点击到UI上:
. h* |, m* g, R$ ^. t: }第一种方法,直接在Update中判断:
- y7 v' Z0 a; O$ E4 L% |/ V) w
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");                    
                }
            }
        }
第二种方式:射线检测
4 m4 @4 N8 P8 ?3 H7 \6 C+ |
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;
        }
    }
}

/ [9 e" x4 J- G/ L5 c) \9 e4 c1 t0 Z& V
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2025-2-19 06:48

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表