ExtensionScrollRect.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. float scaleFactor = scrollRect.GetComponent<Image>().canvas.scaleFactor;
  19. LayoutGroup layoutGroup = scrollRect.content.GetComponent<LayoutGroup>();
  20. RectTransform tra1 = scrollRect.GetComponent<RectTransform>();
  21. Rect rect1 = tra1.rect;
  22. RectTransform tra2 = scrollRect.content.GetChild(index).GetComponent<RectTransform>();
  23. Rect rect2 = tra2.rect;
  24. if (locatePos == LocatePos.Up)
  25. {
  26. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMax, 0) * scaleFactor;
  27. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMax - layoutGroup.padding.top, 0) * scaleFactor;
  28. Vector3 offset = targetPos - itemPos;
  29. offset.x = 0;
  30. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  31. }
  32. if (locatePos == LocatePos.Down)
  33. {
  34. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMin, 0) * scaleFactor;
  35. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMin + layoutGroup.padding.bottom, 0) * scaleFactor;
  36. Vector3 offset = targetPos - itemPos;
  37. offset.x = 0;
  38. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  39. }
  40. else if (locatePos == LocatePos.Middle)
  41. {
  42. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  43. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  44. Vector3 offset = targetPos - itemPos;
  45. offset.x = 0;
  46. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  47. }
  48. if (locatePos == LocatePos.Left)
  49. {
  50. Vector3 itemPos = tra2.position + new Vector3(rect2.xMin, 0, 0) * scaleFactor;
  51. Vector3 targetPos = tra1.position + new Vector3(rect1.xMin + layoutGroup.padding.left, 0, 0) * scaleFactor;
  52. Vector3 offset = targetPos - itemPos;
  53. offset.y = 0;
  54. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  55. }
  56. if (locatePos == LocatePos.Right)
  57. {
  58. Vector3 itemPos = tra2.position + new Vector3(rect2.xMax, 0, 0) * scaleFactor;
  59. Vector3 targetPos = tra1.position + new Vector3(rect1.xMax - layoutGroup.padding.right, 0, 0) * scaleFactor;
  60. Vector3 offset = targetPos - itemPos;
  61. offset.y = 0;
  62. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  63. }
  64. if (locatePos == LocatePos.Center)
  65. {
  66. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  67. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  68. Vector3 offset = targetPos - itemPos;
  69. offset.y = 0;
  70. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  71. }
  72. throw new Exception();
  73. }
  74. public static MoveRoot Locate(this ScrollRect scrollRect, Transform item, float duration, Curve curve, LocatePos locatePos)
  75. {
  76. return scrollRect.Locate(item.GetSiblingIndex(), duration, curve, locatePos);
  77. }
  78. }