IndieGame/client/Assets/Ether/Scripts/Module/Entity/EntityManager.cs
DOBEST\zhaoyingjie f242607587 初始化工程
2024-10-11 10:12:15 +08:00

85 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}