Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- //THIS IS CREATED TO ADDRESS A BUG WEHERE IF THERE IS AN SCROLLBAR ATTACHED TO THE SCROLLRECT THE INERTIA FUCKS UP
- public class ScrollRectWorkaround : MonoBehaviour {
- [SerializeField]
- Scrollbar m_scrollBar;
- [SerializeField]
- ScrollRect m_scrollRect;
- private Bounds m_ContentBounds;
- private Bounds m_ViewBounds;
- // Use this for initialization
- void Start()
- {
- m_scrollRect.onValueChanged.AddListener(UpdateScrollbar);
- m_scrollBar.onValueChanged.AddListener(UpdateScrollRect);
- UpdateScrollbar(m_scrollRect.normalizedPosition);
- UpdateScrollRect(m_scrollBar.value);
- }
- //ScrollBar Updates ScrollRect
- public void UpdateScrollRect(float value)
- {
- if (m_scrollRect.horizontal)
- {
- m_scrollRect.normalizedPosition = new Vector2(value, m_scrollRect.normalizedPosition.y);
- }
- else if(m_scrollRect.vertical)
- {
- m_scrollRect.normalizedPosition = new Vector2(m_scrollRect.normalizedPosition.x, value);
- }
- }
- //Scroll Rect Updates ScrollBar
- public void UpdateScrollbar(Vector2 normalizedPosition)
- {
- //setting offset to zero temporarily.
- UpdateBounds();
- Vector2 offset = Vector2.zero;
- if (m_scrollRect.horizontal)
- {
- if (m_ContentBounds.size.x > 0)
- m_scrollBar.size = Mathf.Clamp01((m_ViewBounds.size.x - Mathf.Abs( offset.x)) / m_ContentBounds.size.x);
- else
- m_scrollBar.size = 1;
- m_scrollBar.value = normalizedPosition.x;
- }
- else if (m_scrollRect.vertical)
- {
- if (m_ContentBounds.size.y > 0)
- m_scrollBar.size = Mathf.Clamp01((m_ViewBounds.size.y - Mathf.Abs(offset.y)) / m_ContentBounds.size.y);
- else
- m_scrollBar.size = 1;
- m_scrollBar.value = normalizedPosition.y;
- }
- }
- private readonly Vector3[] m_Corners = new Vector3[4];
- private void UpdateBounds()
- {
- if (m_scrollRect.content == null)
- {
- m_ContentBounds = new Bounds();
- return;
- }
- //UpdateContentBounds
- m_scrollRect.content.GetWorldCorners(m_Corners);
- RectTransform ViewRect = null;
- ViewRect = m_scrollRect.viewport;
- if (ViewRect == null)
- ViewRect = (RectTransform)m_scrollRect.transform;
- var viewWorldToLocalMatrix = ViewRect.worldToLocalMatrix;
- m_ContentBounds = InternalGetBounds(m_Corners, ref viewWorldToLocalMatrix);
- //UpdateViewBounds
- m_ViewBounds = new Bounds(ViewRect.rect.center, ViewRect.rect.size);
- }
- internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
- {
- var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
- var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
- for (int j = 0; j < 4; j++)
- {
- Vector3 v = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[j]);
- vMin = Vector3.Min(v, vMin);
- vMax = Vector3.Max(v, vMax);
- }
- var bounds = new Bounds(vMin, Vector3.zero);
- bounds.Encapsulate(vMax);
- return bounds;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement