ScrollViewHandler.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class ScrollViewHandler : MonoBehaviour, IBeginDragHandler, IEndDragHandler
  8. {
  9. private ScrollRect sr;
  10. [SerializeField] private int srContentCount;
  11. private float unitPosition;
  12. private const float ANIMTIME = .5f;
  13. [SerializeField] private int currentIndex = 0;
  14. // ==================================================
  15. private void Start() => Init();
  16. // private void Update()
  17. // {
  18. // if (Input.GetKeyDown(KeyCode.Q))
  19. // {
  20. // sr.DOHorizontalNormalizedPos(unitPosition, ANIMTIME);
  21. // }
  22. // if (Input.GetKeyDown(KeyCode.W))
  23. // {
  24. // sr.DOHorizontalNormalizedPos(unitPosition, ANIMTIME);
  25. // }
  26. // }
  27. // ==================================================
  28. private void Init()
  29. {
  30. sr = GetComponent<ScrollRect>();
  31. srContentCount = transform.GetChild(0).GetChild(0).childCount;
  32. unitPosition = 1f / (srContentCount - 1);
  33. }
  34. // ==================================================
  35. #region 手势检测
  36. private Vector2 lastPos; // 鼠标上次位置
  37. private Vector2 currPos; // 鼠标当前位置
  38. private Vector2 offset; // 两次位置的偏移值
  39. public void OnBeginDrag(PointerEventData eventData) => BeginDrag();
  40. public void OnEndDrag(PointerEventData eventData) => EndDrag();
  41. private void BeginDrag() => lastPos = Input.mousePosition;
  42. private void EndDrag()
  43. {
  44. currPos = Input.mousePosition;
  45. offset = currPos - lastPos;
  46. if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) // 水平移动
  47. {
  48. if (offset.x > 0)
  49. {
  50. // Debug.Log("向右");
  51. currentIndex--;
  52. Jump2Position(currentIndex);
  53. }
  54. else
  55. {
  56. // Debug.Log("向左");
  57. currentIndex++;
  58. Jump2Position(currentIndex);
  59. }
  60. }
  61. // else // 垂直移动
  62. // {
  63. // if (offset.y > 0)
  64. // {
  65. // Debug.Log("向上");
  66. // }
  67. // else
  68. // {
  69. // Debug.Log("向下");
  70. // }
  71. // }
  72. }
  73. #endregion
  74. // ==================================================
  75. #region 跳转控制
  76. public void Jump2Position(int index)
  77. {
  78. if (index <= 0)
  79. {
  80. currentIndex = 0;
  81. sr.DOHorizontalNormalizedPos(0, ANIMTIME);
  82. ControllDot(0);
  83. return;
  84. }
  85. if (index >= 6)
  86. {
  87. currentIndex = 6;
  88. sr.DOHorizontalNormalizedPos(1, ANIMTIME);
  89. ControllDot(6);
  90. return;
  91. }
  92. currentIndex = index;
  93. sr.DOHorizontalNormalizedPos(unitPosition * index, ANIMTIME);
  94. ControllDot(index);
  95. }
  96. #endregion
  97. // ==================================================
  98. #region dot控制
  99. [SerializeField] private List<GameObject> dots;
  100. public void ControllDot(int value)
  101. {
  102. for (int i = 0; i < dots.Count; i++)
  103. {
  104. if (i == value)
  105. {
  106. dots[i].transform.DOScale(new Vector3(1.5f, 1.5f, 1.5f), ANIMTIME);
  107. dots[i].GetComponent<Image>().DOColor(Color.white, ANIMTIME);
  108. continue;
  109. }
  110. dots[i].transform.DOScale(Vector3.one, 0.5f);
  111. dots[i].GetComponent<Image>().DOColor(Color.gray, ANIMTIME);
  112. }
  113. }
  114. #endregion
  115. }