Ejemplo n.º 1
0
 internal static GameObject RaycastTry2D(Camera cam, Ray ray, float distance, int layerMask)
 {
     return(CameraRaycastHelper.RaycastTry2D_Injected(cam, ref ray, distance, layerMask));
 }
Ejemplo n.º 2
0
        static void DoSendMouseEvents(int skipRTCameras)
        {
            var mousePosition = Input.mousePosition;

            int camerasCount = Camera.allCamerasCount;

            if (m_Cameras == null || m_Cameras.Length != camerasCount)
            {
                m_Cameras = new Camera[camerasCount];
            }

            // Fetch all cameras.
            Camera.GetAllCameras(m_Cameras);

            // Clear the HitInfos from last time
            for (var hitIndex = 0; hitIndex < m_CurrentHit.Length; ++hitIndex)
            {
                m_CurrentHit[hitIndex] = new HitInfo();
            }

            // If UnityGUI has the mouse over, we simply don't do any mouse hit detection.
            // That way, it will appear as if the mouse has missed everything.
            if (!s_MouseUsed)
            {
                foreach (var camera in m_Cameras)
                {
                    // we do not want to check cameras that are rendering to textures, starting with 4.0
                    if (camera == null || skipRTCameras != 0 && camera.targetTexture != null)
                    {
                        continue;
                    }

                    int displayIndex = camera.targetDisplay;

                    var eventPosition = Display.RelativeMouseAt(mousePosition);
                    if (eventPosition != Vector3.zero)
                    {
                        // We support multiple display and display identification based on event position.
                        int eventDisplayIndex = (int)eventPosition.z;

                        // Discard events that are not part of this display so the user does not interact with multiple displays at once.
                        if (eventDisplayIndex != displayIndex)
                        {
                            continue;
                        }

                        // Multiple display support only when not the main display. For display 0 the reported
                        // resolution is always the desktop resolution since it's part of the display API,
                        // so we use the standard non multiple display method.
                        float w = Screen.width;
                        float h = Screen.height;
                        if (displayIndex > 0 && displayIndex < Display.displays.Length)
                        {
                            w = Display.displays[displayIndex].systemWidth;
                            h = Display.displays[displayIndex].systemHeight;
                        }

                        Vector2 pos = new Vector2(eventPosition.x / w, eventPosition.y / h);

                        // If the mouse is outside the display bounds, do nothing
                        if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        // The multiple display system is not supported on all platforms, when it is not supported the returned position
                        // will be all zeros so when the returned index is 0 we will default to the mouse position to be safe.
                        eventPosition = mousePosition;
                    }

                    // Is the mouse inside the cameras viewport?
                    var rect = camera.pixelRect;
                    if (!rect.Contains(eventPosition))
                    {
                        continue;
                    }

                    HitTestLegacyGUI(camera, eventPosition, ref m_CurrentHit[m_HitIndexGUI]);

                    // There is no need to continue if the camera shouldn't be sending out events
                    if (camera.eventMask == 0)
                    {
                        continue;
                    }

                    // Calculate common physics projection and distance.
                    var screenProjectionRay = camera.ScreenPointToRay(eventPosition);
                    var projectionDirection = screenProjectionRay.direction.z;
                    var distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection) ? Mathf.Infinity : Mathf.Abs((camera.farClipPlane - camera.nearClipPlane) / projectionDirection);

                    // Did we hit any 3D colliders?
                    var hit3D = CameraRaycastHelper.RaycastTry(camera, screenProjectionRay, distanceToClipPlane, camera.cullingMask & camera.eventMask);
                    if (hit3D != null)
                    {
                        m_CurrentHit[m_HitIndexPhysics3D].target = hit3D;
                        m_CurrentHit[m_HitIndexPhysics3D].camera = camera;
                    }
                    // We did not hit anything with a raycast from this camera. But our camera
                    // clears the screen and renders on top of whatever was below, thus making things
                    // rendered before invisible. So clear any previous hit we have found.
                    else if (camera.clearFlags == CameraClearFlags.Skybox || camera.clearFlags == CameraClearFlags.SolidColor)
                    {
                        m_CurrentHit[m_HitIndexPhysics3D].target = null;
                        m_CurrentHit[m_HitIndexPhysics3D].camera = null;
                    }

                    // Did we hit any 2D colliders?
                    var hit2D = CameraRaycastHelper.RaycastTry2D(camera, screenProjectionRay, distanceToClipPlane, camera.cullingMask & camera.eventMask);
                    if (hit2D != null)
                    {
                        m_CurrentHit[m_HitIndexPhysics2D].target = hit2D;
                        m_CurrentHit[m_HitIndexPhysics2D].camera = camera;
                    }
                    // We did not hit anything with a raycast from this camera. But our camera
                    // clears the screen and renders on top of whatever was below, thus making things
                    // rendered before invisible. So clear any previous hit we have found.
                    else if (camera.clearFlags == CameraClearFlags.Skybox || camera.clearFlags == CameraClearFlags.SolidColor)
                    {
                        m_CurrentHit[m_HitIndexPhysics2D].target = null;
                        m_CurrentHit[m_HitIndexPhysics2D].camera = null;
                    }
                }
            }

            // Send hit events.
            for (var hitIndex = 0; hitIndex < m_CurrentHit.Length; ++hitIndex)
            {
                SendEvents(hitIndex, m_CurrentHit[hitIndex]);
            }

            s_MouseUsed = false;
        }
Ejemplo n.º 3
0
        private static void DoSendMouseEvents(int skipRTCameras)
        {
            Vector3 mousePosition   = Input.mousePosition;
            int     allCamerasCount = Camera.allCamerasCount;
            bool    flag            = SendMouseEvents.m_Cameras == null || SendMouseEvents.m_Cameras.Length != allCamerasCount;

            if (flag)
            {
                SendMouseEvents.m_Cameras = new Camera[allCamerasCount];
            }
            Camera.GetAllCameras(SendMouseEvents.m_Cameras);
            for (int i = 0; i < SendMouseEvents.m_CurrentHit.Length; i++)
            {
                SendMouseEvents.m_CurrentHit[i] = default(SendMouseEvents.HitInfo);
            }
            bool flag2 = !SendMouseEvents.s_MouseUsed;

            if (flag2)
            {
                Camera[] cameras = SendMouseEvents.m_Cameras;
                for (int j = 0; j < cameras.Length; j++)
                {
                    Camera camera = cameras[j];
                    bool   flag3  = camera == null || (skipRTCameras != 0 && camera.targetTexture != null);
                    if (!flag3)
                    {
                        int     targetDisplay = camera.targetDisplay;
                        Vector3 vector        = Display.RelativeMouseAt(mousePosition);
                        bool    flag4         = vector != Vector3.zero;
                        if (flag4)
                        {
                            int  num   = (int)vector.z;
                            bool flag5 = num != targetDisplay;
                            if (flag5)
                            {
                                goto IL_358;
                            }
                            float num2  = (float)Screen.width;
                            float num3  = (float)Screen.height;
                            bool  flag6 = targetDisplay > 0 && targetDisplay < Display.displays.Length;
                            if (flag6)
                            {
                                num2 = (float)Display.displays[targetDisplay].systemWidth;
                                num3 = (float)Display.displays[targetDisplay].systemHeight;
                            }
                            Vector2 vector2 = new Vector2(vector.x / num2, vector.y / num3);
                            bool    flag7   = vector2.x <0f || vector2.x> 1f || vector2.y <0f || vector2.y> 1f;
                            if (flag7)
                            {
                                goto IL_358;
                            }
                        }
                        else
                        {
                            vector = mousePosition;
                        }
                        bool flag8 = !camera.pixelRect.Contains(vector);
                        if (!flag8)
                        {
                            bool flag9 = camera.eventMask == 0;
                            if (!flag9)
                            {
                                Ray        ray        = camera.ScreenPointToRay(vector);
                                float      z          = ray.direction.z;
                                float      distance   = Mathf.Approximately(0f, z) ? float.PositiveInfinity : Mathf.Abs((camera.farClipPlane - camera.nearClipPlane) / z);
                                GameObject gameObject = CameraRaycastHelper.RaycastTry(camera, ray, distance, camera.cullingMask & camera.eventMask);
                                bool       flag10     = gameObject != null;
                                if (flag10)
                                {
                                    SendMouseEvents.m_CurrentHit[1].target = gameObject;
                                    SendMouseEvents.m_CurrentHit[1].camera = camera;
                                }
                                else
                                {
                                    bool flag11 = camera.clearFlags == CameraClearFlags.Skybox || camera.clearFlags == CameraClearFlags.Color;
                                    if (flag11)
                                    {
                                        SendMouseEvents.m_CurrentHit[1].target = null;
                                        SendMouseEvents.m_CurrentHit[1].camera = null;
                                    }
                                }
                                GameObject gameObject2 = CameraRaycastHelper.RaycastTry2D(camera, ray, distance, camera.cullingMask & camera.eventMask);
                                bool       flag12      = gameObject2 != null;
                                if (flag12)
                                {
                                    SendMouseEvents.m_CurrentHit[2].target = gameObject2;
                                    SendMouseEvents.m_CurrentHit[2].camera = camera;
                                }
                                else
                                {
                                    bool flag13 = camera.clearFlags == CameraClearFlags.Skybox || camera.clearFlags == CameraClearFlags.Color;
                                    if (flag13)
                                    {
                                        SendMouseEvents.m_CurrentHit[2].target = null;
                                        SendMouseEvents.m_CurrentHit[2].camera = null;
                                    }
                                }
                            }
                        }
                    }
                    IL_358 :;
                }
            }
            for (int k = 0; k < SendMouseEvents.m_CurrentHit.Length; k++)
            {
                SendMouseEvents.SendEvents(k, SendMouseEvents.m_CurrentHit[k]);
            }
            SendMouseEvents.s_MouseUsed = false;
        }