1
0

ScrollViewHandler.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. using UnityEngine.Events;
  8. namespace VolkswagenIDUNYXMusicPlayer
  9. {
  10. public class ScrollViewHandler : MonoBehaviour
  11. {
  12. public static ScrollViewHandler Instance;
  13. public UnityAction<int> OnContentIndexChange;
  14. private ScrollRect scrollRect;
  15. private Vector2 scrollviewLocation;
  16. private int currentContentIndex = 0;
  17. private const float animTime = .66f;
  18. // ==================================================
  19. private void Awake()
  20. {
  21. Instance = this;
  22. }
  23. private void Start()
  24. {
  25. Init();
  26. }
  27. // ==================================================
  28. private void Init()
  29. {
  30. scrollRect = GetComponent<ScrollRect>();
  31. return;
  32. }
  33. public void GetVector2Location(Vector2 value)
  34. {
  35. scrollviewLocation = value;
  36. return;
  37. }
  38. public void AdjustView()
  39. {
  40. int newContentIndex = (int)Math.Round(scrollviewLocation.x, 0);
  41. scrollRect.horizontal = false;
  42. scrollRect.DOHorizontalNormalizedPos(newContentIndex, animTime).OnComplete(() =>
  43. {
  44. scrollRect.horizontal = true;
  45. if (currentContentIndex == newContentIndex) // 无变化
  46. {
  47. return;
  48. }
  49. else
  50. {
  51. currentContentIndex = newContentIndex;
  52. if (OnContentIndexChange != null)
  53. {
  54. OnContentIndexChange(newContentIndex);
  55. }
  56. }
  57. });
  58. return;
  59. }
  60. }
  61. }