using Cysharp.Threading.Tasks; using Gpm.Ui.Sample; using System; using System.Collections.Generic; using System.ComponentModel; using UnityEngine; using UVC.Core; using UVC.Pool; using static UnityEngine.EventSystems.EventTrigger; public class EntityManager : SingletonScene { private readonly Dictionary prefabPaths = new Dictionary() { {"box","prefabs/Box"} }; private Dictionary entityDatas = new Dictionary(); Dictionary possessComponents= new Dictionary(); public Vector3 ObjectSize; public event Action OnEntityDestroyed; private GameObjectPool? entityPool; public GameObjectPool EntityPool { get { if (entityPool == null) { Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first."); } return entityPool!; } } protected override void Init() { InitializeEntityPoolAsync(); } private async UniTask InitializeEntityPoolAsync() { if (entityPool != null) return; var prefab = await Resources.LoadAsync(prefabPaths["box"]) as GameObject; if (prefab == null) { Debug.LogError($"Prefab not found at path: {prefabPaths["box"]}"); return; } entityPool = new GameObjectPool(prefab, transform); ObjectSize = prefab.GetComponent().sharedMesh.bounds.size; } public Entity SpawnEntity(string entityName,ComponentBase component) { //Debug.Log($"spawn{entityName}"); var entity = entityPool.GetItem($"{entityName}"); entity.name = entityName; entityDatas.Add(entityName, entity); possessComponents.Add(entity, component); return entity; } public List SpawnEntites(List entityNames,ComponentBase component) { List entities = new List(); foreach (var entityName in entityNames) { //Debug.Log($"spawn{entityName}"); var entity = entityPool.GetItem($"{entityName}"); entity.name = entityName; if (!entityDatas.ContainsKey(entityName)) { entityDatas.Add(entityName, entity); entities.Add(entity); possessComponents.Add(entity,component); } } return entities; } public Entity GetEntity(string entityName,ComponentBase component) { if (entityDatas.ContainsKey(entityName)) { var entity = entityDatas[entityName]; possessComponents[entity].DecreaseEntity(entity); possessComponents[entity] = component; return entity; } else { return SpawnEntity(entityName,component); } } public List GetEntities(List entityNames, ComponentBase component) { List entities = new List(); foreach (var entityName in entityNames) { var entity = entityDatas[entityName]; possessComponents[entity].DecreaseEntity(entity); possessComponents[entity] = component; entities.Add(entity); } return entities; } public void DestroyEnity(List entityNames) { foreach (var entityName in entityNames) { if (entityDatas.ContainsKey(entityName)) { var entity = entityDatas[entityName]; possessComponents[entity].DecreaseEntity(entity); possessComponents.Remove(entity); OnEntityDestroyed?.Invoke(entity); Destroy(entity.gameObject); entityDatas.Remove(entityName); } } } }