chenxin
2020-11-18 c1de51124dcaf0311c715273d895734cabb2227c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
using UnityEngine.UI;
using UnityInput = UnityEngine.Input;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// Component to override ScrollRect, uses normalized mouse position inside ScrollRect to scroll
    /// </summary>
    [RequireComponent(typeof(ScrollRect))]
    public class MouseScroll : MonoBehaviour
    {
        /// <summary>
        /// If the normalized scroll position should be clamped between 0 & 1
        /// </summary>
        public bool clampScroll = true;
 
        /// <summary>
        /// Buffer to adjust ScrollRect size
        /// </summary>
        public float scrollXBuffer;
        
        public float scrollYBuffer;
 
        protected ScrollRect m_ScrollRect;
        protected RectTransform m_ScrollRectTransform;
 
        protected bool m_OverrideScrolling,
                       m_HasRightBuffer;
 
        public void SetHasRightBuffer(bool rightBuffer)
        {
            m_HasRightBuffer = rightBuffer;
        }
 
        /// <summary>
        /// If appropriate, we cache ScrollRect reference, disable it and enable scrolling override
        /// </summary>
        void Start()
        {
#if UNITY_STANDALONE || UNITY_EDITOR
            m_ScrollRect = GetComponent<ScrollRect>();
            m_ScrollRect.enabled = false;
            m_OverrideScrolling = true;
            m_ScrollRectTransform = (RectTransform) m_ScrollRect.transform;
#else
            m_OverrideScrolling = false;
#endif
        }
 
        /// <summary>
        ///  Use normalized mouse position inside ScrollRect to scroll
        /// </summary>
        void Update()
        {
            if (!m_OverrideScrolling)
            {
                return;
            }
            Vector3 mousePosition = UnityInput.mousePosition;
 
            // only scroll if mouse is inside ScrollRect
            bool inside = RectTransformUtility.RectangleContainsScreenPoint(m_ScrollRectTransform, mousePosition);
            if (!inside)
            {
                return;
            }
 
            Rect rect = m_ScrollRectTransform.rect;
            float adjustmentX = rect.width * scrollXBuffer,
                  adjustmentY = rect.height * scrollYBuffer;
 
            Vector2 localPoint;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ScrollRectTransform, mousePosition, null, out localPoint);
 
            Vector2 pivot = m_ScrollRectTransform.pivot;
            float x = (localPoint.x + (rect.width - adjustmentX) * pivot.x) / (rect.width - 2 * adjustmentX);
            float y = (localPoint.y + (rect.height - adjustmentY) * pivot.y) / (rect.height - 2 * adjustmentY);
 
            if (clampScroll)
            {
                x = Mathf.Clamp01(x);
                y = Mathf.Clamp01(y);
            }
 
            m_ScrollRect.normalizedPosition = new Vector2(x, y);
        }
 
        /// <summary>
        /// Called when a button inside the scroll is selected
        /// </summary>
        /// <param name="levelSelectButton">Selected child</param>
        public void SelectChild(LevelSelectButton levelSelectButton)
        {
            // minus one if  buffer
            int childCount = levelSelectButton.transform.parent.childCount - (m_HasRightBuffer ? 1 : 0);
            if (childCount > 1)
            {
                float normalized = (float)levelSelectButton.transform.GetSiblingIndex() / ( childCount - 1);
                m_ScrollRect.normalizedPosition = new Vector2(normalized, 0);
            }
        }
    }
}