(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user