/* ┌──────────────────────────────────────────────────────────────────┐ │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ │ Copyright (c) 2025 Ivan Murzak │ │ Licensed under the Apache License, Version 2.0. │ │ See the LICENSE file in the project root for more information. │ └──────────────────────────────────────────────────────────────────┘ */ #if UNITY_EDITOR #nullable enable using System; using System.Threading.Tasks; using com.IvanMurzak.ReflectorNet.Utils; using UnityEditor; namespace com.IvanMurzak.Unity.MCP.Runtime.Utils { public static class MainThreadInstaller { [InitializeOnLoadMethod] public static void Init() => MainThread.Instance = new UnityMainThread(); } public class UnityMainThread : MainThread { public override bool IsMainThread => MainThreadDispatcher.IsMainThread; public override Task RunAsync(Task task) => MainThreadDispatcher.IsMainThread ? task : Dispatch(() => { task.Wait(); return true; }); public override Task RunAsync(Task task) => MainThreadDispatcher.IsMainThread ? task : Dispatch(() => task.Result); public override Task RunAsync(Func func) => MainThreadDispatcher.IsMainThread ? Task.FromResult(func()) : Dispatch(func); public override Task RunAsync(Action action) { if (MainThreadDispatcher.IsMainThread) { action(); return Task.CompletedTask; } return Dispatch(() => { action(); return true; }); } static Task Dispatch(Func body) { var tcs = new TaskCompletionSource(); void Execute() { try { tcs.SetResult(body()); } catch (Exception ex) { tcs.SetException(ex); } finally { EditorApplication.update -= Execute; } } EditorApplication.update += Execute; return tcs.Task; } } } #endif