(client) feat:右键菜单全局与UI分离,Building生成支持,双层地图重写
This commit is contained in:
@ -13,6 +13,8 @@ namespace Managers
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
public GameObject buildingLevel;
|
||||
public EntityPrefab buildingPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
@ -63,7 +65,7 @@ namespace Managers
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (entityPrefab == null)
|
||||
if (!entityPrefab)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
@ -89,7 +91,7 @@ namespace Managers
|
||||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查 EntityPrefab 组件是否存在
|
||||
if (entityComponent == null)
|
||||
if (!entityComponent)
|
||||
{
|
||||
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
|
||||
}
|
||||
@ -108,7 +110,7 @@ namespace Managers
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity != null)
|
||||
if (instantiatedEntity)
|
||||
{
|
||||
Destroy(instantiatedEntity); // 删除已创建的对象
|
||||
}
|
||||
@ -121,6 +123,67 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateBuildingEntity(Data.BuildingDef buildingDef, Vector3Int pos)
|
||||
{
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (!entityPrefab)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查 pawnDef 是否为空
|
||||
if (buildingDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject instantiatedEntity = null; // 用于跟踪已实例化的对象
|
||||
|
||||
try
|
||||
{
|
||||
// 实例化实体对象
|
||||
instantiatedEntity = Instantiate(buildingPrefab.gameObject, pos, Quaternion.identity, buildingLevel.transform);
|
||||
|
||||
// 获取 EntityPrefab 组件
|
||||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查 EntityPrefab 组件是否存在
|
||||
if (!entityComponent)
|
||||
{
|
||||
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
|
||||
}
|
||||
|
||||
// 初始化实体组件
|
||||
entityComponent.Init(buildingDef);
|
||||
|
||||
// 确保派系键存在,并初始化对应的列表
|
||||
var factionKey = buildingDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity)
|
||||
{
|
||||
Destroy(instantiatedEntity); // 删除已创建的对象
|
||||
}
|
||||
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
|
||||
|
||||
// 调用默认生成方法
|
||||
GenerateDefaultEntity(pos);
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(defaultEntityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
|
Reference in New Issue
Block a user