RankItemUI.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace LocalRank
  5. {
  6. [System.Serializable]
  7. public class RankSpriteInfo
  8. {
  9. public Sprite sprite;
  10. public Vector2 size; // 显示尺寸
  11. }
  12. public class RankItemUI : MonoBehaviour
  13. {
  14. public Text rankIndexText;
  15. public Text infoText;
  16. public Image avatarImage;
  17. public Transform iconTransform;
  18. public Image iconImage;
  19. public Text iconText;
  20. public Outline outline;
  21. // 图标资源(可选,也可以从外部注入)
  22. public RankSpriteInfo firstPlaceSprite;
  23. public RankSpriteInfo secondPlaceSprite;
  24. public RankSpriteInfo thirdPlaceSprite;
  25. public Sprite selfSprite; // 自己的背景
  26. public Sprite otherSprite; // 其他人的背景
  27. public Sprite otherTopSprite; // 其他人的背景(前3名)
  28. private RankItemData rankItemData;
  29. private Coroutine fakeRankCoroutine;
  30. private int realRank = 0;
  31. public void SetData(RankItemData data)
  32. {
  33. // 设置数据
  34. if (data == null) return;
  35. rankItemData = data;
  36. realRank = data.Rank;
  37. rankIndexText.text = $"#{data.RankIndex}";
  38. infoText.text = $"{data.Score}"; //{data.UserName}
  39. // 设置名次文本或前三图标
  40. ApplyRankDisplay(data.Rank);
  41. // if (iconTransform != null && iconImage != null && iconText != null)
  42. // {
  43. // if (data.Rank == 1 || data.Rank == 2 || data.Rank == 3)
  44. // {
  45. // iconImage.enabled = true;
  46. // iconText.gameObject.SetActive(false);
  47. // RankSpriteInfo info = null;
  48. // switch (data.Rank)
  49. // {
  50. // case 1:
  51. // info = firstPlaceSprite;
  52. // break;
  53. // case 2:
  54. // info = secondPlaceSprite;
  55. // break;
  56. // case 3:
  57. // info = thirdPlaceSprite;
  58. // break;
  59. // }
  60. // if (info != null && info.sprite != null)
  61. // {
  62. // iconImage.sprite = info.sprite;
  63. // iconImage.rectTransform.sizeDelta = info.size;
  64. // }
  65. // }
  66. // else
  67. // {
  68. // iconImage.enabled = false;
  69. // iconText.gameObject.SetActive(true);
  70. // iconText.text = $"{data.Rank}";
  71. // }
  72. // }
  73. Sprite topSprite = data.Rank <= 3 ? otherTopSprite : otherSprite;
  74. GetComponent<Image>().sprite = data.IsSelf ? selfSprite : topSprite;
  75. // Outline
  76. // if (outline != null)
  77. // outline.enabled = data.IsSelf;
  78. }
  79. public void SetAvatar(Sprite avatarSprite)
  80. {
  81. if (avatarImage != null)
  82. avatarImage.sprite = avatarSprite;
  83. }
  84. public void SetOtherSprite()
  85. {
  86. GetComponent<Image>().sprite = otherSprite;
  87. }
  88. public void HideIconAndText()
  89. {
  90. if (iconImage != null) iconImage.enabled = false;
  91. if (iconText != null) iconText.gameObject.SetActive(false);
  92. }
  93. public void HideIcon()
  94. {
  95. if (iconImage != null) iconImage.enabled = false;
  96. }
  97. public void ShowIconAndText()
  98. {
  99. if (rankItemData.Rank == 1 || rankItemData.Rank == 2 || rankItemData.Rank == 3)
  100. {
  101. iconImage.enabled = true;
  102. iconText.gameObject.SetActive(false);
  103. }
  104. else
  105. {
  106. iconImage.enabled = false;
  107. iconText.gameObject.SetActive(true);
  108. }
  109. }
  110. public void StartRunUI()
  111. {
  112. StartFakeRankAnimation(1.5f, 0.1f);
  113. HideIcon();
  114. }
  115. public void StopRunUI()
  116. {
  117. RestoreRealRank();
  118. ShowIconAndText();
  119. }
  120. // 设置排名展示(图标或文本)
  121. private void ApplyRankDisplay(int rank)
  122. {
  123. if (iconTransform != null && iconImage != null && iconText != null)
  124. {
  125. if (rank <= 3 && rank >= 1 )
  126. {
  127. iconImage.enabled = true;
  128. iconText.gameObject.SetActive(false);
  129. RankSpriteInfo info = rank == 1 ? firstPlaceSprite : rank == 2 ? secondPlaceSprite : thirdPlaceSprite;
  130. if (info != null && info.sprite != null)
  131. {
  132. iconImage.sprite = info.sprite;
  133. iconImage.rectTransform.sizeDelta = info.size;
  134. }
  135. }
  136. else
  137. {
  138. iconImage.enabled = false;
  139. iconText.gameObject.SetActive(true);
  140. iconText.text = $"{rank}";
  141. }
  142. }
  143. rankIndexText.text = $"#{rank}";
  144. }
  145. // 自动开始假排名跳动
  146. public void StartFakeRankAnimation(float duration = 1.5f, float interval = 0.1f)
  147. {
  148. if (fakeRankCoroutine != null)
  149. StopCoroutine(fakeRankCoroutine);
  150. fakeRankCoroutine = StartCoroutine(FakeRankRoutine(duration, interval));
  151. }
  152. // 恢复真实排名
  153. public void RestoreRealRank()
  154. {
  155. if (fakeRankCoroutine != null)
  156. {
  157. StopCoroutine(fakeRankCoroutine);
  158. fakeRankCoroutine = null;
  159. }
  160. ApplyRankDisplay(realRank);
  161. }
  162. // 协程:随机数字跳变动画
  163. private IEnumerator FakeRankRoutine(float duration, float interval)
  164. {
  165. float elapsed = 0f;
  166. while (elapsed < duration)
  167. {
  168. int fakeRank = Random.Range(100, 1001);
  169. ApplyRankDisplay(fakeRank);
  170. yield return new WaitForSeconds(interval);
  171. elapsed += interval;
  172. }
  173. // 恢复真实排名
  174. ApplyRankDisplay(realRank);
  175. fakeRankCoroutine = null;
  176. }
  177. }
  178. }