StandaloneAPI.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. //记录一个投币数量
  10. public static int CoinCount = 0;
  11. public static Texture2D GetQR()
  12. {
  13. if (UserSettings.ins.PerRoundCoin <= 0)
  14. {
  15. Debug.LogWarning("PerRoundCoin 小于等于 0,不生成二维码。");
  16. return null;
  17. }
  18. return ShowQRCode(url, 256, 256);
  19. }
  20. /// <summary>
  21. /// 根据二维码图片信息绘制制定字符串信息的二维码到指定区域
  22. /// </summary>
  23. /// <param name="str">字符串信息</param>
  24. /// <param name="width">二维码的宽度</param>
  25. /// <param name="height">二维码的高度</param>
  26. /// <returns></returns>
  27. public static Texture2D ShowQRCode(string str, int width, int height)
  28. {
  29. if (string.IsNullOrEmpty(str))
  30. return null;
  31. Texture2D texture = new Texture2D(width, height);
  32. Color32[] colors = GeneQRCode(str, width, height);
  33. texture.SetPixels32(colors);
  34. texture.Apply();
  35. return texture;
  36. }
  37. /// <summary>
  38. /// 将制定字符串信息转换成二维码图片信息
  39. /// </summary>
  40. /// <param name="formatStr">要产生二维码的字符串信息</param>
  41. /// <param name="width">二维码的宽度</param>
  42. /// <param name="height">二维码的高度</param>
  43. /// <returns></returns>
  44. static Color32[] GeneQRCode(string formatStr, int width, int height)
  45. {
  46. ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();//绘制二维码之前 进行设置
  47. options.CharacterSet = "UTF-8";//设置字符编码,确保字符串信息保持正确
  48. options.Width = width;//设置二维码宽
  49. options.Height = height;//设置二维码高
  50. options.Margin = 1;//设置二维码留白 (值越大,留白越大,二维码越小)
  51. BarcodeWriter a = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };//实例化字符串绘制二维码工具
  52. return a.Write(formatStr);
  53. }
  54. public static void ForceBackHome()
  55. {
  56. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  57. if (PersistenHandler.ins)
  58. {
  59. var views = PersistenHandler.ins.menuBackCtr.views;
  60. while (views.Count > 0)
  61. {
  62. var view = views[views.Count - 1];
  63. views.RemoveAt(views.Count - 1);
  64. view.OnMenuBack();
  65. }
  66. }
  67. }
  68. private static Object _GameLocker = new();
  69. public static void PauseGame()
  70. {
  71. string sceneName = SceneManager.GetActiveScene().name;
  72. if (sceneName == "Game" && GameMgr.ins)
  73. {
  74. GameMgr.ins.addLockerForGamePause(_GameLocker);
  75. }
  76. else if (IsGameScene(sceneName))
  77. {
  78. Time.timeScale = 0;
  79. }
  80. }
  81. public static void ResumeGame()
  82. {
  83. string sceneName = SceneManager.GetActiveScene().name;
  84. if (sceneName == "Game" && GameMgr.ins)
  85. {
  86. GameMgr.ins.removeLockerForGamePause(_GameLocker);
  87. }
  88. else if (IsGameScene(sceneName))
  89. {
  90. Time.timeScale = 1;
  91. }
  92. }
  93. private static long _GameTimeCountDownMS = 0;
  94. private static void AddGameTimeCountDown(long second)
  95. {
  96. _GameTimeCountDownMS += second * 1000;
  97. }
  98. public static long GetGameTimeCountDown()
  99. {
  100. return _GameTimeCountDownMS;
  101. }
  102. private static long _LastTimeMS;
  103. public static void StartGameTimeCountDown()
  104. {
  105. _LastTimeMS = JCUnityLib.TimeUtils.GetTimestamp();
  106. //刷新一次
  107. InsertCoinForAddTime();
  108. }
  109. public static void DoGameTimeCountDown()
  110. {
  111. long t = JCUnityLib.TimeUtils.GetTimestamp();
  112. long dt = t - _LastTimeMS;
  113. _LastTimeMS = t;
  114. if (UserSettings.ins.PerRoundCoin == 0) return;
  115. if (_GameTimeCountDownMS <= 0)
  116. {
  117. _GameTimeCountDownMS = 0;
  118. //延迟一分钟关闭激光
  119. if (UserSettings.ins.lightState)
  120. SerialPortHelper.ins.GetPort()?.DelayCloseLight();
  121. }
  122. else if (_GameTimeCountDownMS > 0)
  123. _GameTimeCountDownMS -= dt;
  124. }
  125. private static bool _TimeCounterInited;
  126. public static void InitTimeCounter()
  127. {
  128. if (_TimeCounterInited) return;
  129. _TimeCounterInited = true;
  130. SceneManager.sceneLoaded += (scene, mode) =>
  131. {
  132. string sceneName = scene.name;
  133. if (IsGameScene(sceneName))
  134. {
  135. //操作引导场景不用投币
  136. if (!GameMgr.bShowDistance && GameMgr.turnOffTimer) return;
  137. if (UserSettings.ins.PerRoundCoin == 0) return;
  138. Object.Instantiate(Resources.Load<GameObject>("GameTimeCounterSA"));
  139. }
  140. };
  141. }
  142. private static bool IsGameScene(string sceneName)
  143. {
  144. if (sceneName.StartsWith("Game") || sceneName.StartsWith("DuckHunter") || sceneName.StartsWith("WildAttack") ||
  145. sceneName.StartsWith("FruitMaster") || sceneName.StartsWith("Hyperspace") ||
  146. sceneName.StartsWith("GameChallenge") || sceneName.StartsWith("GameDouble"))
  147. return true;
  148. return false;
  149. }
  150. /// <summary>
  151. /// 投币加时
  152. /// </summary>
  153. //public static void InsertCoinForAddTime1()
  154. //{
  155. // //如果符合设置数量。增加时间
  156. // if (CoinCount >= UserSettings.ins.PerRoundCoin)
  157. // {
  158. // //打开激光
  159. // var port = SerialPortHelper.ins.GetPort();
  160. // port?.CancelDelayCloseLight();
  161. // if (!UserSettings.ins.lightState)
  162. // port?.RequestLightState(true, true);
  163. // CoinCount -= UserSettings.ins.PerRoundCoin;
  164. // AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
  165. // Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);
  166. // Debug.Log("UserSettings.ins.PerRoundSeconds:" + UserSettings.ins.PerRoundSeconds);
  167. // }
  168. //}
  169. /// <summary>
  170. /// 投币循环加时
  171. /// </summary>
  172. public static void InsertCoinForAddTime()
  173. {
  174. int perRoundCoin = UserSettings.ins.PerRoundCoin;
  175. int perRoundSeconds = UserSettings.ins.PerRoundSeconds;
  176. if (perRoundCoin <= 0)
  177. {
  178. Debug.LogWarning("PerRoundCoin 设置小于0,不用投币!");
  179. return;
  180. }
  181. // 如果符合设置数量。增加时间
  182. if (CoinCount >= perRoundCoin)
  183. {
  184. // 打开激光
  185. var port = SerialPortHelper.ins.GetPort();
  186. port?.CancelDelayCloseLight();
  187. if (!UserSettings.ins.lightState)
  188. port?.RequestLightState(true, true);
  189. while (CoinCount >= perRoundCoin)
  190. {
  191. CoinCount -= perRoundCoin;
  192. AddGameTimeCountDown(perRoundSeconds);
  193. }
  194. Debug.Log("剩余CoinCount:" + CoinCount);
  195. Debug.Log("每轮消耗:" + perRoundCoin);
  196. Debug.Log("每轮增加时间:" + perRoundSeconds);
  197. }
  198. }
  199. public static void InsertCoint(byte num)
  200. {
  201. Debug.Log("确认投币数量:" + num);
  202. CoinCount += num;
  203. StandaloneAPI.InsertCoinForAddTime();
  204. //for (int i = 0; i < num; i++)
  205. //{
  206. // StandaloneAPI.InsertCoinForAddTime();
  207. //}
  208. }
  209. }