GameTimeCounterSA.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. public class GameTimeCounterSA : MonoBehaviour
  7. {
  8. [SerializeField] Text playTimeText;
  9. [SerializeField] GameObject payMask;
  10. [SerializeField] Text payTimeCountDownText;
  11. bool _paused;
  12. long _pauseTimePoint;
  13. const long PayTimeCountDown = 60 * 1000;
  14. private GameObject serialPortObj;
  15. void Start()
  16. {
  17. //防止遮挡
  18. string sceneName = SceneManager.GetActiveScene().name;
  19. if (sceneName == "DuckHunter")
  20. {
  21. playTimeText.rectTransform.anchoredPosition = new Vector2(0, 140);
  22. }
  23. StandaloneAPI.StartGameTimeCountDown();
  24. serialPortObj = GameObject.Find("SerialPortObj");
  25. string qrUrl = serialPortObj.GetComponent<PayGameMode>().getQRCodeUrl();
  26. if (!string.IsNullOrEmpty(qrUrl)) {
  27. GetComponent<CreatQR>().onlyQRCode(qrUrl, 512, 512);
  28. }
  29. Debug.Log("GameTimeCounterSA sceneName:" + sceneName);
  30. }
  31. void Update()
  32. {
  33. StandaloneAPI.DoGameTimeCountDown();
  34. playTimeText.text = string.Format("剩余时间{0}", GetTimeStr());
  35. if (StandaloneAPI.GetGameTimeCountDown() > 0)
  36. {
  37. if (_paused)
  38. {
  39. payMask.SetActive(false);
  40. StandaloneAPI.ResumeGame();
  41. _paused = false;
  42. }
  43. }
  44. else
  45. {
  46. if (!_paused)
  47. {
  48. payMask.SetActive(true);
  49. StandaloneAPI.PauseGame();
  50. _paused = true;
  51. _pauseTimePoint = JCUnityLib.TimeUtils.GetTimestamp();
  52. }
  53. long t = PayTimeCountDown - (JCUnityLib.TimeUtils.GetTimestamp() - _pauseTimePoint);
  54. if (t <= 0) t = 0;
  55. payTimeCountDownText.text = string.Format("投币继续游戏\n{0}", t / 1000);
  56. if (t == 0)
  57. {
  58. StandaloneAPI.ForceBackHome();
  59. serialPortObj.GetComponent<PayGameMode>().setQRPlanObjActive(true);
  60. }
  61. }
  62. #if UNITY_EDITOR
  63. if (Input.GetKeyDown(KeyCode.Z))
  64. {
  65. //投币接口测试
  66. StandaloneAPI.InsertCoinForAddTime();
  67. }
  68. #endif
  69. }
  70. string GetTimeStr()
  71. {
  72. long second = StandaloneAPI.GetGameTimeCountDown() / 1000;
  73. long minute = second / 60;
  74. second = second % 60;
  75. string str = second + "秒";
  76. if (minute > 0) str = minute + "分" + str;
  77. return str;
  78. }
  79. }