StandaloneAPI.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 (_GameTimeCountDownMS <= 0)
  110. {
  111. _GameTimeCountDownMS = 0;
  112. //延迟一分钟关闭激光
  113. if (UserSettings.ins.lightState)
  114. SerialPortHelper.ins.GetPort()?.DelayCloseLight();
  115. }
  116. else if (_GameTimeCountDownMS > 0)
  117. _GameTimeCountDownMS -= dt;
  118. }
  119. private static bool _TimeCounterInited;
  120. public static void InitTimeCounter()
  121. {
  122. if (_TimeCounterInited) return;
  123. _TimeCounterInited = true;
  124. SceneManager.sceneLoaded += (scene, mode) => {
  125. string sceneName = scene.name;
  126. if (IsGameScene(sceneName))
  127. {
  128. //操作引导场景不用投币
  129. if (!GameMgr.bShowDistance && GameMgr.turnOffTimer) return;
  130. Object.Instantiate(Resources.Load<GameObject>("GameTimeCounterSA"));
  131. }
  132. };
  133. }
  134. private static bool IsGameScene(string sceneName)
  135. {
  136. if (sceneName.StartsWith("Game") || sceneName.StartsWith("DuckHunter") || sceneName.StartsWith("WildAttack") ||
  137. sceneName.StartsWith("FruitMaster") || sceneName.StartsWith("Hyperspace") ||
  138. sceneName.StartsWith("GameChallenge") || sceneName.StartsWith("GameDouble"))
  139. return true;
  140. return false;
  141. }
  142. /// <summary>
  143. /// 投币加时
  144. /// </summary>
  145. public static void InsertCoinForAddTime()
  146. {
  147. //如果符合设置数量。增加时间
  148. if (CoinCount >= UserSettings.ins.PerRoundCoin)
  149. {
  150. //打开激光
  151. var port = SerialPortHelper.ins.GetPort();
  152. port?.CancelDelayCloseLight();
  153. if (!UserSettings.ins.lightState)
  154. port?.RequestLightState(true, true);
  155. CoinCount -= UserSettings.ins.PerRoundCoin;
  156. AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
  157. Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);
  158. Debug.Log("UserSettings.ins.PerRoundSeconds:" + UserSettings.ins.PerRoundSeconds);
  159. }
  160. }
  161. public static void InsertCoint(byte num)
  162. {
  163. Debug.Log("确认投币数量:" + num);
  164. CoinCount += num;
  165. StandaloneAPI.InsertCoinForAddTime();
  166. //for (int i = 0; i < num; i++)
  167. //{
  168. // StandaloneAPI.InsertCoinForAddTime();
  169. //}
  170. }
  171. }