MainThread.Player.cs•2.22 kB
/*
┌──────────────────────────────────────────────────────────────────┐
│ 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
using System;
using System.Threading.Tasks;
using com.IvanMurzak.ReflectorNet.Utils;
namespace com.IvanMurzak.Unity.MCP.Runtime.Utils
{
public static class MainThreadInstaller
{
public static void Init()
{
MainThread.Instance = new UnityMainThread();
}
}
public class UnityMainThread : MainThread
{
public override bool IsMainThread => MainThreadDispatcher.IsMainThread;
public override Task<T> RunAsync<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
MainThreadDispatcher.Enqueue(() =>
{
try
{
T result = func();
tcs.SetResult(result);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
}
public override Task RunAsync(Action action)
{
var tcs = new TaskCompletionSource<bool>();
MainThreadDispatcher.Enqueue(() =>
{
try
{
action();
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
}
}
}
#endif