Tanoda
UI_ScrollRectOcclusion.cs
Go to the documentation of this file.
1
4
5using System.Collections.Generic;
6
8{
24
25 [AddComponentMenu("UI/Extensions/UI Scrollrect Occlusion")]
26 public class UI_ScrollRectOcclusion : MonoBehaviour
27 {
28 //if true user will need to call Init() method manually (in case the contend of the scrollview is generated from code or requires special initialization)
29 public bool InitByUser = false;
30 private ScrollRect _scrollRect;
31 private ContentSizeFitter _contentSizeFitter;
32 private VerticalLayoutGroup _verticalLayoutGroup;
33 private HorizontalLayoutGroup _horizontalLayoutGroup;
34 private GridLayoutGroup _gridLayoutGroup;
35 private bool _isVertical = false;
36 private bool _isHorizontal = false;
37 private float _disableMarginX = 0;
38 private float _disableMarginY = 0;
39 private bool hasDisabledGridComponents = false;
40 private List<RectTransform> items = new List<RectTransform>();
41
42 void Awake()
43 {
44 if (InitByUser)
45 return;
46
47 Init();
48
49 }
50
51 public void Init()
52 {
53 if (GetComponent<ScrollRect>() != null)
54 {
55 _scrollRect = GetComponent<ScrollRect>();
56 _scrollRect.onValueChanged.AddListener(OnScroll);
57
58 _isHorizontal = _scrollRect.horizontal;
59 _isVertical = _scrollRect.vertical;
60
61 for (int i = 0; i < _scrollRect.content.childCount; i++)
62 {
63 items.Add(_scrollRect.content.GetChild(i).GetComponent<RectTransform>());
64 }
65 if (_scrollRect.content.GetComponent<VerticalLayoutGroup>() != null)
66 {
67 _verticalLayoutGroup = _scrollRect.content.GetComponent<VerticalLayoutGroup>();
68 }
69 if (_scrollRect.content.GetComponent<HorizontalLayoutGroup>() != null)
70 {
71 _horizontalLayoutGroup = _scrollRect.content.GetComponent<HorizontalLayoutGroup>();
72 }
73 if (_scrollRect.content.GetComponent<GridLayoutGroup>() != null)
74 {
75 _gridLayoutGroup = _scrollRect.content.GetComponent<GridLayoutGroup>();
76 }
77 if (_scrollRect.content.GetComponent<ContentSizeFitter>() != null)
78 {
79 _contentSizeFitter = _scrollRect.content.GetComponent<ContentSizeFitter>();
80 }
81
82 }
83 else
84 {
85 Debug.LogError("UI_ScrollRectOcclusion => No ScrollRect component found");
86 }
87 }
88
89 void DisableGridComponents()
90 {
91 if (_isVertical)
92 _disableMarginY = _scrollRect.GetComponent<RectTransform>().rect.height / 2 + items[0].sizeDelta.y;
93
94 if (_isHorizontal)
95 _disableMarginX = _scrollRect.GetComponent<RectTransform>().rect.width / 2 + items[0].sizeDelta.x;
96
97 if (_verticalLayoutGroup)
98 {
99 _verticalLayoutGroup.enabled = false;
100 }
101 if (_horizontalLayoutGroup)
102 {
103 _horizontalLayoutGroup.enabled = false;
104 }
105 if (_contentSizeFitter)
106 {
107 _contentSizeFitter.enabled = false;
108 }
109 if (_gridLayoutGroup)
110 {
111 _gridLayoutGroup.enabled = false;
112 }
113 hasDisabledGridComponents = true;
114 }
115
116 public void OnScroll(Vector2 pos)
117 {
118
119 if (!hasDisabledGridComponents)
120 DisableGridComponents();
121
122 for (int i = 0; i < items.Count; i++)
123 {
124 if (_isVertical && _isHorizontal)
125 {
126 if (_scrollRect.transform.InverseTransformPoint(items[i].position).y < -_disableMarginY || _scrollRect.transform.InverseTransformPoint(items[i].position).y > _disableMarginY
127 || _scrollRect.transform.InverseTransformPoint(items[i].position).x < -_disableMarginX || _scrollRect.transform.InverseTransformPoint(items[i].position).x > _disableMarginX)
128 {
129 items[i].gameObject.SetActive(false);
130 }
131 else
132 {
133 items[i].gameObject.SetActive(true);
134 }
135 }
136 else
137 {
138 if (_isVertical)
139 {
140 if (_scrollRect.transform.InverseTransformPoint(items[i].position).y < -_disableMarginY || _scrollRect.transform.InverseTransformPoint(items[i].position).y > _disableMarginY)
141 {
142 items[i].gameObject.SetActive(false);
143 }
144 else
145 {
146 items[i].gameObject.SetActive(true);
147 }
148 }
149
150 if (_isHorizontal)
151 {
152 if (_scrollRect.transform.InverseTransformPoint(items[i].position).x < -_disableMarginX || _scrollRect.transform.InverseTransformPoint(items[i].position).x > _disableMarginX)
153 {
154 items[i].gameObject.SetActive(false);
155 }
156 else
157 {
158 items[i].gameObject.SetActive(true);
159 }
160 }
161 }
162 }
163 }
164 }
165}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
ScrollRectOcclusion - disables the objects outside of the scrollrect viewport. Useful for scrolls wit...
Credit Erdener Gonenc - @PixelEnvision.