GameTimeCounterSA.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. void Start()
  14. {
  15. StandaloneAPI.StartGameTimeCountDown();
  16. }
  17. void Update()
  18. {
  19. //投币测试
  20. if (Input.GetKeyDown(KeyCode.Alpha7) || Input.GetKeyDown(KeyCode.Alpha8))
  21. {
  22. //投币接口测试
  23. StandaloneAPI.InsertCoinForAddTime();
  24. }
  25. StandaloneAPI.DoGameTimeCountDown();
  26. playTimeText.text = string.Format("剩余时间{0}", GetTimeStr());
  27. if (StandaloneAPI.GetGameTimeCountDown() > 0)
  28. {
  29. if (_paused)
  30. {
  31. payMask.SetActive(false);
  32. StandaloneAPI.ResumeGame();
  33. _paused = false;
  34. }
  35. }
  36. else
  37. {
  38. if (!_paused)
  39. {
  40. payMask.SetActive(true);
  41. StandaloneAPI.PauseGame();
  42. _paused = true;
  43. _pauseTimePoint = JCUnityLib.TimeUtils.GetTimestamp();
  44. }
  45. long t = PayTimeCountDown - (JCUnityLib.TimeUtils.GetTimestamp() - _pauseTimePoint);
  46. if (t <= 0) t = 0;
  47. payTimeCountDownText.text = string.Format("投币继续游戏\n{0}S", t / 1000);
  48. if (t == 0) StandaloneAPI.ForceBackHome();
  49. }
  50. //#if UNITY_EDITOR
  51. //#endif
  52. }
  53. string GetTimeStr()
  54. {
  55. long second = StandaloneAPI.GetGameTimeCountDown() / 1000;
  56. long minute = second / 60;
  57. second = second % 60;
  58. string str = second + "秒";
  59. if (minute > 0) str = minute + "分" + str;
  60. return str;
  61. }
  62. }