From c0e51e51bcd393ccabfe50b6cc2df0a3895e496e Mon Sep 17 00:00:00 2001 From: Hgnim Date: Sun, 24 Nov 2024 17:52:56 +0800 Subject: [PATCH] v1.1.1 update --- Assets/Scripts/Game/NumberDataControl.cs | 90 +++++------- Assets/Scripts/KeyManager.cs | 60 ++++---- .../MainMenu/MainMenu_StartGameText.cs | 26 ++-- Assets/Scripts/ThreadDelegate.cs | 131 ------------------ update_log.md | 7 +- 5 files changed, 82 insertions(+), 232 deletions(-) delete mode 100644 Assets/Scripts/ThreadDelegate.cs diff --git a/Assets/Scripts/Game/NumberDataControl.cs b/Assets/Scripts/Game/NumberDataControl.cs index b5dafbf..7637b08 100644 --- a/Assets/Scripts/Game/NumberDataControl.cs +++ b/Assets/Scripts/Game/NumberDataControl.cs @@ -1,12 +1,13 @@ -using System; -using System.Collections; -using System.Collections.Generic; using System.Diagnostics; using System.Threading; +using System.Threading.Tasks; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; -using static PublicClass.DLL; -using Debug = UnityEngine.Debug; +using Cysharp.Threading.Tasks; +using System; +using System.Collections; +using System.Timers; public class NumberDataControl : MonoBehaviour { @@ -76,31 +77,44 @@ internal void MCB_Click(bool isHit, int id) if (!gameIsStart) { gameIsStart = true; - countdownThread = new(CountdownThread); + /*countdownThread = new(CountdownThread); countdownUIThread = new(CountdownUIThread); countdownThread.Start(); - countdownUIThread.Start(); - } + countdownUIThread.Start();*/ + //因为WebGL不支持多线程,所以改为协程运行 + StartCoroutine( CountdownThread()); + //CountdownUIThread(); + } if (isHit) GameScore+=id; else GameScore--; } - Thread countdownThread; - Thread countdownUIThread; - void CountdownThread() + IEnumerator CountdownThread() { - while (gameTime > 0) - { - Sleep(10); - gameTime--; - } + static double tenms(DateTime startTime, DateTime endTime) + { + TimeSpan secondSpan = new(endTime.Ticks - startTime.Ticks); + return secondSpan.TotalMilliseconds/10; + } + DateTime start= DateTime.Now; + int gameTimeStartValue = gameTime; + while (gameTime > 0) + { + //await TaskSleep(10); + //await UniTask.Delay(10); + yield return new WaitForSeconds(0.01f); + //gameTime--; + gameTime = gameTimeStartValue-(int)tenms(start,DateTime.Now); + GameTimeUI = -1; + } gameTime = 0; + GameTimeUI = 0; - ThreadDelegate.QueueOnMainThread((param) => - { - if (Application.isPlaying) + /*ThreadDelegate.QueueOnMainThread((param) => + {*/ + if (Application.isPlaying) { GameObject gom = GameObject.Find("GameOverMenu_Root").transform.Find("GameOverMenu").gameObject; gom.SetActive(true); @@ -114,45 +128,11 @@ void CountdownThread() gom.SetActive(false); }); } - }, null); + /*}, null);*/ } - void CountdownUIThread() - { - while(gameTime > 0) - { - bool wait = false; - ThreadDelegate.QueueOnMainThread((param) => - { - GameTimeUI = -1; - wait = true; - }, null); - Sleep(10); - while (!wait) ; - } - ThreadDelegate.QueueOnMainThread((param) => - { - GameTimeUI = 0; - }, null); - } private void Start() { NumberDataControl.MouseClickST_event += MCB_Click; } - /// - /// 更精准的Sleep函数 - /// - /// 毫秒 - static void Sleep(int ms) - { - var sw = Stopwatch.StartNew(); - var sleepMs = ms - 16; - if (sleepMs > 0) - { - Thread.Sleep(sleepMs); - } - while (sw.ElapsedMilliseconds < ms) - { - Thread.Sleep(0); - } - } + } diff --git a/Assets/Scripts/KeyManager.cs b/Assets/Scripts/KeyManager.cs index c88dda4..a46fb35 100644 --- a/Assets/Scripts/KeyManager.cs +++ b/Assets/Scripts/KeyManager.cs @@ -4,25 +4,27 @@ using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine.UIElements; +using System.Collections; public class KeyManager : MonoBehaviour { Thread keyListenThread; void Start() { - keyListenThread = new(KeyListenThread); - keyListenThread.Start(); - } + /*keyListenThread = new(KeyListenThread); + keyListenThread.Start();*/ + StartCoroutine(KeyListenThread()); + } public static class WaitLock { internal static bool exitGameWaitLock = false; } - void KeyListenThread() + IEnumerator KeyListenThread() { - bool CheckEscKey() + static bool CheckEscKey() { - bool tmp = false; + /*bool tmp = false; bool wait = false; ThreadDelegate.QueueOnMainThread((param) => { @@ -30,9 +32,10 @@ bool CheckEscKey() wait = true; }, null); while (!wait) ; - return tmp; - } - while (keyListenThread.IsAlive) + return tmp;*/ + return GameKey.EscClick(); + } + while (Application.isPlaying) { if (!WaitLock.exitGameWaitLock && CheckEscKey()) @@ -41,16 +44,16 @@ bool CheckEscKey() //ExitGameWait(); ExitGame(); } - Thread.Sleep(10); - } + yield return new WaitForSeconds(0.01f); + } } /// /// 长按esc退出游戏 /// void ExitGameWait() { - ThreadDelegate.QueueOnMainThread((param) => - { + /*ThreadDelegate.QueueOnMainThread((param) => + {*/ GameObject quitText = GameObject.Find("MainCamera").transform.Find("Quit_Text").gameObject; quitText.SetActive(true); Text quitText_text = quitText.transform.Find("Quit_Text_text").gameObject.GetComponent(); @@ -64,15 +67,15 @@ bool keyCheck() { bool wait = false; bool tmp = false; - ThreadDelegate.QueueOnMainThread((param) => - { + /*ThreadDelegate.QueueOnMainThread((param) => + {*/ quitText_text.text = mTxt.PadRight(mTxt.Length + i, '.'); if (!GameKey.EscClick()) tmp = false; else tmp = true; wait = true; - }, null); + /*}, null);*/ while (!wait) ; return tmp; } @@ -93,35 +96,34 @@ bool keyCheck() exit:;//停止退出程序 { bool wait = false; - ThreadDelegate.QueueOnMainThread((param) => - { + /*ThreadDelegate.QueueOnMainThread((param) => + {*/ quitText_text.text = ""; quitText.SetActive(false); wait = true; - }, null); + /*}, null);*/ while (!wait) ; } over:; }); runThread.Start(); - }, null); + /*}, null);*/ } void ExitGame() { - bool wait = false; - ThreadDelegate.QueueOnMainThread((param) => - { + /*bool wait = false; + ThreadDelegate.QueueOnMainThread((param) => + {*/ #if UNITY_EDITOR - UnityEditor.EditorApplication.isPlaying = false; + UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif - wait = true; - }, null); - while (!wait) ; - - WaitLock.exitGameWaitLock = false; + /*wait = true; + }, null); + while (!wait) ;*/ + WaitLock.exitGameWaitLock = false; } } public static class GameKey diff --git a/Assets/Scripts/MainMenu/MainMenu_StartGameText.cs b/Assets/Scripts/MainMenu/MainMenu_StartGameText.cs index 4fe37b9..26fdb36 100644 --- a/Assets/Scripts/MainMenu/MainMenu_StartGameText.cs +++ b/Assets/Scripts/MainMenu/MainMenu_StartGameText.cs @@ -9,30 +9,26 @@ public class MainMenu_StartGameText : MonoBehaviour { - public const string version = "1.1.0.20241122"; + public const string version = "1.1.1.20241124"; + private void Start() + { + GameObject.Find("TextUI/About").GetComponent().text = +@$"版本: {version} +Copyright (C) 2024 Hgnim, All rights reserved. +Github: https://github.com/Hgnim/CursorTraining"; + } - - private void Update() + private void Update() { if (Input.anyKeyDown) { - if (Input.GetKey(KeyCode.A)) - { - MessageBox(IntPtr.Zero, - "游戏名: 光标训练(CursorTraining)\r\n" + - "版本: V"+version+"\r\n" + - "Copyright (C) 2024 Hgnim, All rights reserved.\r\n" + - "Github: https://github.com/Hgnim/CursorTraining" - , "关于", 0); - goto exitVoid; - } - else if(Input.GetKey(KeyCode.Escape)) + if(Input.GetKey(KeyCode.Escape)) goto exitVoid; DontDestroyOnLoad(GameObject.Find("KeyManager")); - DontDestroyOnLoad(GameObject.Find("ThreadDelegate")); + //DontDestroyOnLoad(GameObject.Find("ThreadDelegate")); SceneManager.LoadScene("Game"); } exitVoid:; diff --git a/Assets/Scripts/ThreadDelegate.cs b/Assets/Scripts/ThreadDelegate.cs deleted file mode 100644 index 6709f6f..0000000 --- a/Assets/Scripts/ThreadDelegate.cs +++ /dev/null @@ -1,131 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; -using System; -using System.Threading; -using System.Linq; - -public class ThreadDelegate : MonoBehaviour -{ - //是否已经初始化 - static bool isInitialized; - - private static ThreadDelegate _ins; - public static ThreadDelegate ins { get { Initialize(); return _ins; } } - - void Awake() - { - _ins = this; - isInitialized = true; - } - - //初始化 - public static void Initialize() - { - if (!isInitialized) - { - if (!Application.isPlaying) - return; - - isInitialized = true; - var obj = new GameObject("ThreadDelegate"); - _ins = obj.AddComponent(); - - DontDestroyOnLoad(obj); - } - } - - //单个执行单元(无延迟) - struct NoDelayedQueueItem - { - public Action action; - public object param; - } - //全部执行列表(无延迟) - List listNoDelayActions = new List(); - - - //单个执行单元(有延迟) - struct DelayedQueueItem - { - public Action action; - public object param; - public float time; - } - //全部执行列表(有延迟) - List listDelayedActions = new List(); - - - //加入到主线程执行队列(无延迟) - public static void QueueOnMainThread(Action taction, object param) - { - QueueOnMainThread(taction, param, 0f); - } - - //加入到主线程执行队列(有延迟) - public static void QueueOnMainThread(Action action, object param, float time) - { - if (time != 0) - { - lock (ins.listDelayedActions) - { - ins.listDelayedActions.Add(new DelayedQueueItem { time = Time.time + time, action = action, param = param }); - } - } - else - { - lock (ins.listNoDelayActions) - { - ins.listNoDelayActions.Add(new NoDelayedQueueItem { action = action, param = param }); - } - } - } - - - //当前执行的无延时函数链 - List currentActions = new List(); - //当前执行的有延时函数链 - List currentDelayed = new List(); - - void Update() - { - if (listNoDelayActions.Count > 0) - { - lock (listNoDelayActions) - { - currentActions.Clear(); - currentActions.AddRange(listNoDelayActions); - listNoDelayActions.Clear(); - } - for (int i = 0; i < currentActions.Count; i++) - { - currentActions[i].action(currentActions[i].param); - } - } - - if (listDelayedActions.Count > 0) - { - lock (listDelayedActions) - { - currentDelayed.Clear(); - currentDelayed.AddRange(listDelayedActions.Where(d => Time.time >= d.time)); - for (int i = 0; i < currentDelayed.Count; i++) - { - listDelayedActions.Remove(currentDelayed[i]); - } - } - - for (int i = 0; i < currentDelayed.Count; i++) - { - currentDelayed[i].action(currentDelayed[i].param); - } - } - } - - void OnDisable() - { - if (_ins == this) - { - _ins = null; - } - } -} \ No newline at end of file diff --git a/update_log.md b/update_log.md index 4ba1c7a..a3fdd43 100644 --- a/update_log.md +++ b/update_log.md @@ -1,7 +1,10 @@ -- V1.1.1.20241124_pre2-web鏇存柊鍐呭: +- V1.1.1.20241124鏇存柊鍐呭: + - 浼樺寲浜嗛儴鍒嗕唬鐮佸拰淇浜嗛儴鍒嗗皬闂 + +- V1.1.1.20241124_pre2鏇存柊鍐呭: - 灏嗙嚎绋嬫浛鎹㈡垚鍗忕▼锛岃В鍐砏eb鏃犳硶浣跨敤澶氱嚎绋嬪拰寮傛鐨勯棶棰 -- V1.1.1.20241124_pre1-web鏇存柊鍐呭: +- V1.1.1.20241124_pre1鏇存柊鍐呭: - 淇浜哤eb鐗堟湰瀛椾綋涓嶅吋瀹圭殑闂 - 灏濊瘯閫傞厤Web鐗堟湰