Hi, struggling a little with threads inside the unity editor.
It seems my threads don’t stay active after unity re-compiles.
Is there anyway I can keep a thread “working” during a recompile?
I’m using an EditorWindow, here’s a really simple script:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Threading;
public class TestWindow : EditorWindow {
private static Worker workerObject;
private static Thread workerThread;
void OnGUI() {
if (GUILayout.Button("Start")) {
workerObject = new Worker();
workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
}
if (GUILayout.Button("Stop")) {
workerObject.RequestStop();
}
}
public class Worker {
public void DoWork() {
while (!_shouldStop) {
Debug.Log("worker thread: working...");
Thread.Sleep(1000);
}
Debug.Log("worker thread: terminating gracefully.");
}
public void RequestStop() {
_shouldStop = true;
}
private volatile bool _shouldStop;
}
}
Even though it’s not a background thread, it gets stopped whenever unity compiles