赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using DG.Tweening;
-
- public class Carlousel : MonoBehaviour
- {
- private List<Transform> objs=new(); //轮播图片列表
- private List<Vector3> pos = new(); //记录图片位置
- int count = 0;
- Transform curImg;
- // Start is called before the first frame update
- void Start()
- {
- for (int i = 0; i < transform.childCount; i++)
- {
- objs.Add(transform.GetChild(i));
- pos.Add(transform.GetChild(i).position);
- }
- }
- IEnumerator RefreshCarlousel()
- {
- while (true)
- {
- yield return new WaitForSeconds(2f);
- for (int i = 0; i < objs.Count; i++)
- {
- int cur = i;
- objs[i].DOScale(Vector3.one * 0.9f, 0.5f).OnComplete(delegate { DoMove(cur); });
- }
- count++;
- yield return new WaitForSeconds(3f);
- }
- }
- void DoMove(int cur)
- {
- if (cur - (count % objs.Count) < 0)
- {
- objs[cur].DOMove(pos[pos.Count - (count % objs.Count - cur)], 0.5f).OnComplete(() => {
- objs[cur].DOScale(Vector3.one, 0.5f);
- });
- }
- else
- {
- objs[cur].DOMove(pos[cur - (count % objs.Count)], 0.5f).OnComplete(()=> {
- objs[cur].DOScale(Vector3.one, 0.5f);
- });
- }
- }
- private void OnEnable()
- {
- OpenOrClose(true);
- }
- private void OnDisable()
- {
- OpenOrClose(false);
- }
- public void OpenOrClose(bool ison)
- {
- if (ison)
- {
- StartCoroutine(RefreshCarlousel());
- }
- else
- {
- StopAllCoroutines();
- }
- }
- }
图片按顺序排列-(第二个位置是图片显示的位置也是遮罩位置)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。