QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

查看: 4234|回复: 0

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

[复制链接]

等级头衔

积分成就    金币 : 2810
   泡泡 : 1516
   精华 : 6
   在线时间 : 1245 小时
   最后登录 : 2024-5-17

丰功伟绩

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

联系方式
发表于 2021-4-10 22:14:02 | 显示全部楼层 |阅读模式
第一种
% i9 @5 g" I: P# U5 m       可以得到UI,再根据名字判断是不是自己自己要点击的UI,其中参数canvas拖入此UI的canvas
; N3 ^* P' X( ]" p
  1. /// <summary>
  2.         /// 获取鼠标停留处UI
  3.         /// </summary>
  4.         /// <param name="canvas"></param>
  5.         /// <returns></returns>
  6.         public GameObject GetOverUI(GameObject canvas)
  7.         {
  8.             PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
  9.             pointerEventData.position = Input.mousePosition;
  10.             GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
  11.             List<RaycastResult> results = new List<RaycastResult>();
  12.             gr.Raycast(pointerEventData, results);
  13.             if (results.Count != 0)
  14.             {
  15.                 return results[0].gameObject;
  16.             }
  17.             return null;
  18.         }
第二种
2 |" V( t! J! k! B       rect 为要判断的那个UI的RectTransform# f0 \" Z6 t0 D9 ~, G3 D" H
  1. bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)
补充:Unity中判断鼠标或者手指是否点击在UI上(UGUI); c9 y; ]! p  e  `" B; |
       在Unity场景中,有时UI和游戏角色都需要响应触摸事件,如果同时响应可能就会出现点击UI的时候影响到了游戏角色。所以我们需要对所点击到的东西做判断,这里使用UGUI系统自带的方法和射线检测的方式,判断是否点击到UI上:, y3 \, E* k" x$ _# v) z+ J$ h3 K" M
第一种方法,直接在Update中判断:
% U$ I- x& O/ L4 T' s
  1. void Update()
  2.     {      
  3.         //判断是否点击UI
  4.         if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
  5.         {
  6.             //移动端
  7.             if (Application.platform == RuntimePlatform.Android ||
  8.                         Application.platform == RuntimePlatform.IPhonePlayer)
  9.             {
  10.                 int fingerId = Input.GetTouch(0).fingerId;
  11.                 if (EventSystem.current.IsPointerOverGameObject(fingerId))
  12.                 {
  13.                     Debug.Log("点击到UI");                    
  14.                 }
  15.             }
  16.             //其它平台
  17.             else
  18.             {
  19.                 if (EventSystem.current.IsPointerOverGameObject())
  20.                 {
  21.                     Debug.Log("点击到UI");                    
  22.                 }
  23.             }
  24.         }
第二种方式:射线检测) h; G7 t* M5 I- @! l7 p& Y- G
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class NewBehaviourScript : MonoBehaviour
  6. {
  7.     // Update is called once per frame
  8.     void Update()
  9.     {
  10.         //移动端
  11.         if (Application.platform == RuntimePlatform.Android ||
  12.                     Application.platform == RuntimePlatform.IPhonePlayer)
  13.         {
  14.             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  15.             {
  16.                 if (IsPointerOverGameObject(Input.GetTouch(0).position))
  17.                 {
  18.                     Debug.Log("点击到UI");
  19.                 }
  20.             }            
  21.         }
  22.         //其它平台
  23.         else
  24.         {
  25.             if(Input.GetMouseButtonDown(0))
  26.             {
  27.                 if (IsPointerOverGameObject(Input.mousePosition))
  28.                 {
  29.                     Debug.Log("点击到UI");
  30.                 }
  31.             }            
  32.         }
  33.     }
  34.     /// <summary>
  35.     /// 检测是否点击UI
  36.     /// </summary>
  37.     /// <param name="mousePosition"></param>
  38.     /// <returns></returns>
  39.     private bool IsPointerOverGameObject(Vector2 mousePosition)
  40.     {      
  41.         //创建一个点击事件
  42.         PointerEventData eventData = new PointerEventData(EventSystem.current);
  43.         eventData.position = mousePosition;
  44.         List<RaycastResult> raycastResults = new List<RaycastResult>();
  45.         //向点击位置发射一条射线,检测是否点击UI
  46.         EventSystem.current.RaycastAll(eventData, raycastResults);
  47.         if (raycastResults.Count > 0)
  48.         {
  49.             return true;
  50.         }
  51.         else
  52.         {
  53.             return false;
  54.         }
  55.     }
  56. }
1 m/ u8 d1 D/ z

% p  D5 [0 [$ ?2 y% _! y
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-18 17:20

Powered by paopaomj X3.4 © 2016-2024 sitemap

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