| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- namespace LocalRank
- {
- [System.Serializable]
- public class RankSpriteInfo
- {
- public Sprite sprite;
- public Vector2 size; // 显示尺寸
- public string bowTextKey;// 翻译的key
- public string gunTextKey;
- }
- public class RankItemUI : MonoBehaviour
- {
- public Text rankIndexText;
- public Text infoText;
- public Image avatarImage;
- public Transform iconTransform;
- public Image iconImage;
- public Text iconText;
- public Outline outline;
- // 图标资源(可选,也可以从外部注入)
- public RankSpriteInfo firstPlaceSprite;
- public RankSpriteInfo secondPlaceSprite;
- public RankSpriteInfo thirdPlaceSprite;
- // 称谓
- public Text titleText;
- public Sprite selfSprite; // 自己的背景
- public Sprite otherSprite; // 其他人的背景
- public Sprite otherTopSprite; // 其他人的背景(前3名)
- private RankItemData rankItemData;
- private Coroutine fakeRankCoroutine;
- private int realRank = 0;
- public void SetData(RankItemData data)
- {
- // 设置数据
- if (data == null) return;
- rankItemData = data;
- realRank = data.Rank;
- rankIndexText.text = $"#{data.RankIndex}";
- infoText.text = $"{data.Score}"; //{data.UserName}
- // 设置名次文本或前三图标
- ApplyRankDisplay(data.Rank);
- // if (iconTransform != null && iconImage != null && iconText != null)
- // {
- // if (data.Rank == 1 || data.Rank == 2 || data.Rank == 3)
- // {
- // iconImage.enabled = true;
- // iconText.gameObject.SetActive(false);
- // RankSpriteInfo info = null;
- // switch (data.Rank)
- // {
- // case 1:
- // info = firstPlaceSprite;
- // break;
- // case 2:
- // info = secondPlaceSprite;
- // break;
- // case 3:
- // info = thirdPlaceSprite;
- // break;
- // }
- // if (info != null && info.sprite != null)
- // {
- // iconImage.sprite = info.sprite;
- // iconImage.rectTransform.sizeDelta = info.size;
- // }
- // }
- // else
- // {
- // iconImage.enabled = false;
- // iconText.gameObject.SetActive(true);
- // iconText.text = $"{data.Rank}";
- // }
- // }
- Sprite topSprite = data.Rank <= 3 ? otherTopSprite : otherSprite;
- GetComponent<Image>().sprite = data.IsSelf ? selfSprite : topSprite;
- // Outline
- // if (outline != null)
- // outline.enabled = data.IsSelf;
- }
- public void SetAvatar(Sprite avatarSprite)
- {
- if (avatarImage != null)
- avatarImage.sprite = avatarSprite;
- }
- public void SetOtherSprite()
- {
- GetComponent<Image>().sprite = otherSprite;
- }
- public void HideIconAndText()
- {
- if (iconImage != null) iconImage.enabled = false;
- if (iconText != null) iconText.gameObject.SetActive(false);
- }
- public void HideIcon()
- {
- if (iconImage != null) iconImage.enabled = false;
- if (titleText != null) titleText.gameObject.SetActive(false);
- }
- public void ShowIconAndText()
- {
- if (rankItemData.Rank == 1 || rankItemData.Rank == 2 || rankItemData.Rank == 3)
- {
- iconImage.enabled = true;
- iconText.gameObject.SetActive(false);
- }
- else
- {
- iconImage.enabled = false;
- iconText.gameObject.SetActive(true);
- }
- if (titleText != null) titleText.gameObject.SetActive(true);
- }
- public void StartRunUI()
- {
- StartFakeRankAnimation(1.5f, 0.1f);
- HideIcon();
- }
- public void StopRunUI()
- {
- RestoreRealRank();
- ShowIconAndText();
- }
- // 设置排名展示(图标或文本)
- private void ApplyRankDisplay(int rank)
- {
- if (iconTransform != null && iconImage != null && iconText != null)
- {
- if (rank <= 3 && rank >= 1 )
- {
- iconImage.enabled = true;
- iconText.gameObject.SetActive(false);
- RankSpriteInfo info = rank == 1 ? firstPlaceSprite : rank == 2 ? secondPlaceSprite : thirdPlaceSprite;
- if (info != null && info.sprite != null)
- {
- iconImage.sprite = info.sprite;
- iconImage.rectTransform.sizeDelta = info.size;
- //根据设备区分
- titleText.text = TextAutoLanguage2.GetTextByKey(GlobalData.MyDeviceMode == DeviceMode.Archery ? info.bowTextKey:info.gunTextKey);
- }
- }
- else
- {
- iconImage.enabled = false;
- iconText.gameObject.SetActive(true);
- iconText.text = $"{rank}";
- }
- }
- rankIndexText.text = $"#{rank}";
- }
- // 自动开始假排名跳动
- public void StartFakeRankAnimation(float duration = 1.5f, float interval = 0.1f)
- {
- if (fakeRankCoroutine != null)
- StopCoroutine(fakeRankCoroutine);
- fakeRankCoroutine = StartCoroutine(FakeRankRoutine(duration, interval));
- }
- // 恢复真实排名
- public void RestoreRealRank()
- {
- if (fakeRankCoroutine != null)
- {
- StopCoroutine(fakeRankCoroutine);
- fakeRankCoroutine = null;
- }
- ApplyRankDisplay(realRank);
- }
- // 协程:随机数字跳变动画
- private IEnumerator FakeRankRoutine(float duration, float interval)
- {
- float elapsed = 0f;
- while (elapsed < duration)
- {
- int fakeRank = Random.Range(100, 1001);
- ApplyRankDisplay(fakeRank);
- yield return new WaitForSeconds(interval);
- elapsed += interval;
- }
- // 恢复真实排名
- ApplyRankDisplay(realRank);
- fakeRankCoroutine = null;
- }
- }
- }
|