(client) feat:做了初始化加载动画。 fix:修复定义加载列表和加载枚举时的错误识别
This commit is contained in:
@ -10,18 +10,15 @@ namespace Managers
|
||||
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
|
||||
{
|
||||
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public GameObject entityLevel;
|
||||
|
||||
public EntityPrefab entityPrefab;
|
||||
public GameObject buildingLevel;
|
||||
public EntityPrefab buildingPrefab;
|
||||
public GameObject bulletLevel;
|
||||
public EntityPrefab bulletPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
|
||||
private List<Tuple<string,EntityPrefab>> pendingAdditions;
|
||||
private Dictionary<string, Transform> layerCache = new Dictionary<string, Transform>();
|
||||
private List<Tuple<string, EntityPrefab>> pendingAdditions;
|
||||
|
||||
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
{
|
||||
@ -71,6 +68,7 @@ namespace Managers
|
||||
|
||||
factionEntities[entity.Item1].AddLast(entity.Item2);
|
||||
}
|
||||
|
||||
pendingAdditions.Clear();
|
||||
}
|
||||
}
|
||||
@ -112,16 +110,8 @@ namespace Managers
|
||||
extraInit?.Invoke(entityComponent);
|
||||
|
||||
// 管理派系列表
|
||||
var factionKey = def.attributes.label ?? "default";
|
||||
var factionKey = def.attributes.defName ?? "default";
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
|
||||
// if (!factionEntities.ContainsKey(factionKey))
|
||||
// {
|
||||
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
|
||||
// }
|
||||
|
||||
// factionEntities[factionKey].AddLast(entityComponent);
|
||||
|
||||
return entityComponent;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
@ -134,6 +124,34 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态创建层(如果层不存在)
|
||||
/// </summary>
|
||||
private Transform EnsureLayerExists(string layerName)
|
||||
{
|
||||
// 先从缓存中查找
|
||||
if (layerCache.TryGetValue(layerName, out var layerTransform))
|
||||
{
|
||||
return layerTransform;
|
||||
}
|
||||
|
||||
// 如果缓存中没有,尝试通过 transform.Find 查找
|
||||
layerTransform = transform.Find(layerName);
|
||||
|
||||
if (!layerTransform)
|
||||
{
|
||||
// 如果层不存在,动态创建
|
||||
var layerObject = new GameObject(layerName);
|
||||
layerTransform = layerObject.transform;
|
||||
layerTransform.SetParent(transform, false); // 将层附加到当前管理器下
|
||||
}
|
||||
|
||||
// 将新创建的层加入缓存
|
||||
layerCache[layerName] = layerTransform;
|
||||
|
||||
return layerTransform;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据PawnDef生成普通实体
|
||||
/// </summary>
|
||||
@ -154,10 +172,13 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保层存在
|
||||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||||
|
||||
// 调用通用生成逻辑
|
||||
var result = GenerateEntityInternal(
|
||||
entityPrefab.gameObject,
|
||||
entityLevel.transform,
|
||||
entityLevelTransform,
|
||||
pos,
|
||||
entityDef
|
||||
);
|
||||
@ -187,14 +208,17 @@ namespace Managers
|
||||
|
||||
var worldPos = new Vector3(pos.x, pos.y, pos.z);
|
||||
|
||||
// 确保层存在
|
||||
var buildingLevelTransform = EnsureLayerExists("BuildingLevel");
|
||||
|
||||
var result = GenerateEntityInternal(
|
||||
buildingPrefab.gameObject,
|
||||
buildingLevel.transform,
|
||||
buildingLevelTransform,
|
||||
worldPos,
|
||||
buildingDef
|
||||
);
|
||||
|
||||
if (!result) GenerateDefaultEntity(pos);
|
||||
if (!result) GenerateDefaultEntity(worldPos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -217,9 +241,12 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保层存在
|
||||
var bulletLevelTransform = EnsureLayerExists("BulletLevel");
|
||||
|
||||
var result = GenerateEntityInternal(
|
||||
bulletPrefab.gameObject,
|
||||
bulletLevel.transform,
|
||||
bulletLevelTransform,
|
||||
pos,
|
||||
bulletDef,
|
||||
// 子弹特有的方向设置
|
||||
@ -234,18 +261,16 @@ namespace Managers
|
||||
/// </summary>
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevel.transform);
|
||||
// 确保层存在
|
||||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||||
|
||||
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevelTransform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
|
||||
const string factionKey = "default";
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
// if (!factionEntities.ContainsKey(factionKey))
|
||||
// {
|
||||
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
|
||||
// }
|
||||
|
||||
// entityComponent.DefaultInit();
|
||||
// factionEntities[factionKey].AddLast(entityComponent);
|
||||
entityComponent.DefaultInit();
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
@ -258,7 +283,7 @@ namespace Managers
|
||||
{
|
||||
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
||||
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
||||
|
||||
layerCache.Clear();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user