Test de communication de paquets entre un client et un serveur
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ThreadManager.cs 1.6KB

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