using System.Collections.Generic; using UnityEngine; namespace ZC { public interface IObjectManager { void Add(IBehaviour behaviour); void Remove(long id); void Clear(); } internal class ObjectManager : ManagerBase, IObjectManager { private Dictionary pool; public override void OnInit() { base.OnInit(); this.pool = new Dictionary(); } public override void OnUpdate(float time) { base.OnUpdate(time); foreach (var behaviour in this.pool.Values) { if (behaviour.isDisposed) { this.pool.Remove(behaviour.Id); continue; } behaviour.OnUpdate(time); } } public override void OnDispose() { base.OnDispose(); this.Clear(); } #region Dic public void Add(IBehaviour behaviour) { behaviour.OnInit(); this.pool.Add(behaviour.Id, behaviour); } public void Remove(long id) { if (this.pool.TryGetValue(id, out var behaviour)) { behaviour?.OnDispose(); } this.pool.Remove(id); } public void Clear() { foreach (var value in this.pool.Values) { value.OnDispose(); } this.pool.Clear(); } #endregion } }