UICenterOnChild.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Interaction/Center Scroll View on Child")]
  5. public class UICenterOnChild : MonoBehaviour
  6. {
  7. public GameObject centeredObject
  8. {
  9. get
  10. {
  11. return this.mCenteredObject;
  12. }
  13. }
  14. private void Start()
  15. {
  16. this.Recenter();
  17. }
  18. private void OnEnable()
  19. {
  20. if (this.mScrollView)
  21. {
  22. this.mScrollView.centerOnChild = this;
  23. this.Recenter();
  24. }
  25. }
  26. private void OnDisable()
  27. {
  28. if (this.mScrollView)
  29. {
  30. this.mScrollView.centerOnChild = null;
  31. }
  32. }
  33. private void OnDragFinished()
  34. {
  35. if (base.enabled)
  36. {
  37. this.Recenter();
  38. }
  39. }
  40. private void OnValidate()
  41. {
  42. this.nextPageThreshold = Mathf.Abs(this.nextPageThreshold);
  43. }
  44. [ContextMenu("Execute")]
  45. public void Recenter()
  46. {
  47. if (this.mScrollView == null)
  48. {
  49. this.mScrollView = NGUITools.FindInParents<UIScrollView>(base.gameObject);
  50. if (this.mScrollView == null)
  51. {
  52. Debug.LogWarning(string.Concat(new object[]
  53. {
  54. base.GetType(),
  55. " requires ",
  56. typeof(UIScrollView),
  57. " on a parent object in order to work"
  58. }), this);
  59. base.enabled = false;
  60. return;
  61. }
  62. if (this.mScrollView)
  63. {
  64. this.mScrollView.centerOnChild = this;
  65. UIScrollView uiscrollView = this.mScrollView;
  66. uiscrollView.onDragFinished = (UIScrollView.OnDragNotification)Delegate.Combine(uiscrollView.onDragFinished, new UIScrollView.OnDragNotification(this.OnDragFinished));
  67. }
  68. if (this.mScrollView.horizontalScrollBar != null)
  69. {
  70. UIProgressBar horizontalScrollBar = this.mScrollView.horizontalScrollBar;
  71. horizontalScrollBar.onDragFinished = (UIProgressBar.OnDragFinished)Delegate.Combine(horizontalScrollBar.onDragFinished, new UIProgressBar.OnDragFinished(this.OnDragFinished));
  72. }
  73. if (this.mScrollView.verticalScrollBar != null)
  74. {
  75. UIProgressBar verticalScrollBar = this.mScrollView.verticalScrollBar;
  76. verticalScrollBar.onDragFinished = (UIProgressBar.OnDragFinished)Delegate.Combine(verticalScrollBar.onDragFinished, new UIProgressBar.OnDragFinished(this.OnDragFinished));
  77. }
  78. }
  79. if (this.mScrollView.panel == null)
  80. {
  81. return;
  82. }
  83. Transform transform = base.transform;
  84. if (transform.childCount == 0)
  85. {
  86. return;
  87. }
  88. Vector3[] worldCorners = this.mScrollView.panel.worldCorners;
  89. Vector3 vector = (worldCorners[2] + worldCorners[0]) * 0.5f;
  90. Vector3 vector2 = this.mScrollView.currentMomentum * this.mScrollView.momentumAmount;
  91. Vector3 a = NGUIMath.SpringDampen(ref vector2, 9f, 2f);
  92. Vector3 b = vector - a * 0.01f;
  93. float num = float.MaxValue;
  94. Transform target = null;
  95. int index = 0;
  96. int num2 = 0;
  97. int i = 0;
  98. int childCount = transform.childCount;
  99. int num3 = 0;
  100. while (i < childCount)
  101. {
  102. Transform child = transform.GetChild(i);
  103. if (child.gameObject.activeInHierarchy)
  104. {
  105. float num4 = Vector3.SqrMagnitude(child.position - b);
  106. if (num4 < num)
  107. {
  108. num = num4;
  109. target = child;
  110. index = i;
  111. num2 = num3;
  112. }
  113. num3++;
  114. }
  115. i++;
  116. }
  117. if (this.nextPageThreshold > 0f && UICamera.currentTouch != null && this.mCenteredObject != null && this.mCenteredObject.transform == transform.GetChild(index))
  118. {
  119. Vector2 totalDelta = UICamera.currentTouch.totalDelta;
  120. UIScrollView.Movement movement = this.mScrollView.movement;
  121. float num5;
  122. if (movement != UIScrollView.Movement.Horizontal)
  123. {
  124. if (movement != UIScrollView.Movement.Vertical)
  125. {
  126. num5 = totalDelta.magnitude;
  127. }
  128. else
  129. {
  130. num5 = totalDelta.y;
  131. }
  132. }
  133. else
  134. {
  135. num5 = totalDelta.x;
  136. }
  137. if (Mathf.Abs(num5) > this.nextPageThreshold)
  138. {
  139. UIGrid component = base.GetComponent<UIGrid>();
  140. if (component != null && component.sorting != UIGrid.Sorting.None)
  141. {
  142. List<Transform> childList = component.GetChildList();
  143. if (num5 > this.nextPageThreshold)
  144. {
  145. if (num2 > 0)
  146. {
  147. target = childList[num2 - 1];
  148. }
  149. else
  150. {
  151. target = childList[0];
  152. }
  153. }
  154. else if (num5 < -this.nextPageThreshold)
  155. {
  156. if (num2 < childList.Count - 1)
  157. {
  158. target = childList[num2 + 1];
  159. }
  160. else
  161. {
  162. target = childList[childList.Count - 1];
  163. }
  164. }
  165. }
  166. else
  167. {
  168. Debug.LogWarning("Next Page Threshold requires a sorted UIGrid in order to work properly", this);
  169. }
  170. }
  171. }
  172. this.CenterOn(target, vector);
  173. }
  174. private void CenterOn(Transform target, Vector3 panelCenter)
  175. {
  176. if (target != null && this.mScrollView != null && this.mScrollView.panel != null)
  177. {
  178. Transform cachedTransform = this.mScrollView.panel.cachedTransform;
  179. this.mCenteredObject = target.gameObject;
  180. Vector3 a = cachedTransform.InverseTransformPoint(target.position);
  181. Vector3 b = cachedTransform.InverseTransformPoint(panelCenter);
  182. Vector3 b2 = a - b;
  183. if (!this.mScrollView.canMoveHorizontally)
  184. {
  185. b2.x = 0f;
  186. }
  187. if (!this.mScrollView.canMoveVertically)
  188. {
  189. b2.y = 0f;
  190. }
  191. b2.z = 0f;
  192. SpringPanel.Begin(this.mScrollView.panel.cachedGameObject, cachedTransform.localPosition - b2, this.springStrength).onFinished = this.onFinished;
  193. }
  194. else
  195. {
  196. this.mCenteredObject = null;
  197. }
  198. if (this.onCenter != null)
  199. {
  200. this.onCenter(this.mCenteredObject);
  201. }
  202. }
  203. public void CenterOn(Transform target)
  204. {
  205. if (this.mScrollView != null && this.mScrollView.panel != null)
  206. {
  207. Vector3[] worldCorners = this.mScrollView.panel.worldCorners;
  208. Vector3 panelCenter = (worldCorners[2] + worldCorners[0]) * 0.5f;
  209. this.CenterOn(target, panelCenter);
  210. }
  211. }
  212. public float springStrength = 8f;
  213. public float nextPageThreshold;
  214. public SpringPanel.OnFinished onFinished;
  215. public UICenterOnChild.OnCenterCallback onCenter;
  216. private UIScrollView mScrollView;
  217. private GameObject mCenteredObject;
  218. public delegate void OnCenterCallback(GameObject centeredObject);
  219. }