gpt4 book ai didi

c# - Unity 编辑器的线程安全文件系统观察器

转载 作者:行者123 更新时间:2023-11-30 21:45:29 25 4
gpt4 key购买 nike

我需要一个供 File System Watcher 在 Unity Editor 中使用的线程安全类,我已经知道协程不可能使用线程,但我不知道在编辑器中也不允许使用线程。

所以,这是我的错误:

get_isEditor can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. 0x0000000140E431ED (Unity) StackWalker::GetCurrentCallstack 0x0000000140E44EE1 (Unity) StackWalker::ShowCallstack 0x00000001405FC603 (Unity) GetStacktrace 0x00000001405F97FE (Unity) DebugStringToFile 0x00000001405F9C5C (Unity) DebugStringToFile 0x000000014035F7B3 (Unity) ThreadAndSerializationSafeCheckReportError 0x0000000140E7B988 (Unity) Application_Get_Custom_PropIsEditor 0x0000000015AC46AA (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Application:get_isEditor () 0x0000000015AC42FE (Mono JIT Code) [Helpers.cs:585] Lerp2API.DebugHandler.Debug:Log (object) 0x0000000015AC41C2 (Mono JIT Code) [Helpers.cs:578] Lerp2API.DebugHandler.Debug:Log (string) 0x0000000015AC40F7 (Mono JIT Code) [LerpedEditorCore.cs:101] Lerp2APIEditor.LerpedEditorCore:Recompile (object,System.IO.FileSystemEventArgs) 0x0000000015AC3F2D (Mono JIT Code) (wrapper runtime-invoke) :runtime_invoke_void__this___object_object (object,intptr,intptr,intptr) 0x00007FFB400A519B (mono) [mini.c:4937] mono_jit_runtime_invoke 0x00007FFB3FFF84FD (mono) [object.c:2623] mono_runtime_invoke 0x00007FFB3FFFE8F7 (mono) [object.c:3827] mono_runtime_invoke_array 0x00007FFB3FFFEBCC (mono) [object.c:5457] mono_message_invoke 0x00007FFB4001EB8B (mono) [threadpool.c:1019] mono_async_invoke 0x00007FFB4001F5E2 (mono) [threadpool.c:1455] async_invoke_thread 0x00007FFB4002329F (mono) [threads.c:685] start_wrapper 0x00007FFB400D78C9 (mono) [win32_threads.c:599] thread_start 0x00007FFB77FC8364 (KERNEL32) BaseThreadInitThunk

我复制了完整的堆栈跟踪,以了解可能出现问题的任何助手。因为,我搜索了一个解决方案,就像任何线程安全的 FWS,是的,有一个,但仅适用于 .NET 4,我需要一个适用于 .NET 2

这是我的代码:

using System.IO; //class, namespace, redundant info...

private static FileSystemWatcher m_Watcher;

[InitializeOnLoadMethod]
static void HookWatcher()
{
m_Watcher = new FileSystemWatcher("path", "*.cs");
m_Watcher.NotifyFilter = NotifyFilters.LastWrite;
m_Watcher.IncludeSubdirectories = true;
//m_Watcher.Created += new FileSystemEventHandler(); //Add to the solution before compile
//m_Watcher.Renamed += new FileSystemEventHandler(); //Rename to the solution before compile
//m_Watcher.Deleted += new FileSystemEventHandler(); //Remove to the solution before compile
m_Watcher.Changed += Recompile;
m_Watcher.EnableRaisingEvents = true;
}

private static void Recompile(object sender, FileSystemEventArgs e)
{
Debug.Log("Origin files has been changed!");
}

如您所见,没有什么特别的......

我看到的 FSW 是这样的:https://gist.githubusercontent.com/bradsjm/2c839912294d0e2c008a/raw/c4a5c3d920ab46fdaa53b0e111e0d1204b1fe903/FileSystemWatcher.cs

我这样做的目的很简单,我有一个与当前 Unity 项目分离的 DLL,想法很简单,我想在 DLL 项目发生任何更改时自动从 Unity 重新编译所有内容,但我不能'由于线程无法实现,所以我该怎么办?是否有任何替代方案可以监听与 Unity 兼容的文件?

谢谢。

最佳答案

根据我的经验,您可以使用线程,但您必须注意只能从主线程访问 Unity 类。我的建议是在看门狗发出警报时将控制权移交给主线程。

static bool _triggerRecompile = false;

[InitializeOnLoadMethod]
static void HookWatcher()
{
m_Watcher = new FileSystemWatcher("path", "*.cs");
// ....
m_Watcher.Changed += Recompile;
EditorApplication.update += OnEditorApplicationUpdate;
}

private static void Recompile(object sender, FileSystemEventArgs e)
{
bool _triggerRecompile = true;
// Never call any Unity classes as we are not in the main thread
}

static void OnEditorApplicationUpdate ()
{
// note that this is called very often (100/sec)
if (_triggerRecompile)
{
_triggerRecompile = false;
Debug.Log("Origin files has been changed!");
DoRecompile();
}
}

轮询当然有点讨厌和丑陋。一般来说,我更喜欢基于事件的方法。但在这种特殊情况下,我认为没有机会欺骗主线程规则。

关于c# - Unity 编辑器的线程安全文件系统观察器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40140454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com