123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Center Scroll View on Child")]
- public class UICenterOnChild : MonoBehaviour
- {
- public GameObject centeredObject
- {
- get
- {
- return this.mCenteredObject;
- }
- }
- private void Start()
- {
- this.Recenter();
- }
- private void OnEnable()
- {
- if (this.mScrollView)
- {
- this.mScrollView.centerOnChild = this;
- this.Recenter();
- }
- }
- private void OnDisable()
- {
- if (this.mScrollView)
- {
- this.mScrollView.centerOnChild = null;
- }
- }
- private void OnDragFinished()
- {
- if (base.enabled)
- {
- this.Recenter();
- }
- }
- private void OnValidate()
- {
- this.nextPageThreshold = Mathf.Abs(this.nextPageThreshold);
- }
- [ContextMenu("Execute")]
- public void Recenter()
- {
- if (this.mScrollView == null)
- {
- this.mScrollView = NGUITools.FindInParents<UIScrollView>(base.gameObject);
- if (this.mScrollView == null)
- {
- Debug.LogWarning(string.Concat(new object[]
- {
- base.GetType(),
- " requires ",
- typeof(UIScrollView),
- " on a parent object in order to work"
- }), this);
- base.enabled = false;
- return;
- }
- if (this.mScrollView)
- {
- this.mScrollView.centerOnChild = this;
- UIScrollView uiscrollView = this.mScrollView;
- uiscrollView.onDragFinished = (UIScrollView.OnDragNotification)Delegate.Combine(uiscrollView.onDragFinished, new UIScrollView.OnDragNotification(this.OnDragFinished));
- }
- if (this.mScrollView.horizontalScrollBar != null)
- {
- UIProgressBar horizontalScrollBar = this.mScrollView.horizontalScrollBar;
- horizontalScrollBar.onDragFinished = (UIProgressBar.OnDragFinished)Delegate.Combine(horizontalScrollBar.onDragFinished, new UIProgressBar.OnDragFinished(this.OnDragFinished));
- }
- if (this.mScrollView.verticalScrollBar != null)
- {
- UIProgressBar verticalScrollBar = this.mScrollView.verticalScrollBar;
- verticalScrollBar.onDragFinished = (UIProgressBar.OnDragFinished)Delegate.Combine(verticalScrollBar.onDragFinished, new UIProgressBar.OnDragFinished(this.OnDragFinished));
- }
- }
- if (this.mScrollView.panel == null)
- {
- return;
- }
- Transform transform = base.transform;
- if (transform.childCount == 0)
- {
- return;
- }
- Vector3[] worldCorners = this.mScrollView.panel.worldCorners;
- Vector3 vector = (worldCorners[2] + worldCorners[0]) * 0.5f;
- Vector3 vector2 = this.mScrollView.currentMomentum * this.mScrollView.momentumAmount;
- Vector3 a = NGUIMath.SpringDampen(ref vector2, 9f, 2f);
- Vector3 b = vector - a * 0.01f;
- float num = float.MaxValue;
- Transform target = null;
- int index = 0;
- int num2 = 0;
- int i = 0;
- int childCount = transform.childCount;
- int num3 = 0;
- while (i < childCount)
- {
- Transform child = transform.GetChild(i);
- if (child.gameObject.activeInHierarchy)
- {
- float num4 = Vector3.SqrMagnitude(child.position - b);
- if (num4 < num)
- {
- num = num4;
- target = child;
- index = i;
- num2 = num3;
- }
- num3++;
- }
- i++;
- }
- if (this.nextPageThreshold > 0f && UICamera.currentTouch != null && this.mCenteredObject != null && this.mCenteredObject.transform == transform.GetChild(index))
- {
- Vector2 totalDelta = UICamera.currentTouch.totalDelta;
- UIScrollView.Movement movement = this.mScrollView.movement;
- float num5;
- if (movement != UIScrollView.Movement.Horizontal)
- {
- if (movement != UIScrollView.Movement.Vertical)
- {
- num5 = totalDelta.magnitude;
- }
- else
- {
- num5 = totalDelta.y;
- }
- }
- else
- {
- num5 = totalDelta.x;
- }
- if (Mathf.Abs(num5) > this.nextPageThreshold)
- {
- UIGrid component = base.GetComponent<UIGrid>();
- if (component != null && component.sorting != UIGrid.Sorting.None)
- {
- List<Transform> childList = component.GetChildList();
- if (num5 > this.nextPageThreshold)
- {
- if (num2 > 0)
- {
- target = childList[num2 - 1];
- }
- else
- {
- target = childList[0];
- }
- }
- else if (num5 < -this.nextPageThreshold)
- {
- if (num2 < childList.Count - 1)
- {
- target = childList[num2 + 1];
- }
- else
- {
- target = childList[childList.Count - 1];
- }
- }
- }
- else
- {
- Debug.LogWarning("Next Page Threshold requires a sorted UIGrid in order to work properly", this);
- }
- }
- }
- this.CenterOn(target, vector);
- }
- private void CenterOn(Transform target, Vector3 panelCenter)
- {
- if (target != null && this.mScrollView != null && this.mScrollView.panel != null)
- {
- Transform cachedTransform = this.mScrollView.panel.cachedTransform;
- this.mCenteredObject = target.gameObject;
- Vector3 a = cachedTransform.InverseTransformPoint(target.position);
- Vector3 b = cachedTransform.InverseTransformPoint(panelCenter);
- Vector3 b2 = a - b;
- if (!this.mScrollView.canMoveHorizontally)
- {
- b2.x = 0f;
- }
- if (!this.mScrollView.canMoveVertically)
- {
- b2.y = 0f;
- }
- b2.z = 0f;
- SpringPanel.Begin(this.mScrollView.panel.cachedGameObject, cachedTransform.localPosition - b2, this.springStrength).onFinished = this.onFinished;
- }
- else
- {
- this.mCenteredObject = null;
- }
- if (this.onCenter != null)
- {
- this.onCenter(this.mCenteredObject);
- }
- }
- public void CenterOn(Transform target)
- {
- if (this.mScrollView != null && this.mScrollView.panel != null)
- {
- Vector3[] worldCorners = this.mScrollView.panel.worldCorners;
- Vector3 panelCenter = (worldCorners[2] + worldCorners[0]) * 0.5f;
- this.CenterOn(target, panelCenter);
- }
- }
- public float springStrength = 8f;
- public float nextPageThreshold;
- public SpringPanel.OnFinished onFinished;
- public UICenterOnChild.OnCenterCallback onCenter;
- private UIScrollView mScrollView;
- private GameObject mCenteredObject;
- public delegate void OnCenterCallback(GameObject centeredObject);
- }
|