mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 06:07:12 +08:00
(client) feat:支持定义实体的碰撞体大小和偏移;建筑支持定义实体建筑和瓦片建筑,建筑支持指定按钮回调;添加存档管理器;Dev支持设置是否暂停;实体允许定义事件组;添加基地界面 (#57)
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/57
This commit is contained in:
@@ -39,13 +39,15 @@ namespace Managers
|
||||
/// <summary>
|
||||
/// 初始化图像管理器,加载默认精灵并处理所有 ImageDef 定义。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// **修正后的逻辑**:移除 <c>packagesImages.Count > 0</c> 的重复加载检查。
|
||||
/// <c>Init</c> 方法现在将始终执行完整的初始化流程,确保所有组件都被正确加载。
|
||||
/// 如果需要重新加载,应调用 <c>Reload()</c> 方法。
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void Init()
|
||||
{
|
||||
if (packagesImages.Count > 0)
|
||||
{
|
||||
// 如果已经有数据,则跳过初始化,防止重复加载。
|
||||
return;
|
||||
}
|
||||
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
|
||||
if (defaultSprite == null)
|
||||
{
|
||||
@@ -59,6 +61,10 @@ namespace Managers
|
||||
/// </summary>
|
||||
public void InitImageDef()
|
||||
{
|
||||
// 在每次 InitImageDef 调用前,清空旧数据,以确保重新初始化是干净的。
|
||||
packagesImages.Clear();
|
||||
sprites.Clear();
|
||||
|
||||
var textureCache = new Dictionary<string, Texture2D>();
|
||||
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
|
||||
|
||||
@@ -79,7 +85,7 @@ namespace Managers
|
||||
try
|
||||
{
|
||||
string cacheKey;
|
||||
Texture2D texture = null;
|
||||
Texture2D texture;
|
||||
|
||||
if (ima.path.StartsWith("res:"))
|
||||
{
|
||||
@@ -146,27 +152,28 @@ namespace Managers
|
||||
// 资源加载失败
|
||||
if (!texture)
|
||||
{
|
||||
Debug.LogError($"未能加载图像定义关联的纹理: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}')。请验证路径和文件是否存在。");
|
||||
Debug.LogError($"未能加载图像定义关联的纹理: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}')。" +
|
||||
$"请验证文件路径、文件权限以及Unity Asset导入设置中的 'Read/Write Enabled' 选项。");
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- 修改开始 ---
|
||||
Texture2D processedTexture = texture;
|
||||
|
||||
var processedTexture = texture;
|
||||
// 如果需要翻转,创建翻转后的纹理
|
||||
if (ima.flipX || ima.flipY)
|
||||
{
|
||||
Texture2D flippedTex = FlipTexture(texture, ima.flipX, ima.flipY);
|
||||
if (flippedTex != null)
|
||||
{
|
||||
processedTexture = flippedTex;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"未能翻转图像定义关联的纹理: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}'),将使用原始纹理。");
|
||||
}
|
||||
var flippedTex = FlipTexture(texture, ima.flipX, ima.flipY);
|
||||
if (flippedTex != null)
|
||||
{
|
||||
processedTexture = flippedTex;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"未能翻转图像定义关联的纹理: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}'),将使用原始纹理。");
|
||||
}
|
||||
}
|
||||
packagesImages[ima.defName] = processedTexture;
|
||||
|
||||
packagesImages[ima.defName] = processedTexture;
|
||||
|
||||
// 索引计算依赖这些参数来匹配游戏资源加载的预期。
|
||||
SplitTextureIntoSprites(ima.defName, processedTexture, ima.hCount, ima.wCount, ima.pixelsPerUnit,
|
||||
ima.flipX, ima.flipY);
|
||||
}
|
||||
@@ -187,14 +194,22 @@ namespace Managers
|
||||
/// <param name="rows">水平分割的行数。</param>
|
||||
/// <param name="cols">垂直分割的列数。</param>
|
||||
/// <param name="pixelsPerUnit">每个单元的像素数,用于Sprite.Create。</param>
|
||||
/// <param name="flipX">是否沿X轴翻转(此参数用于调整索引)。</param>
|
||||
/// <param name="flipY">是否沿Y轴翻转(此参数用于调整索引)。</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// **修正后的逻辑**:恢复 <c>flipX</c> 和 <c>flipY</c> 参数及其在索引计算中的使用。
|
||||
/// 这确保了精灵的编号逻辑与原始设计保持一致,避免了游戏资源加载错误。
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
private void SplitTextureIntoSprites(
|
||||
string baseName,
|
||||
Texture2D texture,
|
||||
int rows,
|
||||
int cols,
|
||||
int pixelsPerUnit,
|
||||
bool flipX = false,
|
||||
bool flipY = false)
|
||||
bool flipX = false,
|
||||
bool flipY = false)
|
||||
{
|
||||
if (!texture)
|
||||
{
|
||||
@@ -202,30 +217,26 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果行数或列数小于1,则设为1(不分割)
|
||||
rows = Mathf.Max(1, rows);
|
||||
cols = Mathf.Max(1, cols);
|
||||
|
||||
var textureWidth = texture.width;
|
||||
var textureHeight = texture.height;
|
||||
|
||||
// 创建未分割的完整精灵,使用原始名称 (baseName,即 ImageDef.name)
|
||||
var fullSpriteRect = new Rect(0, 0, textureWidth, textureHeight);
|
||||
var fullSprite = Sprite.Create(texture, fullSpriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
||||
fullSprite.name = baseName; // 确保 Sprite.name 被设置
|
||||
fullSprite.name = baseName;
|
||||
sprites[baseName] = fullSprite;
|
||||
|
||||
// 如果不分割(rows和cols都为1),提前返回
|
||||
if (rows == 1 && cols == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查纹理尺寸是否可被分割数整除
|
||||
if (textureWidth % cols != 0 || textureHeight % rows != 0)
|
||||
{
|
||||
Debug.LogError($"'{baseName}' 的纹理尺寸 ({textureWidth}x{textureHeight}) 不能被指定的行数 ({rows}) 和列数 ({cols}) 完美整除。子精灵将不会生成或可能不正确。仅显示完整精灵。");
|
||||
return; // 终止子精灵分割,只保留完整的精灵
|
||||
Debug.LogWarning($"'{baseName}' 的纹理尺寸 ({textureWidth}x{textureHeight}) 不能被指定的行数 ({rows}) 和列数 ({cols}) 完美整除。子精灵将不会生成或可能不正确。仅显示完整精灵。");
|
||||
return;
|
||||
}
|
||||
|
||||
var tileWidth = textureWidth / cols;
|
||||
@@ -237,7 +248,8 @@ namespace Managers
|
||||
{
|
||||
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
|
||||
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
||||
// 精灵索引计算方式
|
||||
|
||||
// 此处的 flipX/Y 参数用于调整索引,以符合游戏或美术的特定约定。
|
||||
var index = (flipY ? row : (rows - row - 1)) * cols + (flipX ? cols - 1 - col : col);
|
||||
var spriteName = $"{baseName}_{index}";
|
||||
sprite.name = spriteName;
|
||||
@@ -252,12 +264,12 @@ namespace Managers
|
||||
/// <param name="originalTexture">原始纹理。</param>
|
||||
/// <param name="flipX">是否沿X轴翻转。</param>
|
||||
/// <param name="flipY">是否沿Y轴翻转。</param>
|
||||
/// <returns>一个新的、翻转后的Texture2D实例,如果不需要翻转则返回原纹理。</returns>
|
||||
/// <returns>一个新的、翻转后的Texture2D实例,如果不需要翻转则返回原纹理;如果纹理不可读,返回null。</returns>
|
||||
private Texture2D FlipTexture(Texture2D originalTexture, bool flipX, bool flipY)
|
||||
{
|
||||
if (!flipX && !flipY)
|
||||
{
|
||||
return originalTexture; // 无需翻转,返回原始纹理
|
||||
return originalTexture;
|
||||
}
|
||||
|
||||
if (originalTexture == null)
|
||||
@@ -265,38 +277,32 @@ namespace Managers
|
||||
Debug.LogError("FlipTexture: 原始纹理为null,无法翻转。");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Unity Texture2D 的 GetPixels/SetPixels 操作要求纹理是可读写的。
|
||||
// 确保纹理的可读写性,或创建一个可读写的副本。
|
||||
// Configs.ConfigProcessor.LoadTextureByIO 和 Resources.Load的纹理,
|
||||
// 它们的"Read/Write Enabled"导入设置必须开启。
|
||||
// 这里为了确保纹理处理成功,我们将像素复制到一个新的可读写纹理中。
|
||||
int width = originalTexture.width;
|
||||
int height = originalTexture.height;
|
||||
|
||||
// 创建一个全新的Texture2D来承载翻转后的像素。
|
||||
// 使用Color32代替Color,效率更高且通常直接对应原始图像数据。
|
||||
Texture2D flippedTexture = new Texture2D(width, height, originalTexture.format, originalTexture.mipmapCount > 1);
|
||||
// 给翻转后的纹理一个名字,方便调试
|
||||
if (!originalTexture.isReadable)
|
||||
{
|
||||
Debug.LogError($"FlipTexture: 纹理 '{originalTexture.name}' (或其来源) 不可读。 " +
|
||||
$"请确保在Unity导入设置中勾选 'Read/Write Enabled' 选项。无法进行翻转。");
|
||||
return null;
|
||||
}
|
||||
|
||||
var width = originalTexture.width;
|
||||
var height = originalTexture.height;
|
||||
|
||||
var flippedTexture = new Texture2D(width, height, originalTexture.format, originalTexture.mipmapCount > 1);
|
||||
flippedTexture.name = originalTexture.name + (flipX ? "_flippedX" : "") + (flipY ? "_flippedY" : "");
|
||||
|
||||
Color32[] originalPixels = originalTexture.GetPixels32();
|
||||
Color32[] flippedPixels = new Color32[width * height];
|
||||
var originalPixels = originalTexture.GetPixels32();
|
||||
var flippedPixels = new Color32[width * height];
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
// 计算原始像素的索引
|
||||
int originalIndex = y * width + x;
|
||||
var originalIndex = y * width + x;
|
||||
var targetX = flipX ? (width - 1 - x) : x;
|
||||
var targetY = flipY ? (height - 1 - y) : y;
|
||||
var flippedIndex = targetY * width + targetX;
|
||||
|
||||
// 计算翻转后像素的目标坐标
|
||||
int targetX = flipX ? (width - 1 - x) : x;
|
||||
int targetY = flipY ? (height - 1 - y) : y;
|
||||
|
||||
// 计算翻转后像素的目标索引
|
||||
int flippedIndex = targetY * width + targetX;
|
||||
|
||||
if (originalIndex >= 0 && originalIndex < originalPixels.Length &&
|
||||
flippedIndex >= 0 && flippedIndex < flippedPixels.Length)
|
||||
{
|
||||
@@ -304,14 +310,13 @@ namespace Managers
|
||||
}
|
||||
else
|
||||
{
|
||||
// 理论上不应该发生,但作为安全措施
|
||||
Debug.LogWarning($"FlipTexture: 像素索引计算异常,原始: ({x},{y}) -> 索引 {originalIndex};翻转: ({targetX},{targetY}) -> 索引 {flippedIndex}. Texture: {originalTexture.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
flippedTexture.SetPixels32(flippedPixels);
|
||||
flippedTexture.Apply(); // 应用像素更改到GPU
|
||||
flippedTexture.Apply();
|
||||
|
||||
return flippedTexture;
|
||||
}
|
||||
@@ -319,10 +324,6 @@ namespace Managers
|
||||
/// <summary>
|
||||
/// 清理所有已加载的纹理和精灵数据。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 此方法会清空 <see cref="packagesImages"/> 和 <see cref="sprites"/> 字典,
|
||||
/// 但不会卸载 <see cref="defaultSprite"/>,因为它通常通过 Resources.Load 加载,由 Unity 管理其生命周期。
|
||||
/// </remarks>
|
||||
public void Clear()
|
||||
{
|
||||
packagesImages.Clear();
|
||||
@@ -332,21 +333,17 @@ namespace Managers
|
||||
/// <summary>
|
||||
/// 重新加载所有图像数据。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 此方法会首先调用 <see cref="Clear()"/> 清理所有数据,然后调用 <see cref="Init()"/> 重新初始化。
|
||||
/// </remarks>
|
||||
public void Reload()
|
||||
{
|
||||
Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据 <see cref="ImageDef"/> 对象获取对应的精灵。
|
||||
/// 根据 <see cref="Data.ImageDef"/> 对象获取对应的精灵。
|
||||
/// </summary>
|
||||
/// <param name="ima">包含精灵名称的 <see cref="ImageDef"/> 对象。</param>
|
||||
/// <returns>如果找到对应的精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
|
||||
/// <param name="ima">图像定义对象。</param>
|
||||
/// <returns>对应的精灵,如果 <paramref name="ima"/> 为空,则返回默认精灵。</returns>
|
||||
public Sprite GetSprite(ImageDef ima)
|
||||
{
|
||||
return ima == null ? defaultSprite : GetSprite(ima.defName);
|
||||
@@ -355,10 +352,15 @@ namespace Managers
|
||||
/// <summary>
|
||||
/// 根据精灵名称(全局唯一的DefName)获取对应的精灵。
|
||||
/// </summary>
|
||||
/// <param name="name">精灵的名称。</param>
|
||||
/// <returns>如果找到对应的精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
|
||||
/// <param name="name">要获取的精灵名称。</param>
|
||||
/// <returns>对应的精灵,如果名称为空或找不到,则返回默认精灵。</returns>
|
||||
public Sprite GetSprite(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
Debug.LogWarning($"GetSprite: 尝试获取的精灵名称为空或null。返回默认精灵。");
|
||||
return defaultSprite;
|
||||
}
|
||||
return sprites.GetValueOrDefault(name, defaultSprite);
|
||||
}
|
||||
|
||||
@@ -367,13 +369,18 @@ namespace Managers
|
||||
/// </summary>
|
||||
/// <param name="name">精灵的基础名称。</param>
|
||||
/// <param name="index">子精灵的索引。</param>
|
||||
/// <returns>如果找到对应的子精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
|
||||
/// <returns>对应的子精灵,如果找不到,则返回默认精灵。</returns>
|
||||
public Sprite GetSprite(string name, int index)
|
||||
{
|
||||
var fullName = $"{name}_{index}";
|
||||
return GetSprite(fullName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据精灵名称数组获取对应的精灵数组。
|
||||
/// </summary>
|
||||
/// <param name="names">精灵名称数组。</param>
|
||||
/// <returns>对应的精灵数组,如果输入为空,则返回包含默认精灵的数组。</returns>
|
||||
public Sprite[] GetSprites(string[] names)
|
||||
{
|
||||
if (names == null || names.Length == 0)
|
||||
|
||||
Reference in New Issue
Block a user