Unity UGUI — 鼠标穿透UI问题(Unity官方的解决方法)

简述

最近在用UGUI的时候遇到了鼠标穿透的问题,就是说在UGUI和3D场景混合的情况下,点击UI区域同时也会 触发3D中物体的鼠标事件。比如下图中

这里给Cube加了一个鼠标点击改变颜色的代码,如下

1
2
3
4
5
6
7
void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
    }
}

运行一下,会发现只要有鼠标点击(任何位置点击),Cube的颜色就会改变,根据代码我们知道这也是必然的,但是问题是如果Cube是一个3D世界中的mesh或者terrain,而button是UI的话也同样会出现同样的问题。

在游戏开发中我们的UI是始终出现在屏幕的,如果在一个战斗场景中用户点了UI战斗场景中的物体也会作出响应肯定是有问题的!

其实关于这个问题网上有不少解决方法了,但是总感觉没有一个是适合我的需求,或者说没有一个最好的答案。

其中提到最多的是利用EventSystem.current.IsPointerOverGameObject()来判断,这个方法的意义是判断鼠标是否点到了GameObject上面,这个GameObject包括UI也包括3D世界中的任何物体,所以他只能判断用户是都点到了东西。对于本文中的问题意义不是很大。那么这个问题到底该怎么解决呢?

 

原理

 

解决方法最终还是离不开射线检测,不过UGUI中已经封装了针对UI部分的射线碰撞的功能,那就是GraphicRaycaster类。里面有个Raycast方法如下,最终就是将射线碰撞到的点添加进resultAppendList数组。

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
       {
           if (canvas == null)
               return;
 
           // Convert to view space
           Vector2 pos;
           if (eventCamera == null)
               pos = new Vector2(eventData.position.x / Screen.width, eventData.position.y / Screen.height);
           else
               pos = eventCamera.ScreenToViewportPoint(eventData.position);
 
           // If it's outside the camera's viewport, do nothing
           if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f)
               return;
 
           float hitDistance = float.MaxValue;
 
           Ray ray = new Ray();
 
           if (eventCamera != null)
               ray = eventCamera.ScreenPointToRay(eventData.position);
 
           if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && blockingObjects != BlockingObjects.None)
           {
               float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
 
               if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All)
               {
                   RaycastHit hit;
                   if (Physics.Raycast(ray, out hit, dist, m_BlockingMask))
                   {
                       hitDistance = hit.distance;
                   }
               }
 
               if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All)
               {
                   RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, dist, m_BlockingMask);
 
                   if (hit.collider != null)
                   {
                       hitDistance = hit.fraction * dist;
                   }
               }
           }
 
           m_RaycastResults.Clear();
           Raycast(canvas, eventCamera, eventData.position, m_RaycastResults);
 
           for (var index = 0; index < m_RaycastResults.Count; index++)
           {
               var go = m_RaycastResults[index].gameObject;
               bool appendGraphic = true;
 
               if (ignoreReversedGraphics)
               {
                   if (eventCamera == null)
                   {
                       // If we dont have a camera we know that we should always be facing forward
                       var dir = go.transform.rotation * Vector3.forward;
                       appendGraphic = Vector3.Dot(Vector3.forward, dir) > 0;
                   }
                   else
                   {
                       // If we have a camera compare the direction against the cameras forward.
                       var cameraFoward = eventCamera.transform.rotation * Vector3.forward;
                       var dir = go.transform.rotation * Vector3.forward;
                       appendGraphic = Vector3.Dot(cameraFoward, dir) > 0;
                   }
               }
 
               if (appendGraphic)
               {
                   float distance = 0;
 
                   if (eventCamera == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay)
                       distance = 0;
                   else
                   {
                       // http://geomalgorithms.com/a06-_intersect-2.html
                       distance = (Vector3.Dot(go.transform.forward, go.transform.position - ray.origin) / Vector3.Dot(go.transform.forward, ray.direction));
 
                       // Check to see if the go is behind the camera.
                       if (distance < 0)
                           continue;
                   }
 
                   if (distance >= hitDistance)
                       continue;
 
                   var castResult = new RaycastResult
                   {
                       gameObject = go,
                       module = this,
                       distance = distance,
                       index = resultAppendList.Count,
                       depth = m_RaycastResults[index].depth,
                       sortingLayer =  canvas.sortingLayerID,
                       sortingOrder = canvas.sortingOrder
                   };
                   resultAppendList.Add(castResult);
               }
           }
       }

从这个方法开始深入查看Unity UGUI源码你会发现,其实每个组件在创建的时候已经被添加进了一个公共列表,UGUI 源码中的GraphicRegistry类就是专门干这件事的。再看下Graphic类中的OnEnable方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    protected override void OnEnable()
        {
            base.OnEnable();
            CacheCanvas();
            GraphicRegistry.RegisterGraphicForCanvas(canvas, this);
 
#if UNITY_EDITOR
            GraphicRebuildTracker.TrackGraphic(this);
#endif
            if (s_WhiteTexture == null)
                s_WhiteTexture = Texture2D.whiteTexture;
 
            SetAllDirty();
 
            SendGraphicEnabledDisabled();
        }

看这句GraphicRegistry.RegisterGraphicForCanvas(canvas, this);就是注册需要做射线检测的UI组件。再看他内部是如何工作的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void RegisterGraphicForCanvas(Canvas c, Graphic graphic)
       {
           if (c == null)
               return;
 
           IndexedSet<Graphic> graphics;
           instance.m_Graphics.TryGetValue(c, out graphics);
 
           if (graphics != null)
           {
               graphics.Add(graphic);
               return;
           }
 
           graphics = new IndexedSet<Graphic>();
           graphics.Add(graphic);
           instance.m_Graphics.Add(c, graphics);
       }

不过,问题又来了,为什么是添加进列表的对象都是Graphic类型呢?这跟ScrollRect,Button,Slider这些有关吗?其实,这就跟UGUI的类继承关系有关了,其实我们使用的UGUI中的每个组件都是继承自Graphic或者依赖一个继承自Graphic的组件

看一下UGUI的类层次结构就会一目了然,如下

看图就会更加清楚,在这我们可以把我们用到的UGUI的所有组件分为两类,1.是直接继承自Graphic的组件。2.是依赖于1的组件"[RequireComponent(typeof(Griphic))]",仔细想想会发现,所有组件都属于这两种中的某一种。

所以对所有Graphic进行Raycast其实就相当于对所有UI组件进行Raycast。

结合上面的知识所以,解决这个问题最好的方法是根据,UGUI的射线碰撞来做。这样会比较合理。

 

解决方案

 

这里我们直接在使用Input.GetMouseButtonDown(0)的地方加了一个检测函数,CheckGuiRaycastObjects,如下

1
2
3
4
5
6
7
8
9
10
11
bool CheckGuiRaycastObjects()
  {
      PointerEventData eventData = new PointerEventData(Main.Instance.eventSystem);
      eventData.pressPosition = Input.mousePosition;
      eventData.position = Input.mousePosition;
 
      List<RaycastResult> list = new List<RaycastResult>();
      Main.Instance.graphicRaycaster.Raycast(eventData, list);
      //Debug.Log(list.Count);
      return list.Count > 0;
  }

不过在使用时需要先获取两个加粗显示的变量,graphicRaycaster和eventSystem。

这两个变量分别对应的是Canvas中的GraphicRaycaster组件,和创建UI时自动生成的“EventSystem”中的EventSystem组件,用的是自己指定一下就可以。

然后在使用的时候可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Update ()
   {
       if (CheckGuiRaycastObjects()) return;
       //Debug.Log(EventSystem.current.gameObject.name);
       if (Input.GetMouseButtonDown(0))
       {
           Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
           RaycastHit hit;
 
           if (Physics.Raycast(ray, out hit))
           {
               //do some thing
           }
       }
   }

还有一个需要注意的地方就是,在做UI的时候一般会用一个Panel做跟目录,这个panel也会被添加到GraphicRegistry中的公共列表中,如果是这样的话记得把list.Count>0改成list.Count>1,或者直接删除Panel上的继承自Graphic的组件。

这样在结合着EventSystem.current.IsPointerOverGameObject()来使用就比较好了。

posted @   多见多闻  阅读(1773)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示