(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -15,12 +15,19 @@ namespace Managers
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
{
|
||||
if (factionEntities.TryGetValue(factionKey, out var entities))
|
||||
{
|
||||
return entities; // 如果找到,返回对应的实体列表
|
||||
}
|
||||
return new List<EntityPrefab>(); // 如果未找到,返回一个空列表
|
||||
}
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var faction in factionEntities)
|
||||
{
|
||||
List<EntityPrefab> entitiesToRemove = new List<EntityPrefab>();
|
||||
var entitiesToRemove = new List<EntityPrefab>();
|
||||
|
||||
foreach (var entityPrefab in faction.Value)
|
||||
{
|
||||
@ -34,7 +41,6 @@ namespace Managers
|
||||
itike.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有标记为死亡的实体
|
||||
foreach (var entityToRemove in entitiesToRemove)
|
||||
{
|
||||
@ -56,7 +62,7 @@ namespace Managers
|
||||
/// </remarks>
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
// 检查entityPrefab是否为空
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
@ -64,7 +70,7 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查pawnDef是否为空
|
||||
// 检查 pawnDef 是否为空
|
||||
if (pawnDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
@ -72,26 +78,27 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject instantiatedEntity = null; // 用于跟踪已实例化的对象
|
||||
|
||||
try
|
||||
{
|
||||
// 实例化实体对象
|
||||
var entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
instantiatedEntity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
|
||||
// 获取EntityPrefab组件
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
// 获取 EntityPrefab 组件
|
||||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查EntityPrefab组件是否存在
|
||||
// 检查 EntityPrefab 组件是否存在
|
||||
if (entityComponent == null)
|
||||
{
|
||||
Debug.LogError($"Error: EntityPrefab component not found on the instantiated object: {entity.name}");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
|
||||
}
|
||||
|
||||
// 初始化实体组件
|
||||
entityComponent.Init(pawnDef);
|
||||
|
||||
// 确保派系键存在,并初始化对应的列表
|
||||
var factionKey = pawnDef.attributes.label == null ? "default" : pawnDef.attributes.label;
|
||||
var factionKey = pawnDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
@ -100,21 +107,30 @@ namespace Managers
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity != null)
|
||||
{
|
||||
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(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
var entity = Instantiate(defaultEntityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
const string factionKey = "default";
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
entityComponent.DefaultInit();
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
protected override void OnStart()
|
||||
|
@ -25,7 +25,6 @@ namespace Managers
|
||||
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
|
||||
InitImageDef();
|
||||
InitDrawOrder();
|
||||
packagesImages = null;
|
||||
}
|
||||
|
||||
public void InitImageDef()
|
||||
@ -54,39 +53,153 @@ namespace Managers
|
||||
|
||||
public void InitDrawOrder()
|
||||
{
|
||||
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
|
||||
if (drawOrderDef == null || drawOrderDef.Length == 0)
|
||||
try
|
||||
{
|
||||
return;
|
||||
}
|
||||
Dictionary<string, string> packRootSite = new();
|
||||
foreach (var drawOrder in drawOrderDef)
|
||||
{
|
||||
if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
|
||||
continue;
|
||||
if (!packRootSite.ContainsKey(drawOrder.packID))
|
||||
packRootSite[drawOrder.packID] = Managers.DefineManager.Instance.GetPackagePath(drawOrder.packID);
|
||||
var rootPath= packRootSite[drawOrder.packID];
|
||||
var folderPath=Path.Combine(rootPath, drawOrder.texturePath);
|
||||
var imagePath = Configs.ConfigProcessor.GetFilesByExtensions(folderPath,
|
||||
new[]
|
||||
{
|
||||
"jpg", "jpeg", "png", "tga", "tif", "tiff", "psd", "bmp"
|
||||
});
|
||||
foreach (var path in imagePath)
|
||||
// 查询绘制顺序定义
|
||||
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
|
||||
if (drawOrderDef == null || drawOrderDef.Length == 0)
|
||||
{
|
||||
var image=Configs.ConfigProcessor.LoadTextureByIO(path);
|
||||
if (image == null)
|
||||
continue;
|
||||
var spr=Sprite.Create(
|
||||
image,
|
||||
new Rect(0, 0, image.width, image.height),
|
||||
new Vector2(0.5f, 0.5f) // 中心点
|
||||
);
|
||||
var name=Path.GetFileNameWithoutExtension(path);
|
||||
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
|
||||
|
||||
Debug.LogWarning("No DrawingOrderDef found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化包路径字典
|
||||
Dictionary<string, string> packRootSite = new();
|
||||
|
||||
foreach (var drawOrder in drawOrderDef)
|
||||
{
|
||||
// 检查必要字段是否为空
|
||||
if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Skipping invalid drawOrder: texturePath or packID is null or empty. PackID: {drawOrder.packID}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取包路径
|
||||
if (!packRootSite.ContainsKey(drawOrder.packID))
|
||||
{
|
||||
var packagePath = Managers.DefineManager.Instance.GetPackagePath(drawOrder.packID);
|
||||
if (string.IsNullOrEmpty(packagePath))
|
||||
{
|
||||
Debug.LogError($"Package path not found for packID: {drawOrder.packID}");
|
||||
continue;
|
||||
}
|
||||
|
||||
packRootSite[drawOrder.packID] = packagePath;
|
||||
}
|
||||
|
||||
// 判断是否为 Unity 资源路径
|
||||
bool isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
|
||||
string rootPath = packRootSite[drawOrder.packID];
|
||||
|
||||
if (isUnityResource)
|
||||
{
|
||||
// 移除 "res:" 前缀并适配 Unity 资源路径规则
|
||||
string resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
|
||||
|
||||
// 加载文件夹下的所有纹理资源
|
||||
Texture2D[] textures = Resources.LoadAll<Texture2D>(resourceFolder);
|
||||
if (textures == null || textures.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"No textures found in Unity resource folder: {resourceFolder}");
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var image in textures)
|
||||
{
|
||||
if (image == null)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Texture loaded from Unity resource folder: {resourceFolder} is null.");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 创建精灵
|
||||
try
|
||||
{
|
||||
var spr = Sprite.Create(
|
||||
image,
|
||||
new Rect(0, 0, image.width, image.height),
|
||||
new Vector2(0.5f, 0.5f), // 中心点
|
||||
drawOrder.pixelsPerUnit
|
||||
);
|
||||
|
||||
var name = image.name;
|
||||
|
||||
// 插入纹理
|
||||
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"Failed to create sprite from Unity resource: {image.name}. Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 文件系统路径处理
|
||||
var folderPath = Path.Combine(rootPath, drawOrder.texturePath);
|
||||
|
||||
// 获取图像文件列表
|
||||
try
|
||||
{
|
||||
var imagePath = Configs.ConfigProcessor.GetFilesByExtensions(folderPath,
|
||||
new[] { "jpg", "jpeg", "png", "tga", "tif", "tiff", "psd", "bmp" });
|
||||
|
||||
foreach (var path in imagePath)
|
||||
{
|
||||
// 加载纹理
|
||||
Texture2D image = null;
|
||||
try
|
||||
{
|
||||
image = Configs.ConfigProcessor.LoadTextureByIO(path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"Failed to load texture from path: {path}. Error: {ex.Message}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (image == null)
|
||||
{
|
||||
Debug.LogWarning($"Texture loaded from path: {path} is null.");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 创建精灵
|
||||
try
|
||||
{
|
||||
var spr = Sprite.Create(
|
||||
image,
|
||||
new Rect(0, 0, image.width, image.height),
|
||||
new Vector2(0.5f, 0.5f), // 中心点
|
||||
drawOrder.pixelsPerUnit
|
||||
);
|
||||
|
||||
var name = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
// 插入纹理
|
||||
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"Failed to create sprite from texture: {path}. Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"Failed to retrieve files from folder: {folderPath}. Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"An unexpected error occurred in InitDrawOrder: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
87
Client/Assets/Scripts/Managers/TileManager.cs
Normal file
87
Client/Assets/Scripts/Managers/TileManager.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class TileManager:Utils.Singleton<TileManager>
|
||||
{
|
||||
public Dictionary<string,TileBase> tileBaseMapping = new();
|
||||
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
|
||||
public Dictionary<string, int> tileID = new();
|
||||
public void Init()
|
||||
{
|
||||
if (tileToTileBaseMapping.Count > 0)
|
||||
return;
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
var imagePack = Managers.PackagesImageManager.Instance;
|
||||
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
|
||||
for (var i = 0; i < tileType.Length; i++)
|
||||
{
|
||||
tileID.Add(tileType[i].name, i);
|
||||
}
|
||||
|
||||
var tileTextureMappingDef=Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
foreach (var mappingTableDef in tileTextureMappingDef)
|
||||
{
|
||||
foreach (var keyVal in mappingTableDef.tileDict)
|
||||
{
|
||||
var key = keyVal.Key;
|
||||
var val = keyVal.Value;
|
||||
var parts = key.Split('_');
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}内容不合法!\n应该为[瓦片名称_瓦片名称_瓦片名称_瓦片名称]的格式");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(tileID.TryGetValue(parts[0], out var k1) &&
|
||||
tileID.TryGetValue(parts[1], out var k2) &&
|
||||
tileID.TryGetValue(parts[2], out var k3) &&
|
||||
tileID.TryGetValue(parts[3], out var k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}中存在未定义的瓦片名称");
|
||||
continue;
|
||||
}
|
||||
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID,val);
|
||||
if (sprite == null)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{val}中存在未定义的图片名称");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogWarning($"来自{packName}定义的TileMappingTableDef键值{(k1, k2, k3, k4)}存在重复索引,将忽略重复项");
|
||||
continue;
|
||||
}
|
||||
|
||||
var tile = LoadTile(sprite);
|
||||
tileToTileBaseMapping[(k1, k2, k3, k4)] = tile;
|
||||
tileBaseMapping[val] = tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
tileToTileBaseMapping.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
public TileBase LoadTile(Sprite sprite)
|
||||
{
|
||||
var newTile = ScriptableObject.CreateInstance<Tile>();
|
||||
newTile.sprite = sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = Tile.ColliderType.Sprite;
|
||||
return newTile;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/TileManager.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/TileManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff3c1eb2791148068c9f03baeb04f96f
|
||||
timeCreated: 1754375678
|
Reference in New Issue
Block a user