| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using UnityEngine; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Script.Utils | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     public class TimerUtils : MonoBehaviour | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |         private static TimerUtils _instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public static void Initialize() | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |             if (_instance != null) return; | 
					
						
							|  |  |  |             GameObject timerObject = new GameObject("TimerUtils"); | 
					
						
							|  |  |  |             timerObject.hideFlags = HideFlags.HideInHierarchy; | 
					
						
							|  |  |  |             DontDestroyOnLoad(timerObject); | 
					
						
							|  |  |  |             _instance = timerObject.AddComponent<TimerUtils>(); | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         public static void DelayExecute(float delay, System.Action action) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_instance == null) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Debug.LogWarning("TimerUtils not initialized. Call TimerUtils.Initialize() first."); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             _instance.StartCoroutine(DelayExecuteCoroutine(delay, action)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public static void StopAllDelayedActions() | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |             if (_instance != null) | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |                 _instance.StopAllCoroutines(); | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |         public static void Dispose() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_instance != null) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Destroy(_instance.gameObject); | 
					
						
							|  |  |  |                 _instance = null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |         private static IEnumerator DelayExecuteCoroutine(float delay, System.Action action) | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |         { | 
					
						
							|  |  |  |             yield return new WaitForSeconds(delay); | 
					
						
							|  |  |  |             action?.Invoke(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-31 02:12:10 +00:00
										 |  |  |          | 
					
						
							|  |  |  |         void OnDestroy() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // 清理静态引用,防止内存泄漏 | 
					
						
							|  |  |  |             if (_instance == this) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 _instance = null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-30 09:24:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |