RankItemUI.cs 6.8 KB

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