| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using ZXing;
- public class StandaloneAPI
- {
- public static string url;//支付二维码对应的URL
- //记录一个投币数量
- public static int CoinCount = 0;
- public static Texture2D GetQR()
- {
- if (UserSettings.ins.PerRoundCoin <= 0)
- {
- Debug.LogWarning("PerRoundCoin 小于等于 0,不生成二维码。");
- return null;
- }
- return ShowQRCode(url, 256, 256);
- }
- /// <summary>
- /// 根据二维码图片信息绘制制定字符串信息的二维码到指定区域
- /// </summary>
- /// <param name="str">字符串信息</param>
- /// <param name="width">二维码的宽度</param>
- /// <param name="height">二维码的高度</param>
- /// <returns></returns>
- public static Texture2D ShowQRCode(string str, int width, int height)
- {
- if (string.IsNullOrEmpty(str))
- return null;
- Texture2D texture = new Texture2D(width, height);
- Color32[] colors = GeneQRCode(str, width, height);
- texture.SetPixels32(colors);
- texture.Apply();
- return texture;
- }
- /// <summary>
- /// 将制定字符串信息转换成二维码图片信息
- /// </summary>
- /// <param name="formatStr">要产生二维码的字符串信息</param>
- /// <param name="width">二维码的宽度</param>
- /// <param name="height">二维码的高度</param>
- /// <returns></returns>
- static Color32[] GeneQRCode(string formatStr, int width, int height)
- {
- ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();//绘制二维码之前 进行设置
- options.CharacterSet = "UTF-8";//设置字符编码,确保字符串信息保持正确
- options.Width = width;//设置二维码宽
- options.Height = height;//设置二维码高
- options.Margin = 1;//设置二维码留白 (值越大,留白越大,二维码越小)
- BarcodeWriter a = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };//实例化字符串绘制二维码工具
- return a.Write(formatStr);
- }
- public static void ForceBackHome()
- {
- SceneManager.LoadScene("Home", LoadSceneMode.Single);
- if (PersistenHandler.ins)
- {
- var views = PersistenHandler.ins.menuBackCtr.views;
- while (views.Count > 0)
- {
- var view = views[views.Count - 1];
- views.RemoveAt(views.Count - 1);
- view.OnMenuBack();
- }
- }
- }
- private static Object _GameLocker = new();
- public static void PauseGame()
- {
- string sceneName = SceneManager.GetActiveScene().name;
- if (sceneName == "Game" && GameMgr.ins)
- {
- GameMgr.ins.addLockerForGamePause(_GameLocker);
- }
- else if (IsGameScene(sceneName))
- {
- Time.timeScale = 0;
- }
- }
- public static void ResumeGame()
- {
- string sceneName = SceneManager.GetActiveScene().name;
- if (sceneName == "Game" && GameMgr.ins)
- {
- GameMgr.ins.removeLockerForGamePause(_GameLocker);
- }
- else if (IsGameScene(sceneName))
- {
- Time.timeScale = 1;
- }
- }
- private static long _GameTimeCountDownMS = 0;
- private static void AddGameTimeCountDown(long second)
- {
- _GameTimeCountDownMS += second * 1000;
- }
- public static long GetGameTimeCountDown()
- {
- return _GameTimeCountDownMS;
- }
- private static long _LastTimeMS;
- public static void StartGameTimeCountDown()
- {
- _LastTimeMS = JCUnityLib.TimeUtils.GetTimestamp();
- //刷新一次
- InsertCoinForAddTime();
- }
- public static void DoGameTimeCountDown()
- {
- long t = JCUnityLib.TimeUtils.GetTimestamp();
- long dt = t - _LastTimeMS;
- _LastTimeMS = t;
- if (UserSettings.ins.PerRoundCoin == 0) return;
- if (_GameTimeCountDownMS <= 0)
- {
- _GameTimeCountDownMS = 0;
- //延迟一分钟关闭激光
- if (UserSettings.ins.lightState)
- SerialPortHelper.ins.GetPort()?.DelayCloseLight();
- }
- else if (_GameTimeCountDownMS > 0)
- _GameTimeCountDownMS -= dt;
- }
- private static bool _TimeCounterInited;
- public static void InitTimeCounter()
- {
- if (_TimeCounterInited) return;
- _TimeCounterInited = true;
- SceneManager.sceneLoaded += (scene, mode) =>
- {
- string sceneName = scene.name;
- if (IsGameScene(sceneName))
- {
- //操作引导场景不用投币
- if (!GameMgr.bShowDistance && GameMgr.turnOffTimer) return;
- if (UserSettings.ins.PerRoundCoin == 0) return;
- Object.Instantiate(Resources.Load<GameObject>("GameTimeCounterSA"));
- }
- };
- }
- private static bool IsGameScene(string sceneName)
- {
- if (sceneName.StartsWith("Game") || sceneName.StartsWith("DuckHunter") || sceneName.StartsWith("WildAttack") ||
- sceneName.StartsWith("FruitMaster") || sceneName.StartsWith("Hyperspace") ||
- sceneName.StartsWith("GameChallenge") || sceneName.StartsWith("GameDouble"))
- return true;
- return false;
- }
- /// <summary>
- /// 投币加时
- /// </summary>
- //public static void InsertCoinForAddTime1()
- //{
- // //如果符合设置数量。增加时间
- // if (CoinCount >= UserSettings.ins.PerRoundCoin)
- // {
- // //打开激光
- // var port = SerialPortHelper.ins.GetPort();
- // port?.CancelDelayCloseLight();
- // if (!UserSettings.ins.lightState)
- // port?.RequestLightState(true, true);
- // CoinCount -= UserSettings.ins.PerRoundCoin;
- // AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
- // Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);
- // Debug.Log("UserSettings.ins.PerRoundSeconds:" + UserSettings.ins.PerRoundSeconds);
- // }
- //}
- /// <summary>
- /// 投币循环加时
- /// </summary>
- public static void InsertCoinForAddTime()
- {
- int perRoundCoin = UserSettings.ins.PerRoundCoin;
- int perRoundSeconds = UserSettings.ins.PerRoundSeconds;
- if (perRoundCoin <= 0)
- {
- Debug.LogWarning("PerRoundCoin 设置小于0,不用投币!");
- return;
- }
- // 如果符合设置数量。增加时间
- if (CoinCount >= perRoundCoin)
- {
- // 打开激光
- var port = SerialPortHelper.ins.GetPort();
- port?.CancelDelayCloseLight();
- if (!UserSettings.ins.lightState)
- port?.RequestLightState(true, true);
- while (CoinCount >= perRoundCoin)
- {
- CoinCount -= perRoundCoin;
- AddGameTimeCountDown(perRoundSeconds);
- }
- Debug.Log("剩余CoinCount:" + CoinCount);
- Debug.Log("每轮消耗:" + perRoundCoin);
- Debug.Log("每轮增加时间:" + perRoundSeconds);
- }
- }
- public static void InsertCoint(byte num)
- {
- Debug.Log("确认投币数量:" + num);
- CoinCount += num;
- StandaloneAPI.InsertCoinForAddTime();
- //for (int i = 0; i < num; i++)
- //{
- // StandaloneAPI.InsertCoinForAddTime();
- //}
- }
- }
|