Test de communication de paquets entre un client et un serveur
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ThreadManager.cs 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class ThreadManager : MonoBehaviour
  6. {
  7. private static readonly List<Action> executeOnMainThread = new List<Action>();
  8. private static readonly List<Action> executeCopiedOnMainThread = new List<Action>();
  9. private static bool actionToExecuteOnMainThread = false;
  10. private void FixedUpdate()
  11. {
  12. UpdateMain();
  13. }
  14. /// <summary>Sets an action to be executed on the main thread.</summary>
  15. /// <param name="_action">The action to be executed on the main thread.</param>
  16. public static void ExecuteOnMainThread(Action _action)
  17. {
  18. if (_action == null)
  19. {
  20. Console.WriteLine("No action to execute on main thread!");
  21. return;
  22. }
  23. lock (executeOnMainThread)
  24. {
  25. executeOnMainThread.Add(_action);
  26. actionToExecuteOnMainThread = true;
  27. }
  28. }
  29. /// <summary>Executes all code meant to run on the main thread. NOTE: Call this ONLY from the main thread.</summary>
  30. public static void UpdateMain()
  31. {
  32. if (actionToExecuteOnMainThread)
  33. {
  34. executeCopiedOnMainThread.Clear();
  35. lock (executeOnMainThread)
  36. {
  37. executeCopiedOnMainThread.AddRange(executeOnMainThread);
  38. executeOnMainThread.Clear();
  39. actionToExecuteOnMainThread = false;
  40. }
  41. for (int i = 0; i < executeCopiedOnMainThread.Count; i++)
  42. {
  43. executeCopiedOnMainThread[i]();
  44. }
  45. }
  46. }
  47. }