GameTimeCounterSA.cs 1.8 KB

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