SpringPanel.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(UIPanel))]
  4. [AddComponentMenu("NGUI/Internal/Spring Panel")]
  5. public class SpringPanel : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.mPanel = base.GetComponent<UIPanel>();
  10. this.mDrag = base.GetComponent<UIScrollView>();
  11. this.mTrans = base.transform;
  12. }
  13. private void Update()
  14. {
  15. this.AdvanceTowardsPosition();
  16. }
  17. protected virtual void AdvanceTowardsPosition()
  18. {
  19. float deltaTime = RealTime.deltaTime;
  20. bool flag = false;
  21. Vector3 localPosition = this.mTrans.localPosition;
  22. Vector3 vector = NGUIMath.SpringLerp(this.mTrans.localPosition, this.target, this.strength, deltaTime);
  23. if ((vector - this.target).sqrMagnitude < 0.01f)
  24. {
  25. vector = this.target;
  26. base.enabled = false;
  27. flag = true;
  28. }
  29. this.mTrans.localPosition = vector;
  30. Vector3 vector2 = vector - localPosition;
  31. Vector2 clipOffset = this.mPanel.clipOffset;
  32. clipOffset.x -= vector2.x;
  33. clipOffset.y -= vector2.y;
  34. this.mPanel.clipOffset = clipOffset;
  35. if (this.mDrag != null)
  36. {
  37. this.mDrag.UpdateScrollbars(false);
  38. }
  39. if (flag && this.onFinished != null)
  40. {
  41. SpringPanel.current = this;
  42. this.onFinished();
  43. SpringPanel.current = null;
  44. }
  45. }
  46. public static SpringPanel Begin(GameObject go, Vector3 pos, float strength)
  47. {
  48. SpringPanel springPanel = go.GetComponent<SpringPanel>();
  49. if (springPanel == null)
  50. {
  51. springPanel = go.AddComponent<SpringPanel>();
  52. }
  53. springPanel.target = pos;
  54. springPanel.strength = strength;
  55. springPanel.onFinished = null;
  56. springPanel.enabled = true;
  57. return springPanel;
  58. }
  59. public static SpringPanel current;
  60. public Vector3 target = Vector3.zero;
  61. public float strength = 10f;
  62. public SpringPanel.OnFinished onFinished;
  63. private UIPanel mPanel;
  64. private Transform mTrans;
  65. private UIScrollView mDrag;
  66. public delegate void OnFinished();
  67. }