using DG.Tweening; using System.Collections.Generic; using System.Threading.Tasks; using TMPro; using UnityEngine; using UnityEngine.UI; public class Carousel2 : UIController { [SerializeField] Button[] Btn; [SerializeField] Button PrototypeBtn; [SerializeField] Transform ParentGame; public List url; int MoveSum = 0; [SerializeField] CarouselSmal2 CarouselSmall; bool isok = true; public List tips = new List(); [SerializeField] TextMeshProUGUI text; [SerializeField] bool isStart = false; [SerializeField] GameObject g1; [SerializeField] Toggle tog; // Start is called before the first frame update void Start() { try { init(); } catch (System.Exception ex) { Debug.LogError(ex.Message); throw; } } void init() { tog.onValueChanged.AddListener((x) => isStart = x); isStart = PlayerPrefs.GetInt("isStart") > 0 ? true : false; if (url.Count != 0) { Btn = new Button[url.Count]; for (int i = 0; i < url.Count; i++) { Btn[i] = Instantiate(PrototypeBtn, ParentGame); Btn[i].name = i.ToString(); } } if (Btn.Length != 0) { foreach (var item in Btn) { item.gameObject.SetActive(true); //item.onClick.AddListener(() => Application.OpenURL(url[int.Parse(item.name)])); item.image.GetComponent().sizeDelta = new Vector2(1008, 450); UIManager.ins.LoadImageAsync(url[int.Parse(item.name)], item.image, false); } for (int i = 0; i < Btn.Length; i++) { if (i > 0) { Btn[i].transform.DOLocalMoveX(Btn[i].transform.GetComponent().sizeDelta.x * i, 0.01f); } } } CarouselSmall.init(Btn.Length); CarouselSmall.show(MoveSum); StartIE(); text.text = tips[MoveSum]; if (!isStart) { g1.gameObject.SetActive(true); } else { g1.gameObject.SetActive(false); } } async void StartIE() { while (isok) { await Task.Delay(5000); try { show(); } catch (System.Exception ex) { #region 说了是小错误了,你就是不信,因为脚本被销毁了,但是while还会再次运行一次,所以不必理会 Debug.Log("一个小错误,不必理会" + ex.Message); #endregion } } } new void show() { if (Btn.Length > 1) { if (MoveSum % Btn.Length != 0) { ParentGame?.DOLocalMoveX(-MoveSum * Btn[MoveSum].GetComponent().sizeDelta.x, 0.25f); text.text = tips[MoveSum]; CarouselSmall?.show(MoveSum); } else { MoveSum = 0; ParentGame?.DOLocalMoveX(-MoveSum * Btn[MoveSum].GetComponent().sizeDelta.x, 0.25f); text.text = tips[MoveSum]; CarouselSmall?.show(MoveSum); } MoveSum++; } } public void show(int sum) { if (Btn.Length > 1) { ParentGame?.DOLocalMoveX(-sum * Btn[sum].GetComponent().sizeDelta.x, 0.25f); } MoveSum = sum; text.text = tips[MoveSum]; CarouselSmall?.show(MoveSum); } private void OnDestroy() { isok = false; } }