74 lines
1.6 KiB
C#
74 lines
1.6 KiB
C#
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
|
|
}
|
|
} |