public override void Process()
        {
            // Try to get pointer for this input module
            pointerEventData = new PointerEventData(EventSystem.current);
            GetPointerData(kMouseLeftId, out pointerEventData, true);

            pointerEventData.Reset();
            pointerEventData.delta       = Vector2.zero;
            pointerEventData.position    = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
            pointerEventData.scrollDelta = Vector2.zero;
            pointerEventData.button      = PointerEventData.InputButton.Left;

            // Raycast pointer to canvas
            m_RaycastResultCache.Clear();
            eventSystem.RaycastAll(pointerEventData, m_RaycastResultCache);
            pointerEventData.pointerCurrentRaycast = FindFirstRaycast(m_RaycastResultCache);
            PointerHitCanvas = m_RaycastResultCache.Count > 0;
            UpdatePointerWorldPosition();

            // Process presses & events
            mouseState.SetButtonState(PointerEventData.InputButton.Left, GetPressState(), pointerEventData);
            MouseButtonEventData buttonState = mouseState.GetButtonState(PointerEventData.InputButton.Left).eventData;

            if (buttonState.PressedThisFrame())
            {
                ButtonPress(buttonState.buttonData);
            }

            if (buttonState.ReleasedThisFrame())
            {
                ButtonRelease(buttonState.buttonData);
            }

            ProcessMove(buttonState.buttonData);

            if (buttonHeld)
            {
                ProcessDrag(buttonState.buttonData);
            }
        }
Exemplo n.º 2
0
    private UnityEngine.Vector2 ProjectPointer(float x, float y)
    {
        if (_textureCamera == null)
        {
            return(new UnityEngine.Vector2(x, UnityEngine.Screen.height - y));
        }
        else if (_texture != null)
        {
            // Project using texture coordinates

            // First try with Unity GUI RawImage objects
            UnityEngine.EventSystems.EventSystem eventSystem = UnityEngine.EventSystems.EventSystem.current;

            if (eventSystem != null && eventSystem.IsPointerOverGameObject())
            {
                UnityEngine.Vector2 pos = new UnityEngine.Vector2(x, y);

                if (_pointerData == null)
                {
                    _pointerData = new UnityEngine.EventSystems.PointerEventData(eventSystem)
                    {
                        pointerId = 0,
                        position  = pos
                    };
                }
                else
                {
                    _pointerData.Reset();
                }

                _pointerData.delta    = pos - _pointerData.position;
                _pointerData.position = pos;

                UnityEngine.RectTransform rect = GetComponent <UnityEngine.RectTransform>();

                if (rect != null &&
                    UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(
                        rect, _pointerData.position, _pointerData.pressEventCamera, out pos))
                {
                    UnityEngine.Vector2 pivot = new UnityEngine.Vector2(
                        rect.pivot.x * rect.rect.width,
                        rect.pivot.y * rect.rect.height);

                    float texCoordX = (pos.x + pivot.x) / rect.rect.width;
                    float texCoordY = (pos.y + pivot.y) / rect.rect.height;

                    float localX = _texture.width * texCoordX;
                    float localY = _texture.height * (1.0f - texCoordY);
                    return(new UnityEngine.Vector2(localX, localY));
                }
            }

            // NOTE: A MeshCollider must be attached to the target to obtain valid
            // texture coordinates, otherwise Hit Testing won't work

            UnityEngine.Ray ray = UnityEngine.Camera.main.ScreenPointToRay(new UnityEngine.Vector3(x, y, 0));

            UnityEngine.RaycastHit hit;
            if (UnityEngine.Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject == gameObject)
                {
                    float localX = _texture.width * hit.textureCoord.x;
                    float localY = _texture.height * (1.0f - hit.textureCoord.y);
                    return(new UnityEngine.Vector2(localX, localY));
                }
            }

            return(new UnityEngine.Vector2(-1, -1));
        }

        return(Vector2.zero);
    }
Exemplo n.º 3
0
        /// <summary>
        /// Raises the begin drag event.
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public virtual void OnBeginDrag(PointerEventData eventData)
        {
            if (!this.enabled || !this.IsAssigned() || !this.m_DragAndDropEnabled)
            {
                eventData.Reset();
                return;
            }

            // Check if we have a key modifier and if it's held down
            if (!this.DragKeyModifierIsDown())
            {
                eventData.Reset();
                return;
            }

            // Start the drag
            this.m_DragHasBegan = true;

            // Create the temporary icon for dragging
            this.CreateTemporaryIcon(eventData);

            // Prevent event propagation
            eventData.Use();
        }
Exemplo n.º 4
0
    private void CastRayFromGaze()
    {
        pointerData = (pointerData == null ? new PointerEventData(eventSystem) : pointerData);

        pointerData.Reset();
        pointerData.position = new Vector3(hotspot.x * Screen.width, hotspot.y * Screen.height);
        eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
        //List<RaycastResult> removeResult = new List<RaycastResult>();
        //foreach (RaycastResult rayResult in m_RaycastResultCache)
        //{
        //    if (interactTag != null && interactTag.Length > 1 && !rayResult.gameObject.tag.Equals(interactTag))
        //    {
        //        removeResult.Add(rayResult);
        //    }
        //}

        //foreach (RaycastResult rayResult in removeResult)
        //{
        //    m_RaycastResultCache.Remove(rayResult);
        //}

        //TO-DO: FIND OUT HOW TO GET SPECIFIC RAYCAST FOR THIS OBJECT.
        pointerData.pointerCurrentRaycast = FindFirstRaycast(m_RaycastResultCache);
        m_RaycastResultCache.Clear();
    }