示例#1
0
        /// <summary>
        /// Handles raycasting specifically buttons
        /// </summary>
        private void HandleButtonRaycast(RaycastHit hit)
        {
            Transform hitUI = hit.transform;

            if (hitUI.GetComponent <Button>())
            {
                Button button = hitUI.GetComponent <Button>();

                if (inputPressed)
                {
                    EventSystem.current.SetSelectedGameObject(null);
                    isButtonSelected = false;
                }
                else if (!isButtonSelected)
                {
                    button.Select();
                    isButtonSelected = true;
                }

                if (inputUp)
                {
                    button.onClick.Invoke(); // Boom we clicked the button
                }
            }
            else if (hitUI.GetComponent <Toggle>())
            {
                Toggle toggle = hitUI.GetComponent <Toggle>();
                if (inputPressed)
                {
                    EventSystem.current.SetSelectedGameObject(null);
                    isToggleSelected = false;
                }
                else if (!isButtonSelected)
                {
                    toggle.Select();
                    isToggleSelected = true;
                }

                if (inputUp)
                {
                    toggle.isOn = !toggle.isOn;
                }
            }
            else if (hitUI.GetComponent <UnityEngine.UI.Slider>())
            {
                UnityEngine.UI.Slider slider = hitUI.GetComponent <UnityEngine.UI.Slider>();
                if (inputPressed)
                {
                    RectTransform rectTransform = slider.transform.GetComponent <RectTransform>();
                    float         width         = rectTransform.rect.width;
                    EventSystem.current.SetSelectedGameObject(null);
                    isSliderSelected = false;
                    Vector3 newSliderValue = slider.transform.InverseTransformPoint(hit.point);
                    float   maxValue       = (width / 2);
                    float   minValue       = (maxValue) * -1;
                    float   value          = Mathf.InverseLerp(minValue, maxValue, newSliderValue.x);
                    slider.value = value;
                }
                else if (!isButtonSelected)
                {
                    slider.Select();
                    isSliderSelected = true;
                }
            }
            else if (hitUI.GetComponent <Scrollbar>())
            {
                Scrollbar scrollbar = hitUI.GetComponent <Scrollbar>();
                if (inputPressed)
                {
                    RectTransform rectTransform = scrollbar.transform.GetComponent <RectTransform>();
                    EventSystem.current.SetSelectedGameObject(null);
                    isScrollBarSelected = false;
                    Vector3 newScrollbarValue = scrollbar.transform.InverseTransformPoint(hit.point);

                    float value = 0;
                    if (scrollbar.direction == Scrollbar.Direction.LeftToRight)
                    {
                        float width    = rectTransform.rect.width;
                        float maxValue = (width / 2);
                        float minValue = (maxValue) * -1;
                        value = Mathf.InverseLerp(minValue, maxValue, newScrollbarValue.x);
                    }
                    else if (scrollbar.direction == Scrollbar.Direction.RightToLeft)
                    {
                        float width    = rectTransform.rect.width;
                        float maxValue = (width / 2);
                        float minValue = (maxValue) * -1;
                        value = Mathf.InverseLerp(maxValue, minValue, newScrollbarValue.x);
                    }
                    else if (scrollbar.direction == Scrollbar.Direction.TopToBottom)
                    {
                        float height   = rectTransform.rect.height;
                        float maxValue = (height / 2);
                        float minValue = (maxValue) * -1;
                        value = Mathf.InverseLerp(maxValue, minValue, newScrollbarValue.y);
                    }
                    else if (scrollbar.direction == Scrollbar.Direction.BottomToTop)
                    {
                        float height   = rectTransform.rect.height;
                        float maxValue = (height / 2);
                        float minValue = (maxValue) * -1;
                        value = Mathf.InverseLerp(minValue, maxValue, newScrollbarValue.y);
                    }

                    if (hitUI.GetComponentInParent <ScrollRect>())
                    {
                        float height   = rectTransform.rect.height;
                        float maxValue = 0;
                        float minValue = (height) * -1;
                        value = Mathf.InverseLerp(minValue, maxValue, newScrollbarValue.y);
                    }

                    scrollbar.value = value;
                }
                else if (!isButtonSelected)
                {
                    scrollbar.Select();
                    isScrollBarSelected = true;
                }
            }
        }