Exemple #1
0
    void Awake()
    {
        raycaster = GetComponent <OSVRRaycaster>();
#if UNITY_ANDROID
        pointerObject.SetActive(false);
        this.enabled = false;
#endif
    }
        /// <summary>
        /// Get state for pointer which is a pointer moving in world space across the surface of a world space canvas.
        /// </summary>
        /// <returns></returns>
        protected MouseState GetCanvasPointerData()
        {
            // Get the OSVRRayPointerEventData reference
            PointerEventData leftData;

            GetPointerData(kMouseLeftId, out leftData, true);
            leftData.Reset();

            // Setup default values here. Set position to zero because we don't actually know the pointer
            // positions. Each canvas knows the position of its canvas pointer.
            leftData.position    = Vector2.zero;
            leftData.scrollDelta = Input.mouseScrollDelta;
            leftData.button      = PointerEventData.InputButton.Left;

            if (activeGraphicRaycaster)
            {
                // Let the active raycaster find intersections on its canvas
                activeGraphicRaycaster.RaycastPointer(leftData, m_RaycastResultCache);
                var raycast = FindFirstRaycast(m_RaycastResultCache);
                leftData.pointerCurrentRaycast = raycast;
                m_RaycastResultCache.Clear();

                OSVRRaycaster osvrRaycaster = raycast.module as OSVRRaycaster;
                if (osvrRaycaster) // raycast may not actually contain a result
                {
                    // The Unity UI system expects event data to have a screen position
                    // so even though this raycast came from a world space ray we must get a screen
                    // space position for the camera attached to this raycaster for compatability
                    Vector2 position = osvrRaycaster.GetScreenPosition(raycast);

                    leftData.delta    = position - leftData.position;
                    leftData.position = position;
                }
            }

            // copy the apropriate data into right and middle slots
            PointerEventData rightData;

            GetPointerData(kMouseRightId, out rightData, true);
            CopyFromTo(leftData, rightData);
            rightData.button = PointerEventData.InputButton.Right;

            PointerEventData middleData;

            GetPointerData(kMouseMiddleId, out middleData, true);
            CopyFromTo(leftData, middleData);
            middleData.button = PointerEventData.InputButton.Middle;

            m_MouseState.SetButtonState(PointerEventData.InputButton.Left, StateForMouseButton(0), leftData);
            m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
            m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
            return(m_MouseState);
        }
        // Overridden so that we can process the two types of pointer separately


        // The following 2 functions are equivalent to PointerInputModule.GetMousePointerEventData but are customized to
        // get data for ray pointers and canvas mouse pointers.

        /// <summary>
        /// State for a pointer controlled by a world space ray. E.g. gaze pointer
        /// </summary>
        /// <returns></returns>
        protected MouseState GetGazePointerData()
        {
            // Get the OSVRRayPointerEventData reference
            OSVRRayPointerEventData leftData;

            GetPointerData(kMouseLeftId, out leftData, true);
            leftData.Reset();

            //Now set the world space ray. This ray is what the user uses to point at UI elements
            leftData.worldSpaceRay = new Ray(rayTransform.position, rayTransform.forward);
            leftData.scrollDelta   = GetExtraScrollDelta();

            //Populate some default values
            leftData.button           = PointerEventData.InputButton.Left;
            leftData.useDragThreshold = true;
            // Perform raycast to find intersections with world
            eventSystem.RaycastAll(leftData, m_RaycastResultCache);
            var raycast = FindFirstRaycast(m_RaycastResultCache);

            leftData.pointerCurrentRaycast = raycast;
            m_RaycastResultCache.Clear();

            OSVRRaycaster osvrRaycaster = raycast.module as OSVRRaycaster;

            // We're only interested in intersections from OSVRRaycasters
            if (osvrRaycaster)
            {
                // The Unity UI system expects event data to have a screen position
                // so even though this raycast came from a world space ray we must get a screen
                // space position for the camera attached to this raycaster for compatability
                leftData.position = osvrRaycaster.GetScreenPosition(raycast);


                // Find the world position and normal the Graphic the ray intersected
                RectTransform graphicRect = raycast.gameObject.GetComponent <RectTransform>();
                if (graphicRect != null)
                {
                    // Set are gaze indicator with this world position and normal
                    Vector3 worldPos = raycast.worldPosition;
                    Vector3 normal   = GetRectTransformNormal(graphicRect);
                    OSVRGazePointer.instance.SetPosition(worldPos, normal);
                    // Make sure it's being shown
                    OSVRGazePointer.instance.RequestShow();
                }
            }
            OSVRPhysicsRaycaster physicsRaycaster = raycast.module as OSVRPhysicsRaycaster;

            if (physicsRaycaster)
            {
                leftData.position = physicsRaycaster.GetScreenPos(raycast.worldPosition);
                OSVRGazePointer.instance.RequestShow();
                OSVRGazePointer.instance.SetPosition(raycast.worldPosition, raycast.worldNormal);
            }



            // Stick default data values in right and middle slots for compatability

            // copy the apropriate data into right and middle slots
            OSVRRayPointerEventData rightData;

            GetPointerData(kMouseRightId, out rightData, true);
            CopyFromTo(leftData, rightData);
            rightData.button = PointerEventData.InputButton.Right;

            OSVRRayPointerEventData middleData;

            GetPointerData(kMouseMiddleId, out middleData, true);
            CopyFromTo(leftData, middleData);
            middleData.button = PointerEventData.InputButton.Middle;


            m_MouseState.SetButtonState(PointerEventData.InputButton.Left, GetGazeButtonState(), leftData);
            m_MouseState.SetButtonState(PointerEventData.InputButton.Right, PointerEventData.FramePressState.NotChanged, rightData);
            m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, PointerEventData.FramePressState.NotChanged, middleData);
            return(m_MouseState);
        }