IndieGame/client/Assets/Ether/Scripts/Module/Entity/EntityManager.cs

85 lines
2.0 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ether
{
public class EntityManager : SingletonAutoMono<EntityManager>
{
Dictionary<string, Entity> allEntityDic = new Dictionary<string, Entity>();
public void AddEntity(Entity entity)
{
if (string.IsNullOrEmpty(entity.key))
{
return;
}
if (allEntityDic.TryGetValue(entity.key, out Entity saveKeyEntity))
{
Debug.LogError($"有重复EntityKey:{entity.key}, 有重复EntityName:{entity.name}字典中的EntityName:{saveKeyEntity.name}");
return;
}
allEntityDic.Add(entity.key, entity);
}
public void RemoveEntity(Entity entity)
{
if (allEntityDic.ContainsKey(entity.key))
{
allEntityDic.Remove(entity.key);
}
}
public void RemoveEntity(string key)
{
if (allEntityDic.TryGetValue(key, out Entity entity))
{
RemoveEntity(entity);
}
}
public Entity GetEntity(string key)
{
if (allEntityDic.TryGetValue(key, out Entity saveEntity))
{
return saveEntity;
}
return null;
}
public override void Clear()
{
allEntityDic.Clear();
}
public void CheckEmptyEntity()
{
List<string> removeIds = new List<string>();
foreach (var entityPair in allEntityDic)
{
if (entityPair.Value == null)
{
removeIds.Add(entityPair.Key);
}
}
for (int i = 0; i < removeIds.Count; i++)
{
RemoveEntity(removeIds[i]);
}
}
private void OnDestroy()
{
Clear();
}
}
}