GameTimeCounterSA.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GameTimeCounterSA : MonoBehaviour
  6. {
  7. [SerializeField] Text playTimeText;
  8. [SerializeField] GameObject payMask;
  9. [SerializeField] Text payTimeCountDownText;
  10. bool _paused;
  11. long _pauseTimePoint;
  12. const long PayTimeCountDown = 10 * 1000;
  13. string coinContinueGameUnit;
  14. string remainingTimeUnit;
  15. string minutesUnit;
  16. string secondsUnit;
  17. void Start()
  18. {
  19. coinContinueGameUnit = TextAutoLanguage2.GetTextByKey("CoinContinueGame");
  20. remainingTimeUnit = TextAutoLanguage2.GetTextByKey("RemainingTime");
  21. minutesUnit = TextAutoLanguage2.GetTextByKey("Minutes");
  22. secondsUnit = TextAutoLanguage2.GetTextByKey("Seconds");
  23. StandaloneAPI.StartGameTimeCountDown();
  24. }
  25. void Update()
  26. {
  27. //投币测试
  28. if (Input.GetKeyDown(KeyCode.Alpha7) || Input.GetKeyDown(KeyCode.Alpha8))
  29. {
  30. //投币接口测试
  31. StandaloneAPI.InsertCoinForAddTime();
  32. }
  33. StandaloneAPI.DoGameTimeCountDown();
  34. //"剩余时间{0}"
  35. playTimeText.text = string.Format(remainingTimeUnit, GetTimeStr());
  36. if (StandaloneAPI.GetGameTimeCountDown() > 0)
  37. {
  38. if (_paused)
  39. {
  40. payMask.SetActive(false);
  41. StandaloneAPI.ResumeGame();
  42. _paused = false;
  43. }
  44. }
  45. else
  46. {
  47. if (!_paused)
  48. {
  49. payMask.SetActive(true);
  50. StandaloneAPI.PauseGame();
  51. _paused = true;
  52. _pauseTimePoint = JCUnityLib.TimeUtils.GetTimestamp();
  53. }
  54. long t = PayTimeCountDown - (JCUnityLib.TimeUtils.GetTimestamp() - _pauseTimePoint);
  55. if (t <= 0) t = 0;
  56. //"投币继续游戏\n{0}S"
  57. payTimeCountDownText.text = string.Format(coinContinueGameUnit, t / 1000);
  58. if (t == 0) StandaloneAPI.ForceBackHome();
  59. }
  60. //#if UNITY_EDITOR
  61. //#endif
  62. }
  63. string GetTimeStr()
  64. {
  65. long second = StandaloneAPI.GetGameTimeCountDown() / 1000;
  66. long minute = second / 60;
  67. second = second % 60;
  68. string str = second + secondsUnit;//"秒";
  69. if (minute > 0) str = minute + minutesUnit + str; //"分"
  70. return str;
  71. }
  72. }