ScrollViewHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using DG.Tweening;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class ScrollViewHandler : MonoBehaviour
  8. {
  9. private ScrollRect sr;
  10. [SerializeField] private int srContentCount;
  11. [SerializeField] 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. return;
  34. }
  35. // ==================================================
  36. #region 手势检测
  37. private Vector2 lastPos; // 鼠标上次位置
  38. private Vector2 currPos; // 鼠标当前位置
  39. private Vector2 offset; // 两次位置的偏移值
  40. public void BeginDrag()
  41. {
  42. lastPos = Input.mousePosition;
  43. return;
  44. }
  45. public void EndDrag()
  46. {
  47. currPos = Input.mousePosition;
  48. offset = currPos - lastPos;
  49. if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) // 水平移动
  50. {
  51. if (offset.x > 0)
  52. {
  53. // Debug.Log("向右");
  54. currentIndex--;
  55. Jump2Position(currentIndex);
  56. }
  57. else
  58. {
  59. // Debug.Log("向左");
  60. currentIndex++;
  61. Jump2Position(currentIndex);
  62. }
  63. }
  64. // else // 垂直移动
  65. // {
  66. // if (offset.y > 0)
  67. // {
  68. // Debug.Log("向上");
  69. // }
  70. // else
  71. // {
  72. // Debug.Log("向下");
  73. // }
  74. // }
  75. return;
  76. }
  77. #endregion
  78. // ==================================================
  79. #region 跳转控制
  80. public void Jump2Position(int index)
  81. {
  82. if (index <= 0)
  83. {
  84. currentIndex = 0;
  85. sr.DOHorizontalNormalizedPos(0, ANIMTIME);
  86. ControllDot(0);
  87. return;
  88. }
  89. if (index >= 6)
  90. {
  91. currentIndex = 6;
  92. sr.DOHorizontalNormalizedPos(1, ANIMTIME);
  93. ControllDot(6);
  94. return;
  95. }
  96. currentIndex = index;
  97. sr.DOHorizontalNormalizedPos(unitPosition * index, ANIMTIME);
  98. ControllDot(index);
  99. return;
  100. }
  101. #endregion
  102. // ==================================================
  103. #region dot控制
  104. [SerializeField] private List<GameObject> dots;
  105. public void ControllDot(int value)
  106. {
  107. for (int i = 0; i < dots.Count; i++)
  108. {
  109. if (i == value)
  110. {
  111. dots[i].transform.DOScale(new Vector3(1.5f, 1.5f, 1.5f), ANIMTIME);
  112. dots[i].GetComponent<Image>().DOColor(Color.white, ANIMTIME);
  113. continue;
  114. }
  115. dots[i].transform.DOScale(Vector3.one, 0.5f);
  116. dots[i].GetComponent<Image>().DOColor(Color.gray, ANIMTIME);
  117. }
  118. return;
  119. }
  120. #endregion
  121. }