Example #1
0
        private TrackedPointerEventData GetOrCreateCachedTrackedPointerEvent()
        {
            var result = m_CachedTrackedPointerEventData;

            if (result == null)
            {
                result = new TrackedPointerEventData(eventSystem);
                m_CachedTrackedPointerEventData = result;
            }

            return(result);
        }
        public void CopyFrom(TrackedPointerEventData eventData)
        {
            m_InternalData.pointerTarget        = eventData.pointerEnter;
            m_InternalData.isDragging           = eventData.dragging;
            m_InternalData.pressedTime          = eventData.clickTime;
            m_InternalData.pressedPosition      = eventData.pressPosition;
            m_InternalData.pressedRaycast       = eventData.pointerPressRaycast;
            m_InternalData.pressedGameObject    = eventData.pointerPress;
            m_InternalData.pressedGameObjectRaw = eventData.rawPointerPress;
            m_InternalData.draggedGameObject    = eventData.pointerDrag;

            var hoverTargets = m_InternalData.hoverTargets;

            hoverTargets.ClearWithCapacity();
            hoverTargets.Append(eventData.hovered);
            m_InternalData.hoverTargets = hoverTargets;
        }
        public void CopyTo(TrackedPointerEventData eventData)
        {
            eventData.ray         = new Ray(m_Position, m_Orientation * Vector3.forward);
            eventData.maxDistance = 1000;
            // Demolish the position so we don't trigger any checks from the Graphics Raycaster.
            eventData.position = new Vector2(float.MinValue, float.MinValue);

            eventData.pointerEnter        = m_InternalData.pointerTarget;
            eventData.dragging            = m_InternalData.isDragging;
            eventData.clickTime           = m_InternalData.pressedTime;
            eventData.pressPosition       = m_InternalData.pressedPosition;
            eventData.pointerPressRaycast = m_InternalData.pressedRaycast;
            eventData.pointerPress        = m_InternalData.pressedGameObject;
            eventData.rawPointerPress     = m_InternalData.pressedGameObjectRaw;
            eventData.pointerDrag         = m_InternalData.draggedGameObject;

            eventData.hovered.Clear();
            eventData.hovered.AddRange(m_InternalData.hoverTargets);
        }
        private void PerformRaycast(TrackedPointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            if (canvas == null)
            {
                return;
            }

            if (eventCamera == null)
            {
                return;
            }

            var ray = eventData.ray;

            var hitDistance = eventData.maxDistance;

            if (checkFor3DOcclusion)
            {
                var hits = Physics.RaycastAll(ray, hitDistance, m_BlockingMask);

                if (hits.Length > 0 && hits[0].distance < hitDistance)
                {
                    hitDistance = hits[0].distance;
                }
            }

            if (checkFor2DOcclusion)
            {
                var raycastDistance = hitDistance;
                var hits            = Physics2D.GetRayIntersectionAll(ray, raycastDistance, m_BlockingMask);

                if (hits.Length > 0 && hits[0].fraction * raycastDistance < hitDistance)
                {
                    hitDistance = hits[0].fraction * raycastDistance;
                }
            }

            m_RaycastResultsCache.Clear();
            SortedRaycastGraphics(canvas, ray, m_RaycastResultsCache);
            var screenPosition = Vector2.zero;

            if (m_RaycastResultsCache.Count == 0)
            {
                Vector3 endPosition = ray.origin + (ray.direction.normalized * hitDistance);
                screenPosition = eventCamera.WorldToScreenPoint(endPosition);
            }
            else
            {
                screenPosition = m_RaycastResultsCache[0].screenPosition;
            }

            var thisFrameDelta = screenPosition - eventData.position;

            eventData.position = screenPosition;
            eventData.delta    = thisFrameDelta;

            //Now that we have a list of sorted hits, process any extra settings and filters.
            for (var i = 0; i < m_RaycastResultsCache.Count; i++)
            {
                var validHit = true;

                var hitData = m_RaycastResultsCache[i];

                var go = hitData.graphic.gameObject;
                if (ignoreReversedGraphics)
                {
                    var forward     = ray.direction;
                    var goDirection = go.transform.rotation * Vector3.forward;
                    validHit = Vector3.Dot(forward, goDirection) > 0;
                }

                validHit &= hitData.distance < hitDistance;

                if (validHit)
                {
                    var castResult = new RaycastResult
                    {
                        gameObject = go,
                        module     = this,
                        distance   = hitData.distance,
                        index      = resultAppendList.Count,
                        depth      = hitData.graphic.depth,

                        worldPosition = hitData.worldHitPosition
                    };
                    resultAppendList.Add(castResult);
                }
            }
        }