1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class ThreadManager : MonoBehaviour
- {
- private static readonly List<Action> executeOnMainThread = new List<Action>();
- private static readonly List<Action> executeCopiedOnMainThread = new List<Action>();
- private static bool actionToExecuteOnMainThread = false;
-
- private void FixedUpdate()
- {
- UpdateMain();
- }
-
- /// <summary>Sets an action to be executed on the main thread.</summary>
- /// <param name="_action">The action to be executed on the main thread.</param>
- public static void ExecuteOnMainThread(Action _action)
- {
- if (_action == null)
- {
- Console.WriteLine("No action to execute on main thread!");
- return;
- }
-
- lock (executeOnMainThread)
- {
- executeOnMainThread.Add(_action);
- actionToExecuteOnMainThread = true;
- }
- }
-
- /// <summary>Executes all code meant to run on the main thread. NOTE: Call this ONLY from the main thread.</summary>
- public static void UpdateMain()
- {
- if (actionToExecuteOnMainThread)
- {
- executeCopiedOnMainThread.Clear();
- lock (executeOnMainThread)
- {
- executeCopiedOnMainThread.AddRange(executeOnMainThread);
- executeOnMainThread.Clear();
- actionToExecuteOnMainThread = false;
- }
-
- for (int i = 0; i < executeCopiedOnMainThread.Count; i++)
- {
- executeCopiedOnMainThread[i]();
- }
- }
- }
- }
|