Exemplo n.º 1
0
    /// <summary>
    /// Perform a Unity physics Raycast to determine which scene objects with a collider is currently being gazed at, if any.
    /// </summary>
    private void RaycastPhysics(PointerData pointer, LayerMask[] prioritizedLayerMasks)
    {
        bool       isHit        = false;
        int        rayStepIndex = 0;
        RayStep    rayStep      = default(RayStep);
        RaycastHit physicsHit   = default(RaycastHit);

        Debug.Assert(pointer.PointingSource.Rays != null, "No valid rays for " + pointer.GetType());
        Debug.Assert(pointer.PointingSource.Rays.Length > 0, "No valid rays for " + pointer.GetType());

        // Check raycast for each step in the pointing source
        for (int i = 0; i < pointer.PointingSource.Rays.Length; i++)
        {
            if (RaycastPhysicsStep(pointer.PointingSource.Rays[i], prioritizedLayerMasks, out physicsHit))
            {
                // Set the pointer source's origin ray to this step
                isHit        = true;
                rayStep      = pointer.PointingSource.Rays[i];
                rayStepIndex = i;
                // No need to continue once we've hit something
                break;
            }
        }

        if (isHit)
        {
            pointer.UpdateHit(physicsHit, rayStep, rayStepIndex);
        }
        else
        {
            pointer.UpdateHit(GetPointingExtent(pointer.PointingSource));
        }
    }
Exemplo n.º 2
0
    private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMasks)
    {
        Debug.Assert(pointer.End.Point != Vector3.zero, string.Format("No pointer {0} end point found to raycast against!", pointer.PointingSource.GetType()));
        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;

        Debug.Assert(pointer.PointingSource.Rays != null, "No valid rays for " + pointer.GetType());
        Debug.Assert(pointer.PointingSource.Rays.Length > 0, "No valid rays for " + 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.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);
        }
    }