当前位置:   article > 正文

unity泛型委托制作排行榜_unity杀人王

unity杀人王

泛型能解决一些重复性的问题,比如分数从大到小排,死亡数从大到小排等等

这里先封装玩家数据

如名字,击杀数,死亡数等

  1. using UnityEngine;
  2. [System.Serializable]
  3. public class PlayerStatus
  4. {
  5. public string playerName;
  6. public int killNmber, deatNmber, flagNmber;
  7. public Sprite profileSprite, playerSprite;
  8. [HideInInspector] public string title;
  9. [HideInInspector] public string dateTime;
  10. public static bool CompareKill(PlayerStatus playerStatusA, PlayerStatus playerStatusB) => playerStatusA.killNmber>playerStatusB.killNmber;
  11. }

再创建一个游戏数据处理类,单列模式,来计算玩家的数据的排序

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. /// <summary>
  6. /// 委托条件 参数要一致至 返回值也有一致
  7. /// Action 可以带参数 没有返回值
  8. /// Func 可以带参数 有返回值
  9. /// </summary>
  10. public class GameDataManager : MonoBehaviour
  11. {
  12. public static GameDataManager Instance;
  13. public PlayerStatus[] playstatus;
  14. [HideInInspector] public string topSkllName, topDeathName, topFlagName;//[HideInInspector] Inspector隐藏 不能在unity中看到和编辑
  15. private void Awake()
  16. {
  17. if(Instance!=null)
  18. {
  19. Destroy(Instance);
  20. }
  21. else
  22. {
  23. Instance= this;
  24. }
  25. DontDestroyOnLoad(Instance);
  26. }
  27. private void Start()
  28. {
  29. topSkllName=GetTopNum((player) => player.killNmber);
  30. topDeathName=GetTopNum((player) => player.deatNmber);
  31. topFlagName=GetTopNum((player) => player.flagNmber);
  32. UIManager.Instance.UpdatePlayerTitle(topSkllName, "杀人王", (player) => player.playerName);
  33. UIManager.Instance.UpdatePlayerTitle(topDeathName, "死亡者", Logger.Log);
  34. UIManager.Instance.UpdatePlayerTitle(topFlagName, "夺标晓", Logger.Log);
  35. Debug.Log("kill "+GetTopNum((player) => player.killNmber));//lambda 会做类型推断 等同与上方的 //public int GetPlayerKillNum(PlayerStatus player)
  36. Debug.Log("death"+GetTopNum((player) => player.deatNmber));
  37. Debug.Log("flag"+GetTopNum((player) => player.flagNmber));
  38. UIManager.Instance.UpdataLeftUI();
  39. UIManager.Instance.UpdataRightUI();
  40. UIManager.Instance.UpdateSoltAlpha();
  41. }
  42. //委托 找到最大值
  43. public string GetTopNum(Func<PlayerStatus,int> func)//PlayerStatus 是参数 int 返回值
  44. {
  45. string topName = "";
  46. int bestRecrod = 0;
  47. foreach(PlayerStatus player in playstatus)
  48. {
  49. int tempNum = func(player);
  50. if(tempNum>bestRecrod)
  51. {
  52. bestRecrod = tempNum;
  53. topName=player.playerName;
  54. }
  55. }
  56. return topName;
  57. }
  58. //委托 冒泡排序法 泛型
  59. public void BubbleSort<T>(T[] playerStatuses ,Func<T, T, bool> func)
  60. {
  61. bool isSwapped=false;
  62. do
  63. {
  64. isSwapped=false;
  65. for (int i = 0; i <playerStatuses.Length-1; i++)
  66. {
  67. if (func(playerStatuses[i], playerStatuses[i+1]))
  68. {
  69. T temp = playerStatuses[i];
  70. playerStatuses[i]= playerStatuses[i+1];
  71. playerStatuses[i+1]= temp;
  72. isSwapped=true;
  73. }
  74. }
  75. }while( isSwapped);
  76. UIManager.Instance.UpdataLeftUI();
  77. }
  78. }

在创建一个UI管理类负责UI的显示

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.UI;
  7. public class UIManager : MonoBehaviour
  8. {
  9. public static UIManager Instance;
  10. int currentIndex;
  11. [Header("left UI")]
  12. public GameObject[] solts;
  13. [Header("right UI")]
  14. public Text statauText;
  15. public Image playerImage;
  16. public Text playerName;
  17. public Text titleText;//ToDo 称号
  18. [Header("按钮")]
  19. public Button leftButton, rightButton, skllButton, deatButton, flagButton;
  20. bool isAscendSkll=true;//判断是否倒序 从底层 从小到大
  21. bool isAscendDeath=true;//判断是否倒序
  22. bool isAscendFlag=true;//判断是否倒序
  23. bool isPressButton = true;
  24. private void Awake()
  25. {
  26. if (Instance!=null)
  27. {
  28. Destroy(Instance);
  29. }
  30. else
  31. {
  32. Instance= this;
  33. }
  34. DontDestroyOnLoad(Instance);
  35. }
  36. private void Start()
  37. {
  38. leftButton.onClick.AddListener(BackButton);
  39. rightButton.onClick.AddListener(NextButton);
  40. skllButton.onClick.AddListener(KillSortButton);
  41. deatButton.onClick.AddListener(DeathSortButton);
  42. flagButton.onClick.AddListener(FlagSortButton);
  43. // skllButton.onClick.AddListener(() => SortMetond(GameDataManager.Instance.playstatus, (playerStatusA, playerStatusB) => playerStatusA.killNmber>playerStatusB.killNmber));
  44. }
  45. private void Update()
  46. {
  47. Debug.Log(isPressButton);
  48. }
  49. //更新左边UI
  50. public void UpdataLeftUI()
  51. {
  52. for (int i = 0; i < solts.Length; i++)
  53. {
  54. solts[i].transform.GetChild(0).GetComponent<Image>().sprite=GameDataManager.Instance.playstatus[i].profileSprite;
  55. solts[i].GetComponentInChildren<Text>().text=string.Format("{0}/{1}/{2}", GameDataManager.Instance.playstatus[i].killNmber,
  56. GameDataManager.Instance.playstatus[i].deatNmber,
  57. GameDataManager.Instance.playstatus[i].flagNmber);
  58. solts[i].transform.GetChild(2).GetComponent<Text>().text=GameDataManager.Instance.playstatus[i].dateTime;
  59. Debug.Log(i+"号"+GameDataManager.Instance.playstatus[i].dateTime);
  60. }
  61. }
  62. //图层透明度控制
  63. public void UpdateSoltAlpha()
  64. {
  65. for (int i = 0; i < solts.Length; i++)
  66. {
  67. if(i==currentIndex)
  68. {
  69. solts[i].GetComponent<CanvasGroup>().alpha=1f;
  70. solts[i].transform.GetChild(0).GetComponentInChildren<ParticleSystem>().Play();//获得子物体的粒子
  71. }
  72. else
  73. {
  74. // solts[i].GetComponent<CanvasGroup>().alpha=0.25f;
  75. UITweener.instance.AlphaChange(solts[i].GetComponent<CanvasGroup>(), 0.25f);
  76. solts[i].transform.GetChild(0).GetComponentInChildren<ParticleSystem>().Stop();//获得子物体的粒子
  77. }
  78. }
  79. }
  80. //更新右边UI
  81. public void UpdataRightUI()
  82. {
  83. PlayerStatus playerStatus = GameDataManager.Instance.playstatus[currentIndex];
  84. playerImage.sprite=playerStatus.playerSprite;
  85. statauText.text=string.Format("{0}/{1}/{2}", playerStatus.killNmber, playerStatus.deatNmber, playerStatus.flagNmber);
  86. playerName.text=playerStatus.playerName;
  87. titleText.text=playerStatus.title;
  88. }
  89. //更新玩家称号
  90. public void UpdatePlayerTitle(string topName,string title,Func<PlayerStatus,string> func)
  91. {
  92. for (int i = 0; i < GameDataManager.Instance.playstatus.Length; i++)//遍历数据
  93. {
  94. if (GameDataManager.Instance.playstatus[i].playerName==topName)
  95. {
  96. GameDataManager.Instance.playstatus[i].title=title;
  97. GameDataManager.Instance.playstatus[i].dateTime=func(GameDataManager.Instance.playstatus[i]);
  98. }
  99. }
  100. }
  101. //总排列方法 <T> 泛型标志
  102. public void SortMetond<T>(T[] playerStatuses, Func<T, T, bool> func,bool isAscend)
  103. {
  104. if (isAscend)//是否升序
  105. {
  106. GameDataManager.Instance.BubbleSort(playerStatuses, func);
  107. }
  108. else
  109. {
  110. ReverseArray();
  111. }
  112. UpdataLeftUI();
  113. UpdataRightUI();
  114. UpdateSoltAlpha();
  115. }
  116. //右按钮
  117. public void NextButton()
  118. {
  119. if(isPressButton)
  120. {
  121. currentIndex++;
  122. if (currentIndex >= solts.Length)
  123. {
  124. currentIndex=0;
  125. }
  126. UpdataRightUI();
  127. UpdateSoltAlpha();
  128. UIAimitor();
  129. }
  130. StopCoroutine(nameof(PressButtonCorotinue));
  131. StartCoroutine(nameof(PressButtonCorotinue));
  132. }
  133. //左按钮
  134. public void BackButton()
  135. {
  136. if (isPressButton)
  137. {
  138. currentIndex--;
  139. if (currentIndex<0)
  140. {
  141. currentIndex=solts.Length-1;
  142. }
  143. UpdataRightUI();
  144. UpdateSoltAlpha();
  145. UIAimitor();
  146. }
  147. StopCoroutine(nameof(PressButtonCorotinue));
  148. StartCoroutine(nameof(PressButtonCorotinue));
  149. }
  150. //击杀数排列
  151. public void KillSortButton()
  152. {
  153. SortMetond(GameDataManager.Instance.playstatus,(playerStatusA, playerStatusB) => playerStatusA.killNmber>playerStatusB.killNmber,isAscendSkll);
  154. isAscendSkll=!isAscendSkll;
  155. isAscendDeath=true;
  156. isAscendFlag=true;
  157. }
  158. //死亡数排列
  159. public void DeathSortButton()
  160. {
  161. SortMetond(GameDataManager.Instance.playstatus, (playerStatusA, playerStatusB) => playerStatusA.deatNmber>playerStatusB.deatNmber,isAscendDeath);
  162. isAscendDeath=!isAscendDeath;
  163. isAscendFlag=true;
  164. isAscendSkll=true;
  165. }
  166. //旗帜排列
  167. public void FlagSortButton()
  168. {
  169. SortMetond(GameDataManager.Instance.playstatus, (playerStatusA, playerStatusB) => playerStatusA.flagNmber>playerStatusB.flagNmber,isAscendFlag);
  170. isAscendFlag=!isAscendFlag;
  171. isAscendDeath=true;
  172. isAscendSkll=true;
  173. }
  174. //将数组倒序
  175. public void ReverseArray()=>Array.Reverse(GameDataManager.Instance.playstatus);
  176. //各种ui动画效果
  177. public void UIAimitor()
  178. {
  179. UITweener.instance.DoTweenPlayerImage();
  180. }
  181. IEnumerator PressButtonCorotinue()//IEnumerator 开始协程
  182. {
  183. isPressButton=false;
  184. yield return new WaitForSeconds(0.5f);
  185. isPressButton=true;
  186. }
  187. }

显示获得称呼的时间

  1. using UnityEngine;
  2. using System;
  3. public class Logger
  4. {
  5. public static string Log(PlayerStatus player)
  6. {
  7. DateTime currentTime= DateTime.UtcNow;//通用时间 UtcNow不带时区
  8. Debug.Log(string.Format("{0}{1}{2}", player.playerName, player.title, player.dateTime));
  9. return currentTime.ToString();
  10. }
  11. }

后面可以用Dotween来丰富UI的交互

  1. using UnityEngine;
  2. using DG.Tweening;
  3. using UnityEngine.EventSystems;
  4. public class UITweener : MonoBehaviour
  5. {
  6. public static UITweener instance;
  7. public RectTransform statusRT, playerImageRT, playerTextRT, titleTextRT;
  8. public RectTransform nextButton, backButton;
  9. private void Awake()
  10. {
  11. if (instance == null)
  12. {
  13. instance = this;
  14. }
  15. else
  16. {
  17. if (instance != null)
  18. {
  19. Destroy(gameObject);
  20. }
  21. }
  22. DontDestroyOnLoad(gameObject);
  23. }
  24. //鼠标进入【上下页按钮】触发,按钮变大
  25. public void ScaleIn(RectTransform _trans)
  26. {
  27. _trans.DOScale(new Vector3(2f, 2f, 2f), 0.2f).SetEase(Ease.InSine);
  28. }
  29. //鼠标离开【上下页按钮】触发,按钮恢复
  30. public void ScaleOut(RectTransform _trans)
  31. {
  32. _trans.DOScale(new Vector3(1f, 1f, 1f), 0.2f).SetEase(Ease.OutSine);
  33. }
  34. //鼠标点击【上下页按钮】触发,右侧人物“扭动”动画
  35. public void ShakeScale(RectTransform _trans)
  36. {
  37. _trans.DOShakeScale(0.5f, 0.25f, 10, 15);
  38. }
  39. //鼠标点击【上下页按钮】触发,右侧人物信息“颤抖”动画
  40. public void ShakeRotation(RectTransform _trans)
  41. {
  42. _trans.DOShakeRotation(0.5f, 25, 5, 25);
  43. }
  44. //改变透明度
  45. public void AlphaChange(CanvasGroup _canvasGroup, float _amount)
  46. {
  47. _canvasGroup.DOFade(_amount, 0.5f);//DOFade 透明度 所需时间
  48. }
  49. public void DoTweenPlayerImage()
  50. {
  51. ShakeScale(playerImageRT);
  52. ShakeRotation(statusRT);
  53. ShakeScale(playerTextRT);
  54. ShakeRotation(titleTextRT);
  55. }
  56. }

详细的可以学习

【UI游戏界面】上下页切换,委托类型作为方法的参数和LAMBDA表达式的实际使用(附:计算所有数据最高的方法,GetChild方法优缺点等)_哔哩哔哩_bilibili

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/369447
推荐阅读
相关标签
  

闽ICP备14008679号