GameCanvas.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GameCanvas : MonoBehaviour
  6. {
  7. public Text timeText;
  8. public static GameCanvas ins;
  9. void Start()
  10. {
  11. ins = this;
  12. }
  13. void Update()
  14. {
  15. RefreshRender();
  16. }
  17. void RefreshRender()
  18. {
  19. RenderTime(GameController.ins.time);
  20. RenderScore(0, GameController.ins.scores[0]);
  21. RenderScore(1, GameController.ins.scores[1]);
  22. }
  23. void RenderTime(float time)
  24. {
  25. timeText.text = GetTimeStr(time);
  26. }
  27. void RenderScore(int playerIndex, float score)
  28. {
  29. transform.Find("ScoreBG" + (playerIndex + 1)).GetComponentInChildren<Text>().text =
  30. "玩家得分" + (float)System.Math.Round(score, CommonConfig.ringsPrecision);
  31. }
  32. string GetTimeStr(float time)
  33. {
  34. int seconds = (int)Mathf.Ceil(time);
  35. string str = "";
  36. int m = seconds / 60;
  37. if (m < 10)
  38. {
  39. str += 0;
  40. }
  41. str += m;
  42. str += " : ";
  43. int s = seconds % 60;
  44. if (s < 10)
  45. {
  46. str += 0;
  47. }
  48. str += s;
  49. return str;
  50. }
  51. }