DiamondChooseComp.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Api;
  5. using UI.Common;
  6. using Unity.VisualScripting;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using UnityEngine.UIElements;
  10. public class DiamondChooseComp : MonoBehaviour
  11. {
  12. public ScrollRect scrollRect;
  13. public Text Item;
  14. public float smoothTime = 1.0f;
  15. public static int[] DiamondArray = new[] { 200, 500, 1000, 2000 };
  16. private int curindex = 0;
  17. private bool _scrolling = false;
  18. public int getSelectDiamond()
  19. {
  20. return DiamondArray[curindex];
  21. }
  22. public Action<int> OnDiamondChange;
  23. public void gameStart()
  24. {
  25. _scrolling = true;
  26. }
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. //从第一个开始复制
  31. for (var i = 0; i < DiamondArray.Length; i++)
  32. {
  33. if (i == 0)
  34. {
  35. Item.GetComponent<Text>().text = "" + DiamondArray[i];
  36. }
  37. else
  38. {
  39. Instantiate(Item, scrollRect.content, false).GetComponent<Text>().text = "" + DiamondArray[i];
  40. }
  41. }
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. }
  47. public void A__ClickChangedDiamond()
  48. {
  49. if (!_scrolling)
  50. {
  51. _scrolling = true;
  52. if (curindex + 1 < DiamondArray.Length)
  53. {
  54. int nextDiamond = DiamondArray[curindex + 1];
  55. if (AccountManager.Instance.GetDiamond() >= nextDiamond)
  56. {
  57. curindex += 1;
  58. DiaplayNextDiamond(curindex);
  59. OnDiamondChange?.Invoke(CurSelectDiamond());
  60. }
  61. else
  62. {
  63. TipsComp.ShowTips("Max Diamond");
  64. }
  65. }
  66. }
  67. Debug.Log("当前选择钻石是" + CurSelectDiamond());
  68. }
  69. public int CurSelectDiamond()
  70. {
  71. return DiamondArray[curindex];
  72. }
  73. public void DiaplayNextDiamond(int index)
  74. {
  75. Transform child = scrollRect.content.GetChild(index);
  76. RectTransform childRectTransform = child.GetComponent<RectTransform>();
  77. RectTransform scrollViewRectTransform = scrollRect.GetComponent<RectTransform>();
  78. // 计算子物体相对于ScrollView的偏移量
  79. Vector2 offset = scrollViewRectTransform.InverseTransformPoint(childRectTransform.position) -
  80. scrollViewRectTransform.InverseTransformPoint(scrollViewRectTransform.position);
  81. // 滚动ScrollView
  82. StartCoroutine(SmoothScrollCoroutine(offset));
  83. // scrollRect.ScrollTo()
  84. }
  85. IEnumerator SmoothScrollCoroutine(Vector2 offset)
  86. {
  87. float elapsedTime = 0;
  88. Vector2 startingPos = scrollRect.content.anchoredPosition;
  89. Vector2 targetPos = startingPos - offset;
  90. while (elapsedTime < smoothTime)
  91. {
  92. elapsedTime += Time.deltaTime;
  93. scrollRect.content.anchoredPosition =
  94. Vector2.Lerp(startingPos, targetPos, Mathf.SmoothStep(0, 1, elapsedTime / smoothTime));
  95. _scrolling = false;
  96. yield return null;
  97. }
  98. scrollRect.content.anchoredPosition = targetPos;
  99. }
  100. }