123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Api;
- using UI.Common;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.UIElements;
- public class DiamondChooseComp : MonoBehaviour
- {
- public ScrollRect scrollRect;
- public Text Item;
- public float smoothTime = 1.0f;
- public static int[] DiamondArray = new[] { 200, 500, 1000, 2000 };
- private int curindex = 0;
- private bool _scrolling = false;
- public int getSelectDiamond()
- {
- return DiamondArray[curindex];
- }
- public Action<int> OnDiamondChange;
- public void gameStart()
- {
- _scrolling = true;
- }
- // Start is called before the first frame update
- void Start()
- {
- //从第一个开始复制
- for (var i = 0; i < DiamondArray.Length; i++)
- {
- if (i == 0)
- {
- Item.GetComponent<Text>().text = "" + DiamondArray[i];
- }
- else
- {
- Instantiate(Item, scrollRect.content, false).GetComponent<Text>().text = "" + DiamondArray[i];
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- public void A__ClickChangedDiamond()
- {
- if (!_scrolling)
- {
- _scrolling = true;
- if (curindex + 1 < DiamondArray.Length)
- {
- int nextDiamond = DiamondArray[curindex + 1];
- if (AccountManager.Instance.GetDiamond() >= nextDiamond)
- {
- curindex += 1;
- DiaplayNextDiamond(curindex);
- OnDiamondChange?.Invoke(CurSelectDiamond());
- }
- else
- {
- TipsComp.ShowTips("Max Diamond");
- }
- }
- }
- Debug.Log("当前选择钻石是" + CurSelectDiamond());
- }
- public int CurSelectDiamond()
- {
- return DiamondArray[curindex];
- }
- public void DiaplayNextDiamond(int index)
- {
- Transform child = scrollRect.content.GetChild(index);
- RectTransform childRectTransform = child.GetComponent<RectTransform>();
- RectTransform scrollViewRectTransform = scrollRect.GetComponent<RectTransform>();
- // 计算子物体相对于ScrollView的偏移量
- Vector2 offset = scrollViewRectTransform.InverseTransformPoint(childRectTransform.position) -
- scrollViewRectTransform.InverseTransformPoint(scrollViewRectTransform.position);
- // 滚动ScrollView
- StartCoroutine(SmoothScrollCoroutine(offset));
- // scrollRect.ScrollTo()
- }
- IEnumerator SmoothScrollCoroutine(Vector2 offset)
- {
- float elapsedTime = 0;
- Vector2 startingPos = scrollRect.content.anchoredPosition;
- Vector2 targetPos = startingPos - offset;
- while (elapsedTime < smoothTime)
- {
- elapsedTime += Time.deltaTime;
- scrollRect.content.anchoredPosition =
- Vector2.Lerp(startingPos, targetPos, Mathf.SmoothStep(0, 1, elapsedTime / smoothTime));
- _scrolling = false;
- yield return null;
- }
- scrollRect.content.anchoredPosition = targetPos;
- }
- }
|