StandaloneAPI.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using ZXing;
  6. public class StandaloneAPI
  7. {
  8. public static string url;//支付二维码对应的URL
  9. public static Texture2D GetQR()
  10. {
  11. return ShowQRCode(url, 256, 256);
  12. }
  13. /// <summary>
  14. /// 根据二维码图片信息绘制制定字符串信息的二维码到指定区域
  15. /// </summary>
  16. /// <param name="str">字符串信息</param>
  17. /// <param name="width">二维码的宽度</param>
  18. /// <param name="height">二维码的高度</param>
  19. /// <returns></returns>
  20. public static Texture2D ShowQRCode(string str, int width, int height)
  21. {
  22. if (string.IsNullOrEmpty(str))
  23. return null;
  24. Texture2D texture = new Texture2D(width, height);
  25. Color32[] colors = GeneQRCode(str, width, height);
  26. texture.SetPixels32(colors);
  27. texture.Apply();
  28. return texture;
  29. }
  30. /// <summary>
  31. /// 将制定字符串信息转换成二维码图片信息
  32. /// </summary>
  33. /// <param name="formatStr">要产生二维码的字符串信息</param>
  34. /// <param name="width">二维码的宽度</param>
  35. /// <param name="height">二维码的高度</param>
  36. /// <returns></returns>
  37. static Color32[] GeneQRCode(string formatStr, int width, int height)
  38. {
  39. ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();//绘制二维码之前 进行设置
  40. options.CharacterSet = "UTF-8";//设置字符编码,确保字符串信息保持正确
  41. options.Width = width;//设置二维码宽
  42. options.Height = height;//设置二维码高
  43. options.Margin = 1;//设置二维码留白 (值越大,留白越大,二维码越小)
  44. BarcodeWriter a = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };//实例化字符串绘制二维码工具
  45. return a.Write(formatStr);
  46. }
  47. public static void ForceBackHome()
  48. {
  49. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  50. if (PersistenHandler.ins)
  51. {
  52. var views = PersistenHandler.ins.menuBackCtr.views;
  53. while (views.Count > 0)
  54. {
  55. var view = views[views.Count - 1];
  56. views.RemoveAt(views.Count - 1);
  57. view.OnMenuBack();
  58. }
  59. }
  60. }
  61. private static Object _GameLocker = new();
  62. public static void PauseGame()
  63. {
  64. string sceneName = SceneManager.GetActiveScene().name;
  65. if (sceneName == "Game" && GameMgr.ins)
  66. {
  67. GameMgr.ins.addLockerForGamePause(_GameLocker);
  68. }
  69. else if (IsGameScene(sceneName))
  70. {
  71. Time.timeScale = 0;
  72. }
  73. }
  74. public static void ResumeGame()
  75. {
  76. string sceneName = SceneManager.GetActiveScene().name;
  77. if (sceneName == "Game" && GameMgr.ins)
  78. {
  79. GameMgr.ins.removeLockerForGamePause(_GameLocker);
  80. }
  81. else if (IsGameScene(sceneName))
  82. {
  83. Time.timeScale = 1;
  84. }
  85. }
  86. private static long _GameTimeCountDownMS = 0;
  87. private static void AddGameTimeCountDown(long second)
  88. {
  89. _GameTimeCountDownMS += second * 1000;
  90. }
  91. public static long GetGameTimeCountDown()
  92. {
  93. return _GameTimeCountDownMS;
  94. }
  95. private static long _LastTimeMS;
  96. public static void StartGameTimeCountDown()
  97. {
  98. _LastTimeMS = JCUnityLib.TimeUtils.GetTimestamp();
  99. }
  100. public static void DoGameTimeCountDown()
  101. {
  102. long t = JCUnityLib.TimeUtils.GetTimestamp();
  103. long dt = t - _LastTimeMS;
  104. _LastTimeMS = t;
  105. _GameTimeCountDownMS -= dt;
  106. if (_GameTimeCountDownMS < 0) _GameTimeCountDownMS = 0;
  107. }
  108. private static bool _TimeCounterInited;
  109. public static void InitTimeCounter()
  110. {
  111. if (_TimeCounterInited) return;
  112. _TimeCounterInited = true;
  113. SceneManager.sceneLoaded += (scene, mode) => {
  114. string sceneName = scene.name;
  115. if (IsGameScene(sceneName))
  116. {
  117. //操作引导场景不用投币
  118. if (!GameMgr.bShowDistance && GameMgr.turnOffTimer) return;
  119. Object.Instantiate(Resources.Load<GameObject>("GameTimeCounterSA"));
  120. }
  121. };
  122. }
  123. private static bool IsGameScene(string sceneName)
  124. {
  125. if (sceneName.StartsWith("Game") || sceneName.StartsWith("DuckHunter") || sceneName.StartsWith("WildAttack") ||
  126. sceneName.StartsWith("FruitMaster") || sceneName.StartsWith("Hyperspace") ||
  127. sceneName.StartsWith("GameChallenge") || sceneName.StartsWith("GameDouble"))
  128. return true;
  129. return false;
  130. }
  131. /// <summary>
  132. /// 投币加时
  133. /// </summary>
  134. public static void InsertCoinForAddTime()
  135. {
  136. AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
  137. Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);
  138. Debug.Log("UserSettings.ins.PerRoundSeconds:"+ UserSettings.ins.PerRoundSeconds);
  139. }
  140. public static void InsertCoint(byte num)
  141. {
  142. Debug.Log("确认投币数量:" + num);
  143. for (int i = 0; i < num; i++)
  144. {
  145. StandaloneAPI.InsertCoinForAddTime();
  146. }
  147. }
  148. }