当前位置:   article > 正文

Unity实现轮播图_unity图片轮播切换

unity图片轮播切换
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. public class Carlousel : MonoBehaviour
  7. {
  8. private List<Transform> objs=new(); //轮播图片列表
  9. private List<Vector3> pos = new(); //记录图片位置
  10. int count = 0;
  11. Transform curImg;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. for (int i = 0; i < transform.childCount; i++)
  16. {
  17. objs.Add(transform.GetChild(i));
  18. pos.Add(transform.GetChild(i).position);
  19. }
  20. }
  21. IEnumerator RefreshCarlousel()
  22. {
  23. while (true)
  24. {
  25. yield return new WaitForSeconds(2f);
  26. for (int i = 0; i < objs.Count; i++)
  27. {
  28. int cur = i;
  29. objs[i].DOScale(Vector3.one * 0.9f, 0.5f).OnComplete(delegate { DoMove(cur); });
  30. }
  31. count++;
  32. yield return new WaitForSeconds(3f);
  33. }
  34. }
  35. void DoMove(int cur)
  36. {
  37. if (cur - (count % objs.Count) < 0)
  38. {
  39. objs[cur].DOMove(pos[pos.Count - (count % objs.Count - cur)], 0.5f).OnComplete(() => {
  40. objs[cur].DOScale(Vector3.one, 0.5f);
  41. });
  42. }
  43. else
  44. {
  45. objs[cur].DOMove(pos[cur - (count % objs.Count)], 0.5f).OnComplete(()=> {
  46. objs[cur].DOScale(Vector3.one, 0.5f);
  47. });
  48. }
  49. }
  50. private void OnEnable()
  51. {
  52. OpenOrClose(true);
  53. }
  54. private void OnDisable()
  55. {
  56. OpenOrClose(false);
  57. }
  58. public void OpenOrClose(bool ison)
  59. {
  60. if (ison)
  61. {
  62. StartCoroutine(RefreshCarlousel());
  63. }
  64. else
  65. {
  66. StopAllCoroutines();
  67. }
  68. }
  69. }

图片按顺序排列-(第二个位置是图片显示的位置也是遮罩位置)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/606810
推荐阅读
相关标签
  

闽ICP备14008679号