コード例 #1
0
        private void ResolveGenericRaycastHandlers(IRaycastPointerHandler raycastHandlerTarget)
        {
            if (Utils.LockedThisFrame())
            {
                return;
            }

            var mouseIsDown = Input.GetMouseButtonDown(0);
            var mouseIsUp   = Input.GetMouseButtonUp(0);

            if (raycastHandlerTarget is IRaycastPointerDownHandler down)
            {
                if (mouseIsDown)
                {
                    down.OnPointerDown();
                }
            }

            if (raycastHandlerTarget is IRaycastPointerUpHandler up)
            {
                if (mouseIsUp)
                {
                    up.OnPointerUp();
                }
            }

            if (raycastHandlerTarget is IRaycastPointerClickHandler click)
            {
                if (mouseIsDown)
                {
                    clickHandler = click;
                }

                if (mouseIsUp)
                {
                    if (clickHandler == click)
                    {
                        click.OnPointerClick();
                    }
                    clickHandler = null;
                }
            }
        }
コード例 #2
0
        void Update()
        {
            if (!CommonScriptableObjects.rendererState.Get() || charCamera == null)
            {
                return;
            }

            // We use Physics.Raycast() instead of our raycastHandler.Raycast() as that one is slower, sometimes 2x, because it fetches info we don't need here
            if (!Physics.Raycast(GetRayFromCamera(), out hitInfo, Mathf.Infinity, PhysicsLayers.physicsCastLayerMaskWithoutCharacter))
            {
                clickHandler = null;
                UnhoverLastHoveredObject();
                return;
            }

            var raycastHandlerTarget = hitInfo.collider.GetComponent <IRaycastPointerHandler>();

            if (raycastHandlerTarget != null)
            {
                ResolveGenericRaycastHandlers(raycastHandlerTarget);
                UnhoverLastHoveredObject();
                return;
            }

            if (CollidersManager.i.GetColliderInfo(hitInfo.collider, out ColliderInfo info))
            {
                newHoveredEvent = info.entity.gameObject.GetComponentInChildren <OnPointerEvent>();
            }
            else
            {
                newHoveredEvent = hitInfo.collider.GetComponentInChildren <OnPointerEvent>();
            }

            clickHandler = null;

            if (!EventObjectCanBeHovered(newHoveredEvent, info))
            {
                UnhoverLastHoveredObject();
                return;
            }

            newHoveredObject = newHoveredEvent.gameObject;

            if (newHoveredObject != lastHoveredObject)
            {
                UnhoverLastHoveredObject();

                lastHoveredObject    = newHoveredObject;
                lastHoveredEventList = newHoveredObject.GetComponents <OnPointerEvent>();
                OnPointerHoverStarts?.Invoke();
            }

            // OnPointerDown/OnClick and OnPointerUp should display their hover feedback at different moments
            if (lastHoveredEventList != null && lastHoveredEventList.Length > 0)
            {
                for (int i = 0; i < lastHoveredEventList.Length; i++)
                {
                    OnPointerEvent e = lastHoveredEventList[i];

                    bool eventButtonIsPressed = InputController_Legacy.i.IsPressed(e.GetActionButton());

                    if (e is OnPointerUp && eventButtonIsPressed)
                    {
                        e.SetHoverState(true);
                    }
                    else if ((e is OnPointerDown || e is OnClick) && !eventButtonIsPressed)
                    {
                        e.SetHoverState(true);
                    }
                    else
                    {
                        e.SetHoverState(false);
                    }
                }
            }

            newHoveredObject = null;
            newHoveredEvent  = null;
        }
コード例 #3
0
        public void Update()
        {
            if (!CommonScriptableObjects.rendererState.Get() || charCamera == null)
            {
                return;
            }

            IWorldState worldState = Environment.i.world.state;

            // We use Physics.Raycast() instead of our raycastHandler.Raycast() as that one is slower, sometimes 2x, because it fetches info we don't need here
            bool   didHit         = Physics.Raycast(GetRayFromCamera(), out hitInfo, Mathf.Infinity, PhysicsLayers.physicsCastLayerMaskWithoutCharacter);
            bool   uiIsBlocking   = false;
            string currentSceneId = worldState.currentSceneId;

            bool validCurrentSceneId = !string.IsNullOrEmpty(currentSceneId);
            bool validCurrentScene   = validCurrentSceneId && worldState.loadedScenes.ContainsKey(currentSceneId);

            // NOTE: in case of a single scene loaded (preview or builder) sceneId is set to null when stepping outside
            if (didHit && validCurrentSceneId && validCurrentScene)
            {
                GraphicRaycaster raycaster = worldState.loadedScenes[currentSceneId].uiScreenSpace?.graphicRaycaster;
                if (raycaster)
                {
                    uiGraphicRaycastPointerEventData.position = new Vector2(Screen.width / 2, Screen.height / 2);
                    uiGraphicRaycastResults.Clear();
                    raycaster.Raycast(uiGraphicRaycastPointerEventData, uiGraphicRaycastResults);
                    uiIsBlocking = uiGraphicRaycastResults.Count > 0;
                }
            }

            if (!didHit || uiIsBlocking)
            {
                clickHandler = null;
                UnhoverLastHoveredObject(hoverController);
                return;
            }

            var raycastHandlerTarget = hitInfo.collider.GetComponent <IRaycastPointerHandler>();

            if (raycastHandlerTarget != null)
            {
                ResolveGenericRaycastHandlers(raycastHandlerTarget);
                UnhoverLastHoveredObject(hoverController);
                return;
            }

            if (CollidersManager.i.GetColliderInfo(hitInfo.collider, out ColliderInfo info))
            {
                newHoveredEvent = info.entity.gameObject.GetComponentInChildren <OnPointerEvent>();
            }
            else
            {
                newHoveredEvent = hitInfo.collider.GetComponentInChildren <OnPointerEvent>();
            }

            clickHandler = null;

            if (!EventObjectCanBeHovered(newHoveredEvent, info, hitInfo.distance))
            {
                UnhoverLastHoveredObject(hoverController);
                return;
            }

            newHoveredObject = newHoveredEvent.gameObject;

            if (newHoveredObject != lastHoveredObject)
            {
                UnhoverLastHoveredObject(hoverController);

                lastHoveredObject    = newHoveredObject;
                lastHoveredEventList = newHoveredObject.GetComponents <OnPointerEvent>();
                OnPointerHoverStarts?.Invoke();
            }

            // OnPointerDown/OnClick and OnPointerUp should display their hover feedback at different moments
            if (lastHoveredEventList != null && lastHoveredEventList.Length > 0)
            {
                for (int i = 0; i < lastHoveredEventList.Length; i++)
                {
                    OnPointerEvent e = lastHoveredEventList[i];

                    bool eventButtonIsPressed = InputController_Legacy.i.IsPressed(e.GetActionButton());

                    if (e is OnPointerUp && eventButtonIsPressed)
                    {
                        e.SetHoverState(true);
                    }
                    else if ((e is OnPointerDown || e is OnClick) && !eventButtonIsPressed)
                    {
                        e.SetHoverState(true);
                    }
                    else
                    {
                        e.SetHoverState(false);
                    }
                }
            }

            newHoveredObject = null;
            newHoveredEvent  = null;
        }