Advertisement
Guest User

ScrollRectWorkaround

a guest
Feb 16th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. //THIS IS CREATED TO ADDRESS A BUG WEHERE IF THERE IS AN SCROLLBAR ATTACHED TO THE SCROLLRECT THE INERTIA FUCKS UP
  6.  
  7. public class ScrollRectWorkaround : MonoBehaviour {
  8.  
  9. [SerializeField]
  10. Scrollbar m_scrollBar;
  11.  
  12. [SerializeField]
  13. ScrollRect m_scrollRect;
  14.  
  15. private Bounds m_ContentBounds;
  16. private Bounds m_ViewBounds;
  17.  
  18. // Use this for initialization
  19. void Start()
  20. {
  21. m_scrollRect.onValueChanged.AddListener(UpdateScrollbar);
  22. m_scrollBar.onValueChanged.AddListener(UpdateScrollRect);
  23. UpdateScrollbar(m_scrollRect.normalizedPosition);
  24. UpdateScrollRect(m_scrollBar.value);
  25. }
  26.  
  27. //ScrollBar Updates ScrollRect
  28. public void UpdateScrollRect(float value)
  29. {
  30. if (m_scrollRect.horizontal)
  31. {
  32. m_scrollRect.normalizedPosition = new Vector2(value, m_scrollRect.normalizedPosition.y);
  33. }
  34. else if(m_scrollRect.vertical)
  35. {
  36. m_scrollRect.normalizedPosition = new Vector2(m_scrollRect.normalizedPosition.x, value);
  37. }
  38. }
  39.  
  40.  
  41. //Scroll Rect Updates ScrollBar
  42. public void UpdateScrollbar(Vector2 normalizedPosition)
  43. {
  44. //setting offset to zero temporarily.
  45. UpdateBounds();
  46. Vector2 offset = Vector2.zero;
  47.  
  48. if (m_scrollRect.horizontal)
  49. {
  50. if (m_ContentBounds.size.x > 0)
  51. m_scrollBar.size = Mathf.Clamp01((m_ViewBounds.size.x - Mathf.Abs( offset.x)) / m_ContentBounds.size.x);
  52. else
  53. m_scrollBar.size = 1;
  54.  
  55. m_scrollBar.value = normalizedPosition.x;
  56. }
  57.  
  58. else if (m_scrollRect.vertical)
  59. {
  60. if (m_ContentBounds.size.y > 0)
  61. m_scrollBar.size = Mathf.Clamp01((m_ViewBounds.size.y - Mathf.Abs(offset.y)) / m_ContentBounds.size.y);
  62. else
  63. m_scrollBar.size = 1;
  64.  
  65. m_scrollBar.value = normalizedPosition.y;
  66. }
  67. }
  68.  
  69. private readonly Vector3[] m_Corners = new Vector3[4];
  70. private void UpdateBounds()
  71. {
  72. if (m_scrollRect.content == null)
  73. {
  74. m_ContentBounds = new Bounds();
  75. return;
  76. }
  77.  
  78. //UpdateContentBounds
  79.  
  80. m_scrollRect.content.GetWorldCorners(m_Corners);
  81.  
  82. RectTransform ViewRect = null;
  83. ViewRect = m_scrollRect.viewport;
  84.  
  85. if (ViewRect == null)
  86. ViewRect = (RectTransform)m_scrollRect.transform;
  87.  
  88. var viewWorldToLocalMatrix = ViewRect.worldToLocalMatrix;
  89. m_ContentBounds = InternalGetBounds(m_Corners, ref viewWorldToLocalMatrix);
  90.  
  91. //UpdateViewBounds
  92.  
  93. m_ViewBounds = new Bounds(ViewRect.rect.center, ViewRect.rect.size);
  94. }
  95.  
  96. internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
  97. {
  98. var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  99. var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  100.  
  101. for (int j = 0; j < 4; j++)
  102. {
  103. Vector3 v = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[j]);
  104. vMin = Vector3.Min(v, vMin);
  105. vMax = Vector3.Max(v, vMax);
  106. }
  107.  
  108. var bounds = new Bounds(vMin, Vector3.zero);
  109. bounds.Encapsulate(vMax);
  110. return bounds;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement