我是靠谱客的博主 无限墨镜,这篇文章主要介绍unity 如何判断鼠标是否在哪个UI上(两种方法),现在分享给大家,希望可以做个参考。

第一种

可以得到UI,再根据名字判断是不是自己自己要点击的UI

其中参数canvas拖入此UI的canvas

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <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; }

第二种就简单了

rect 为要判断的那个UI的RectTransform

复制代码
1
bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)

补充:Unity中判断鼠标或者手指是否点击在UI上(UGUI)

在Unity场景中,有时UI和游戏角色都需要响应触摸事件,如果同时响应可能就会出现点击UI的时候影响到了游戏角色。所以我们需要对所点击到的东西做判断,这里使用UGUI系统自带的方法和射线检测的方式,判断是否点击到UI上:

第一种方法,直接在Update中判断:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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"); } } }

第二种方式:射线检测

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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; } } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。如有错误或未考虑完全的地方,望不吝赐教。

最后

以上就是无限墨镜最近收集整理的关于unity 如何判断鼠标是否在哪个UI上(两种方法)的全部内容,更多相关unity内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部