2026-04-16 14:57:19 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Kill.Utils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 主线程执行器 - 确保回调在主线程执行
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MainThread : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private static MainThread _instance;
|
|
|
|
|
private static readonly Queue<Action> _actions = new Queue<Action>();
|
|
|
|
|
private static readonly object _lock = new object();
|
|
|
|
|
|
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
|
|
|
private static void Initialize()
|
|
|
|
|
{
|
|
|
|
|
var go = new GameObject("MainThread");
|
|
|
|
|
_instance = go.AddComponent<MainThread>();
|
|
|
|
|
DontDestroyOnLoad(go);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
while (_actions.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
_actions.Dequeue()?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在主线程执行
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Enqueue(Action action)
|
|
|
|
|
{
|
|
|
|
|
if (action == null) return;
|
|
|
|
|
|
2026-04-20 08:31:41 +08:00
|
|
|
// 编辑器环境下,如果调度器未初始化,直接执行
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// 如果调度器已初始化,加入队列
|
|
|
|
|
if (_instance != null)
|
2026-04-16 14:57:19 +08:00
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
_actions.Enqueue(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-04-20 08:31:41 +08:00
|
|
|
// 真机环境下调度器应该已初始化,如果未初始化则直接执行
|
2026-04-16 14:57:19 +08:00
|
|
|
action?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 立即在主线程执行(如果已经在主线程)或排队
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Run(Action action)
|
|
|
|
|
{
|
|
|
|
|
if (action == null) return;
|
|
|
|
|
|
|
|
|
|
// 检查是否在主线程
|
|
|
|
|
if (Time.frameCount > 0)
|
|
|
|
|
{
|
|
|
|
|
// 已经在主线程,直接执行
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 不在主线程,排队
|
|
|
|
|
Enqueue(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|