using System; using System.Threading; namespace ET { [ObjectSystem] public class NetThreadComponentAwakeSystem: AwakeSystem { public override void Awake(NetThreadComponent self) { NetThreadComponent.Instance = self; self.ThreadSynchronizationContext = ThreadSynchronizationContext.Instance; self.foreachAction = service => service.Update(); } } [ObjectSystem] public class NetThreadComponentUpdateSystem: LateUpdateSystem { public override void LateUpdate(NetThreadComponent self) { self.Services.Foreach(self.foreachAction); } } [ObjectSystem] public class NetThreadComponentDestroySystem: DestroySystem { public override void Destroy(NetThreadComponent self) { self.Stop(); } } [FriendClass(typeof(NetThreadComponent))] public static class NetThreadComponentSystem { public static void Stop(this NetThreadComponent self) { } public static void Add(this NetThreadComponent self, AService kService) { // 这里要去下一帧添加,避免foreach错误 self.ThreadSynchronizationContext.PostNext(() => { if (kService.IsDispose()) { return; } self.Services.Add(kService); }); } public static void Remove(this NetThreadComponent self, AService kService) { // 这里要去下一帧删除,避免foreach错误 self.ThreadSynchronizationContext.PostNext(() => { if (kService.IsDispose()) { return; } self.Services.Remove(kService); }); } } }