(client) chore:将图片包加载添加包ID区分

This commit is contained in:
m0_75251201
2025-07-21 16:17:12 +08:00
parent 28ddcda9a0
commit 506d0a68a8
3 changed files with 55 additions and 30 deletions

View File

@ -30,9 +30,7 @@ namespace Entity
}
private bool _isPlayerControlled = false;
private const int WarningInterval = 5000;
private int _warningTicks = 0;
private bool _warning = false;
public void Tick()
{
@ -85,13 +83,11 @@ namespace Entity
currentJob = aiTree.GetJob(this);
if (currentJob == null)
{
if (_warningTicks<=0)
if (!_warning)
{
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
_warningTicks += WarningInterval;
_warning = true;
}
_warningTicks--;
return;
}
currentJob.StartJob(this);

View File

@ -7,29 +7,42 @@ namespace Managers
{
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
{
public Dictionary<string, Texture2D> packagesImages = new();
public Dictionary<string, Sprite> sprites = new();
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
public void Init()
{
if (packagesImages.Count > 0)
return;
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
foreach (var ima in imageDef)
{
if (ima.path == null)
if (ima.path == null || ima.packID == null)
continue;
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
var path = Path.Combine(pack.packRootPath, ima.path);
var texture = Configs.ConfigProcessor.LoadTextureByIO(path);
if (texture == null)
continue;
packagesImages.Add(ima.name, texture);
SplitTextureIntoSprites(ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
continue;
var packId = ima.packID;
if (!packagesImages.ContainsKey(packId))
packagesImages[packId] = new Dictionary<string, Texture2D>();
packagesImages[packId].Add(ima.name, texture);
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
}
}
void SplitTextureIntoSprites(string name, Texture2D texture, int rows, int cols, int pixelsPerUnit)
private void SplitTextureIntoSprites(
string packId,
string baseName,
Texture2D texture,
int rows,
int cols,
int pixelsPerUnit)
{
if (texture == null || rows <= 0 || cols <= 0)
{
@ -43,26 +56,26 @@ namespace Managers
var tileWidth = textureWidth / cols;
var tileHeight = textureHeight / rows;
// 确保纹理可以被整除
if (tileWidth * cols != textureWidth || tileHeight * rows != textureHeight)
{
Debug.LogError("Texture dimensions are not divisible by the specified rows and columns.");
return;
}
if (!sprites.ContainsKey(packId))
sprites[packId] = new Dictionary<string, Sprite>();
// 遍历每一行和每一列
for (var row = 0; row < rows; row++)
{
for (var col = 0; col < cols; col++)
{
// 计算当前小块的矩形区域
var spriteRect = new Rect(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
// 创建Sprite
var sprite = Sprite.Create(texture, (Rect)spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
var index = (rows - row - 1) * cols + col;
sprites[name + $"_{index}"] = sprite;
var spriteName = $"{baseName}_{index}";
sprites[packId][spriteName] = sprite;
}
}
}
@ -70,18 +83,34 @@ namespace Managers
public void Reload()
{
packagesImages.Clear();
sprites.Clear();
Init();
}
public Sprite GetSprite(string name)
public Sprite GetSprite(string packID, string name)
{
return sprites.GetValueOrDefault(name, null);
if (string.IsNullOrEmpty(packID))
{
foreach (var kvp in sprites)
{
if (kvp.Value.TryGetValue(name, out var sprite))
return sprite;
}
}
else
{
if (!sprites.TryGetValue(packID, out var dict)) return null;
dict.TryGetValue(name, out var sprite);
return sprite;
}
return null;
}
public Sprite GetSprite(string name, int index)
public Sprite GetSprite(string packID, string name, int index)
{
name += $"_{index}";
return GetSprite(name);
var fullName = $"{name}_{index}";
return GetSprite(packID, fullName);
}
}
}

View File

@ -142,7 +142,7 @@ namespace Map
continue;
}
var sprite = imagePack.GetSprite(val);
var sprite = imagePack.GetSprite(mappingTableDef.packID,val);
if (sprite == null)
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);