Tanoda
UIScrollToSelectionXY.cs
Go to the documentation of this file.
1
5
6/*USAGE:
7Simply place the script on the ScrollRect that contains the selectable children we'll be scrolling to
8and drag'n'drop the RectTransform of the options "container" that we'll be scrolling.*/
9
11
13{
14 [AddComponentMenu("UI/Extensions/UI ScrollTo Selection XY")]
15 [RequireComponent(typeof(ScrollRect))]
16 public class UIScrollToSelectionXY : MonoBehaviour
17 {
18
19 #region Variables
20
21 // settings
22 public float scrollSpeed = 10f;
23
24 [SerializeField]
25 private RectTransform layoutListGroup = null;
26
27 // temporary variables
28 private RectTransform targetScrollObject;
29 private bool scrollToSelection = true;
30
31 // references
32 private RectTransform scrollWindow = null;
33 private ScrollRect targetScrollRect = null;
34 #endregion
35
36 // Use this for initialization
37 private void Start()
38 {
39 targetScrollRect = GetComponent<ScrollRect>();
40 scrollWindow = targetScrollRect.GetComponent<RectTransform>();
41 }
42
43 // Update is called once per frame
44 private void Update()
45 {
46 ScrollRectToLevelSelection();
47 }
48
49 private void ScrollRectToLevelSelection()
50 {
51 // FIX: if you do not do that here events can have null value
52 var events = EventSystem.current;
53
54 // check main references
55 bool referencesAreIncorrect =
56 (targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
57 if (referencesAreIncorrect == true)
58 {
59 return;
60 }
61
62 // get calculation references
63 RectTransform selection = events.currentSelectedGameObject != null ?
64 events.currentSelectedGameObject.GetComponent<RectTransform>() :
65 null;
66
67 if (selection != targetScrollObject)
68 {
69 scrollToSelection = true;
70 }
71
72 // check if scrolling is possible
73 bool isScrollDirectionUnknown = (selection == null || scrollToSelection == false);
74
75 if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
76 {
77 return;
78 }
79
80 bool finishedX = false, finishedY = false;
81
82 if (targetScrollRect.vertical)
83 {
84 // move the current scroll rect to correct position
85 float selectionPos = -selection.anchoredPosition.y;
86
87 float listPixelAnchor = layoutListGroup.anchoredPosition.y;
88
89 // get the element offset value depending on the cursor move direction
90 float offlimitsValue = 0;
91
92 offlimitsValue = listPixelAnchor - selectionPos;
93 // move the target scroll rect
94 targetScrollRect.verticalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
95
96 finishedY = Mathf.Abs(offlimitsValue) < 2f;
97 }
98
99 if (targetScrollRect.horizontal)
100 {
101 // move the current scroll rect to correct position
102 float selectionPos = -selection.anchoredPosition.x;
103
104 float listPixelAnchor = layoutListGroup.anchoredPosition.x;
105
106 // get the element offset value depending on the cursor move direction
107 float offlimitsValue = 0;
108
109 offlimitsValue = listPixelAnchor - selectionPos;
110 // move the target scroll rect
111 targetScrollRect.horizontalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.x) * Time.deltaTime * scrollSpeed;
112
113 finishedX = Mathf.Abs(offlimitsValue) < 2f;
114 }
115 // check if we reached our destination
116 if (finishedX && finishedY) {
117 scrollToSelection = false;
118 }
119 // save last object we were "heading to" to prevent blocking
120 targetScrollObject = selection;
121 }
122 }
123}
Credit Erdener Gonenc - @PixelEnvision.