private async void ThreadWork(object state) { try { while (true) { await Task.Delay(maxPrintingPeriodMs); string[] logsCopy = new string[logs.Count]; lock (lockObj) { if (logs.Count > 0) { logs.CopyTo(logsCopy); logs.Clear(); } } Print(logsCopy); } } catch (Exception e) { UnityThread.Execute(() => { Debug.LogError("Было брошено исключение в логгере " + e.Message); }); } // ReSharper disable once FunctionNeverReturns }
public void OnDisable() { if (instance == this) { instance = null; } }
//Used to initialize UnityThread. Call once before any function here public static void InitUnityThread(bool visible = false) { if (instance != null) { return; } if (Application.isPlaying) { // add an invisible game object to the scene GameObject obj = new GameObject("MainThreadExecuter"); if (!visible) { obj.hideFlags = HideFlags.HideAndDontSave; } DontDestroyOnLoad(obj); instance = obj.AddComponent <UnityThread>(); } }