ExtensionScrollRect.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. public enum LocatePos
  6. {
  7. Left,
  8. Right,
  9. Center,
  10. Up,
  11. Down,
  12. Middle,
  13. }
  14. public static class ExtensionScrollRect
  15. {
  16. public static MoveRoot Locate(this ScrollRect scrollRect, int index, float duration, Curve curve, LocatePos locatePos)
  17. {
  18. LayoutGroup layoutGroup = scrollRect.content.GetComponent<LayoutGroup>();
  19. RectTransform tra1 = scrollRect.GetComponent<RectTransform>();
  20. Rect rect1 = tra1.rect;
  21. RectTransform tra2 = scrollRect.content.GetChild(index).GetComponent<RectTransform>();
  22. Rect rect2 = tra2.rect;
  23. if (locatePos == LocatePos.Up)
  24. {
  25. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMax, 0);
  26. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMax - layoutGroup.padding.top, 0);
  27. Vector3 offset = targetPos - itemPos;
  28. offset.x = 0;
  29. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  30. }
  31. if (locatePos == LocatePos.Down)
  32. {
  33. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMin, 0);
  34. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMin + layoutGroup.padding.bottom, 0);
  35. Vector3 offset = targetPos - itemPos;
  36. offset.x = 0;
  37. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  38. }
  39. else if (locatePos == LocatePos.Middle)
  40. {
  41. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  42. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  43. Vector3 offset = targetPos - itemPos;
  44. offset.x = 0;
  45. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  46. }
  47. if (locatePos == LocatePos.Left)
  48. {
  49. Vector3 itemPos = tra2.position + new Vector3(rect2.xMin, 0, 0);
  50. Vector3 targetPos = tra1.position + new Vector3(rect1.xMin + layoutGroup.padding.left, 0, 0);
  51. Vector3 offset = targetPos - itemPos;
  52. offset.y = 0;
  53. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  54. }
  55. if (locatePos == LocatePos.Right)
  56. {
  57. Vector3 itemPos = tra2.position + new Vector3(rect2.xMax, 0, 0);
  58. Vector3 targetPos = tra1.position + new Vector3(rect1.xMax - layoutGroup.padding.right, 0, 0);
  59. Vector3 offset = targetPos - itemPos;
  60. offset.y = 0;
  61. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  62. }
  63. if (locatePos == LocatePos.Center)
  64. {
  65. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  66. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  67. Vector3 offset = targetPos - itemPos;
  68. offset.y = 0;
  69. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  70. }
  71. throw new Exception();
  72. }
  73. public static MoveRoot Locate(this ScrollRect scrollRect, Transform item, float duration, Curve curve, LocatePos locatePos)
  74. {
  75. return scrollRect.Locate(item.GetSiblingIndex(), duration, curve, locatePos);
  76. }
  77. }