Ejemplo n.º 1
0
        /// <summary>
        /// Performs the sampling, updating the input status; expected to be invoked on each render loop update.
        /// </summary>
        public void SampleInput()
        {
            #if ENABLE_LEGACY_INPUT_MANAGER
            if (config.ProcessLegacyBindings && Binding.Keys?.Count > 0)
            {
                foreach (var key in Binding.Keys)
                {
                    if (Input.GetKeyDown(key))
                    {
                        SetInputValue(1);
                    }
                    if (Input.GetKeyUp(key))
                    {
                        SetInputValue(0);
                    }
                }
            }

            if (config.ProcessLegacyBindings && Binding.Axes?.Count > 0)
            {
                var maxValue = 0f;
                foreach (var axis in Binding.Axes)
                {
                    var axisValue = axis.Sample();
                    if (Mathf.Abs(axisValue) > Mathf.Abs(maxValue))
                    {
                        maxValue = axisValue;
                    }
                }
                if (maxValue != Value)
                {
                    SetInputValue(maxValue);
                }
            }

            if (Input.touchSupported && Binding.Swipes?.Count > 0)
            {
                var swipeRegistered = false;
                foreach (var swipe in Binding.Swipes)
                {
                    if (swipe.Sample())
                    {
                        swipeRegistered = true; break;
                    }
                }
                if (swipeRegistered != Active)
                {
                    SetInputValue(swipeRegistered ? 1 : 0);
                }
            }

            if (objectTriggers.Count > 0)
            {
                var touchBegan = Input.touchCount > 0 &&
                                 Input.GetTouch(0).phase == TouchPhase.Began &&
                                 (Time.time - lastTouchTime) > touchCooldown;
                if (touchBegan)
                {
                    lastTouchTime = Time.time;
                }
                var clickedDown = Input.GetMouseButtonDown(0);
                if (clickedDown || touchBegan)
                {
                    var hoveredObject = EventSystem.current.GetHoveredGameObject();
                    if (hoveredObject && objectTriggers.Contains(hoveredObject))
                    {
                        SetInputValue(1f);
                    }
                }

                var touchEnded = Input.touchCount > 0 &&
                                 Input.GetTouch(0).phase == TouchPhase.Ended;
                var clickedUp = Input.GetMouseButtonUp(0);
                if (touchEnded || clickedUp)
                {
                    SetInputValue(0f);
                }
            }
            #endif

            #if ENABLE_INPUT_SYSTEM && INPUT_SYSTEM_AVAILABLE
            if (inputAction != null)
            {
                if (inputAction.type == UnityEngine.InputSystem.InputActionType.Value)
                {
                    var value = inputAction.ReadValue <float>();
                    SetInputValue(value);
                }
                else
                {
                    SetInputValue(inputAction.triggered ? 1 : 0);
                }
            }
            #endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs the sampling, updating the input status; expected to be invoked on each render loop update.
        /// </summary>
        public virtual void SampleInput()
        {
            if (!Enabled)
            {
                return;
            }

            #if ENABLE_LEGACY_INPUT_MANAGER
            if (config.ProcessLegacyBindings && Binding.Keys?.Count > 0)
            {
                SampleKeys();
            }

            if (config.ProcessLegacyBindings && Binding.Axes?.Count > 0)
            {
                SampleAxes();
            }

            if (Input.touchSupported && Binding.Swipes?.Count > 0)
            {
                SampleSwipes();
            }

            if (objectTriggers.Count > 0)
            {
                SampleObjectTriggers();
            }

            void SampleKeys()
            {
                foreach (var key in Binding.Keys)
                {
                    if (Input.GetKeyDown(key))
                    {
                        SetInputValue(1);
                    }
                    if (Input.GetKeyUp(key))
                    {
                        SetInputValue(0);
                    }
                }
            }

            void SampleAxes()
            {
                var maxValue = 0f;

                foreach (var axis in Binding.Axes)
                {
                    var axisValue = axis.Sample();
                    if (Mathf.Abs(axisValue) > Mathf.Abs(maxValue))
                    {
                        maxValue = axisValue;
                    }
                }
                if (!Mathf.Approximately(maxValue, Value))
                {
                    SetInputValue(maxValue);
                }
            }

            void SampleSwipes()
            {
                if (!Input.touchSupported)
                {
                    return;
                }
                var swipeRegistered = false;

                foreach (var swipe in Binding.Swipes)
                {
                    if (swipe.Sample())
                    {
                        swipeRegistered = true; break;
                    }
                }
                if (swipeRegistered != Active)
                {
                    SetInputValue(swipeRegistered ? 1 : 0);
                }
            }

            void SampleObjectTriggers()
            {
                if (!Triggered())
                {
                    return;
                }
                var hoveredObject = EventSystem.current.GetHoveredGameObject();

                if (hoveredObject && objectTriggers.Contains(hoveredObject))
                {
                    if (!hoveredObject.TryGetComponent <IInputTrigger>(out var trigger) || trigger.CanTriggerInput())
                    {
                        SetInputValue(1f);
                    }
                }

                bool Triggered()
                {
                    if (!Input.touchSupported || Input.touchCount == 0)
                    {
                        return(Input.GetMouseButtonDown(0));
                    }

                    var touch = Input.GetTouch(0);

                    if (touch.phase == TouchPhase.Began)
                    {
                        lastTouchBeganPosition = touch.position;
                        return(false);
                    }

                    if (touch.phase != TouchPhase.Ended)
                    {
                        return(false);
                    }

                    var cooldown = Time.unscaledTime - lastTouchTime <= config.TouchFrequencyLimit;

                    if (cooldown)
                    {
                        return(false);
                    }

                    var withinDistanceLimit = Vector2.Distance(touch.position, lastTouchBeganPosition) < config.TouchDistanceLimit;

                    if (!withinDistanceLimit)
                    {
                        return(false);
                    }

                    lastTouchTime = Time.unscaledTime;
                    return(true);
                }
            }
            #endif

            #if ENABLE_INPUT_SYSTEM && INPUT_SYSTEM_AVAILABLE
            if (inputAction != null)
            {
                if (inputAction.type == UnityEngine.InputSystem.InputActionType.Value)
                {
                    var value = inputAction.ReadValue <float>();
                    SetInputValue(value);
                }
                else if (inputAction.WasPressedThisFrame())
                {
                    SetInputValue(1);
                }
                else if (inputAction.WasReleasedThisFrame())
                {
                    SetInputValue(0);
                }
            }
            #endif
        }