StandaloneAPI.cs 6.2 KB

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