Exemplo n.º 1
0
        private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMasks)
        {
            Debug.Assert(pointer.End.Point != Vector3.zero, "No pointer source end point found to raycast against!");
            Debug.Assert(UIRaycastCamera != null, "You must assign a UIRaycastCamera on the FocusManager before you can process uGUI raycasting.");

            RaycastResult uiRaycastResult = default(RaycastResult);
            bool overridePhysicsRaycast = false;
            RayStep rayStep = default(RayStep);
            int rayStepIndex = 0;

            // Comment back in GetType() only when debugging for a specific pointer.
            Debug.Assert(pointer.PointingSource.Rays != null, "No valid rays for pointer "/* + pointer.GetType()*/);
            Debug.Assert(pointer.PointingSource.Rays.Length > 0, "No valid rays for pointer "/* + pointer.GetType()*/);

            // Cast rays for every step until we score a hit
            for (int i = 0; i < pointer.PointingSource.Rays.Length; i++)
            {
                if (RaycastUnityUIStep(pointer, pointer.PointingSource.Rays[i], prioritizedLayerMasks, out overridePhysicsRaycast, out uiRaycastResult))
                {
                    rayStepIndex = i;
                    rayStep = pointer.PointingSource.Rays[i];
                    break;
                }
            }

            // Check if we need to overwrite the physics raycast info
            if ((pointer.End.Object == null || overridePhysicsRaycast) && uiRaycastResult.isValid &&
                 uiRaycastResult.module != null && uiRaycastResult.module.eventCamera == UIRaycastCamera)
            {
                newUiRaycastPosition.x = uiRaycastResult.screenPosition.x;
                newUiRaycastPosition.y = uiRaycastResult.screenPosition.y;
                newUiRaycastPosition.z = uiRaycastResult.distance;

                Vector3 worldPos = UIRaycastCamera.ScreenToWorldPoint(newUiRaycastPosition);

                var hitInfo = new RaycastHit
                {
                    point = worldPos,
                    normal = -uiRaycastResult.gameObject.transform.forward
                };

                pointer.UpdateHit(uiRaycastResult, hitInfo, rayStep, rayStepIndex);
            }
        }
Exemplo n.º 2
0
        private bool RaycastPhysicsStep(RayStep step, LayerMask[] prioritizedLayerMasks, out RaycastHit physicsHit)
        {
            bool isHit = false;
            physicsHit = default(RaycastHit);

            // If there is only one priority, don't prioritize
            if (prioritizedLayerMasks.Length == 1)
            {
                isHit = Physics.Raycast(step.Origin, step.Direction, out physicsHit, step.Length, prioritizedLayerMasks[0]);
            }
            else
            {
                // Raycast across all layers and prioritize
                RaycastHit? hit = PrioritizeHits(Physics.RaycastAll(step.Origin, step.Direction, step.Length, Physics.AllLayers), prioritizedLayerMasks);
                isHit = hit.HasValue;

                if (isHit)
                {
                    physicsHit = hit.Value;
                }
            }

            return isHit;
        }