Files
XRLib/Assets/Scripts/Simulator/Model/EntityManager.cs

138 lines
3.9 KiB
C#
Raw Permalink Normal View History

2025-11-04 11:02:02 +09:00
using Cysharp.Threading.Tasks;
using System;
2025-10-16 10:24:29 +09:00
using System.Collections.Generic;
using UnityEngine;
using UVC.Core;
using UVC.Pool;
public class EntityManager : SingletonScene<EntityManager>
{
private readonly Dictionary<string, string> prefabPaths = new Dictionary<string, string>()
{
{"box","prefabs/Box"}
};
2025-11-04 11:02:02 +09:00
private Dictionary<string, Entity> entityDatas = new Dictionary<string, Entity>();
public Vector3 ObjectSize;
public event Action<Entity> OnEntityDestroyed;
2026-02-26 14:04:33 +09:00
public event Action<Entity> OnEntityTransferred;
2025-10-16 10:24:29 +09:00
private GameObjectPool<Entity>? entityPool;
public GameObjectPool<Entity> EntityPool
{
get
{
if (entityPool == null)
{
Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first.");
}
return entityPool!;
}
}
2025-11-04 11:02:02 +09:00
protected override void Init()
{
2026-02-25 16:30:12 +09:00
InitializeEntityPoolAsync().Forget();
2025-11-04 11:02:02 +09:00
}
private async UniTask InitializeEntityPoolAsync()
{
if (entityPool != null) return;
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths["box"]) as GameObject;
if (prefab == null)
{
Debug.LogError($"Prefab not found at path: {prefabPaths["box"]}");
return;
}
entityPool = new GameObjectPool<Entity>(prefab, transform);
ObjectSize = prefab.GetComponent<MeshFilter>().sharedMesh.bounds.size;
}
2026-02-26 14:04:33 +09:00
public Entity SpawnEntity(string entityName, string prefabName)
2025-11-04 11:02:02 +09:00
{
var entity = entityPool.GetItem($"{entityName}");
entity.name = entityName;
2025-12-11 18:26:09 +09:00
SetEntity(entity, prefabName);
2026-02-10 17:01:25 +09:00
if (!entityDatas.ContainsKey(entityName))
{
entityDatas.Add(entityName, entity);
}
else
{
entityDatas[entityName] = entity;
}
2026-02-26 14:04:33 +09:00
return entity;
2025-11-04 11:02:02 +09:00
}
2026-02-26 14:04:33 +09:00
public List<Entity> SpawnEntites(List<string> entityNames)
2025-10-16 10:24:29 +09:00
{
2025-11-04 11:02:02 +09:00
List<Entity> entities = new List<Entity>();
foreach (var entityName in entityNames)
{
var entity = entityPool.GetItem($"{entityName}");
entity.name = entityName;
if (!entityDatas.ContainsKey(entityName))
{
entityDatas.Add(entityName, entity);
entities.Add(entity);
}
}
return entities;
}
2026-02-26 14:04:33 +09:00
public Entity GetEntity(string entityName, string prefabName = null)
2025-11-04 11:02:02 +09:00
{
if (entityDatas.ContainsKey(entityName))
{
var entity = entityDatas[entityName];
2026-02-26 14:04:33 +09:00
OnEntityTransferred?.Invoke(entity);
2025-11-04 11:02:02 +09:00
return entity;
}
else
{
2026-02-26 14:04:33 +09:00
return SpawnEntity(entityName, prefabName);
2025-11-04 11:02:02 +09:00
}
}
2026-02-26 14:04:33 +09:00
public List<Entity> GetEntities(List<string> entityNames, List<string> prefabNames = null)
2025-11-04 11:02:02 +09:00
{
List<Entity> entities = new List<Entity>();
foreach (var entityName in entityNames)
{
2026-02-26 14:04:33 +09:00
var entity = GetEntity(entityName);
2025-11-04 11:02:02 +09:00
entities.Add(entity);
}
return entities;
}
2026-02-26 14:04:33 +09:00
public void SetEntity(Entity entity, string prefabName)
2025-12-11 18:26:09 +09:00
{
var prefabData = PrefabManager.Instance.GetPrefab(prefabName);
if (!string.IsNullOrEmpty(prefabName) && prefabData != null)
{
2026-02-03 11:40:26 +09:00
string colorHex = $"{prefabData.color}";
2025-12-11 18:26:09 +09:00
if (ColorUtility.TryParseHtmlString(colorHex, out var color))
{
entity.SetColor(color);
}
}
}
2025-11-04 11:02:02 +09:00
public void DestroyEnity(List<string> entityNames)
{
foreach (var entityName in entityNames)
{
if (entityDatas.ContainsKey(entityName))
{
var entity = entityDatas[entityName];
2026-02-26 14:04:33 +09:00
OnEntityTransferred?.Invoke(entity);
2025-11-04 11:02:02 +09:00
OnEntityDestroyed?.Invoke(entity);
Destroy(entity.gameObject);
entityDatas.Remove(entityName);
}
}
2025-10-16 10:24:29 +09:00
}
}