HAARFTE/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs

74 lines
1.6 KiB
C#
Raw Normal View History

2024-10-24 16:16:57 +08:00
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<ObjectManager>, IObjectManager
{
private Dictionary<long, IBehaviour> pool;
public override void OnInit()
{
base.OnInit();
this.pool = new Dictionary<long, IBehaviour>();
}
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
}
}