ScalePageScrollView.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. public class ScalePageScrollView : PageScrollView
  3. {
  4. #region 字段
  5. // 所有页的Object
  6. protected GameObject[] items;
  7. public float currentScale = 1f;
  8. public float otherScale = 0.6f;
  9. public int lastPage;
  10. public int nextPage;
  11. #endregion
  12. #region Unity回调
  13. protected override void Start()
  14. {
  15. base.Start();
  16. items = new GameObject[pageCount];
  17. // 初始化所有的GameObject
  18. for (int i = 0; i < pageCount; i++)
  19. {
  20. items[i] = transform.Find("Viewport/Content").GetChild(i).gameObject;
  21. }
  22. }
  23. protected override void Update()
  24. {
  25. base.Update();
  26. ListenerScale();
  27. }
  28. #endregion
  29. // 监听scale
  30. public void ListenerScale()
  31. {
  32. // 找到上一页 和 下一页
  33. for (int i = 0; i < pages.Length; i++)
  34. {
  35. if ( pages[i] <= rect.horizontalNormalizedPosition )
  36. {
  37. lastPage = i;
  38. }
  39. }
  40. for (int i = 0; i < pages.Length; i++)
  41. {
  42. if (pages[i] > rect.horizontalNormalizedPosition)
  43. {
  44. nextPage = i;
  45. break;
  46. }
  47. }
  48. if ( nextPage == lastPage )
  49. {
  50. return;
  51. }
  52. float percent = (rect.horizontalNormalizedPosition - pages[lastPage]) / ( pages[nextPage] - pages[lastPage] );
  53. items[lastPage].transform.localScale = Vector3.Lerp(Vector3.one * currentScale, Vector3.one * otherScale, percent);
  54. items[nextPage].transform.localScale = Vector3.Lerp(Vector3.one * currentScale, Vector3.one * otherScale, 1 - percent);
  55. for (int i = 0; i < items.Length; i++)
  56. {
  57. if (i != lastPage && i != nextPage)
  58. {
  59. items[i].transform.localScale = Vector3.one * otherScale;
  60. }
  61. }
  62. }
  63. }