LoomModule.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// 线程反馈模块
  7. /// </summary>
  8. partial class LoomModule : UpdateModule
  9. {
  10. private Queue<DelayedTask> _delay;
  11. /// <summary>
  12. /// 在主线程跑一个方法
  13. /// </summary>
  14. /// <param name="action"></param>
  15. public void RunDelay(Action action)
  16. {
  17. if (action == null) return;
  18. lock (_delay)
  19. {
  20. _delay.Enqueue(new DelayedTask(action));
  21. }
  22. }
  23. protected override ModulePriority OnGetDefautPriority()
  24. {
  25. return ModulePriority.Loom;
  26. }
  27. Queue<DelayedTask> _tasks = new Queue<DelayedTask>();
  28. protected override void OnUpdate()
  29. {
  30. int count = 0;
  31. lock (_delay)
  32. {
  33. count = _delay.Count;
  34. if (count <= 0) return;
  35. for (int i = 0; i < count; i++)
  36. {
  37. _tasks.Enqueue(_delay.Dequeue());
  38. }
  39. }
  40. for (int i = 0; i < count; i++)
  41. {
  42. var _task = _tasks.Dequeue();
  43. _task.action();
  44. }
  45. }
  46. protected override void OnDispose()
  47. {
  48. _delay.Clear();
  49. _delay = null;
  50. }
  51. protected override void Awake()
  52. {
  53. _delay = new Queue<DelayedTask>();
  54. }
  55. }
  56. }