StandaloneAPI.cs 5.0 KB

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