(client) feat:实现身体贴图加载
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
@ -339,5 +340,53 @@ namespace Configs
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定目录下所有匹配后缀名的文件路径(可递归)
|
||||
/// </summary>
|
||||
/// <param name="directoryPath">目标文件夹路径</param>
|
||||
/// <param name="extensions">后缀名列表(如 ["txt", "jpg"])</param>
|
||||
/// <param name="searchOption">是否包含子文件夹</param>
|
||||
/// <returns>匹配的文件路径列表</returns>
|
||||
public static List<string> GetFilesByExtensions(
|
||||
string directoryPath,
|
||||
string[] extensions,
|
||||
SearchOption searchOption = SearchOption.AllDirectories)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directoryPath))
|
||||
throw new ArgumentException("目录路径不能为空", nameof(directoryPath));
|
||||
|
||||
if (!Directory.Exists(directoryPath))
|
||||
throw new DirectoryNotFoundException($"目录不存在: {directoryPath}");
|
||||
|
||||
if (extensions == null || extensions.Length == 0)
|
||||
throw new ArgumentException("后缀名列表不能为空", nameof(extensions));
|
||||
|
||||
// 标准化后缀名(去掉点,转小写)
|
||||
var normalizedExtensions = new HashSet<string>(
|
||||
extensions.Select(ext => ext.TrimStart('.').ToLower())
|
||||
);
|
||||
|
||||
var result = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(directoryPath, "*", searchOption);
|
||||
foreach (var file in files)
|
||||
{
|
||||
var ext = Path.GetExtension(file).TrimStart('.').ToLower();
|
||||
if (normalizedExtensions.Contains(ext))
|
||||
{
|
||||
result.Add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
// 可选:记录日志或忽略无权限目录
|
||||
Console.WriteLine($"访问被拒绝: {ex.Message}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ namespace Entity
|
||||
|
||||
public AIBase aiTree;
|
||||
public JobBase currentJob;
|
||||
public AttributesDef attributes;
|
||||
public AttributesDef attributes=new();
|
||||
public Vector3 direction;
|
||||
public GameObject body;
|
||||
public string affiliation;
|
||||
@ -42,15 +42,18 @@ namespace Entity
|
||||
private bool _isPlayerControlled = false;
|
||||
private bool _warning = false;
|
||||
|
||||
private Dictionary<Orientation,List<ITick>> bodyNode=new();
|
||||
private Orientation currentOrientation=Orientation.Down;
|
||||
private Dictionary<Orientation,List<ITick>> bodyAnimationNode=new();
|
||||
private Dictionary<Orientation, GameObject> bodyNodes = new();
|
||||
|
||||
private Orientation currentOrientation = Orientation.Down;
|
||||
|
||||
public virtual void Init(PawnDef pawnDef)
|
||||
{
|
||||
attributes = pawnDef.attributes.Clone();
|
||||
aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
affiliation=pawnDef.affiliation;
|
||||
affiliation = pawnDef.affiliation;
|
||||
InitBody(pawnDef.drawingOrder);
|
||||
|
||||
}
|
||||
|
||||
public virtual void InitBody(DrawingOrderDef drawingOrder)
|
||||
@ -60,8 +63,8 @@ namespace Entity
|
||||
|
||||
foreach (var orientation in orientations)
|
||||
{
|
||||
currentOrientation=orientation;
|
||||
bodyNode[orientation]=new();
|
||||
currentOrientation = orientation;
|
||||
bodyAnimationNode[orientation] = new();
|
||||
// 获取当前方向的绘图节点
|
||||
var drawNode = drawingOrder.GetDrawingOrder(orientation);
|
||||
|
||||
@ -70,8 +73,15 @@ namespace Entity
|
||||
directionRoot.transform.SetParent(body.transform, false);
|
||||
|
||||
InitBodyPart(drawNode, directionRoot);
|
||||
bodyNodes[orientation] = directionRoot;
|
||||
}
|
||||
currentOrientation=Orientation.Down;
|
||||
currentOrientation = Orientation.Down;
|
||||
|
||||
foreach (var bodyNode in bodyNodes)
|
||||
{
|
||||
bodyNode.Value.SetActive(false);
|
||||
}
|
||||
SetOrientation(Orientation.Down);
|
||||
}
|
||||
|
||||
// 递归初始化单个绘图节点及其子节点
|
||||
@ -83,19 +93,19 @@ namespace Entity
|
||||
switch (drawNode.drawNodeType)
|
||||
{
|
||||
case DrawNodeType.Image:
|
||||
nodeObject = Instantiate(imagePrefab.gameObject);
|
||||
nodeObject = Instantiate(imagePrefab.gameObject,parent.transform);
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
nodeObject = Instantiate(animatorPrefab.gameObject);
|
||||
nodeObject = Instantiate(animatorPrefab.gameObject,parent.transform);
|
||||
ITick tick = nodeObject.GetComponent<SpriteAnimator>();
|
||||
if (tick != null)
|
||||
bodyNode[currentOrientation].Add(tick);
|
||||
bodyAnimationNode[currentOrientation].Add(tick);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
nodeObject.transform.localPosition = drawNode.position;
|
||||
// 递归初始化子节点
|
||||
foreach (var child in drawNode.children)
|
||||
{
|
||||
@ -113,9 +123,9 @@ namespace Entity
|
||||
AutoBehave();
|
||||
}
|
||||
|
||||
foreach (var bodyPart in bodyNode.Values)
|
||||
if (bodyAnimationNode.TryGetValue(currentOrientation, out var ticks))
|
||||
{
|
||||
foreach (var tick in bodyPart)
|
||||
foreach (var tick in ticks)
|
||||
{
|
||||
tick.Tick();
|
||||
}
|
||||
@ -126,12 +136,19 @@ namespace Entity
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual void SetOrientation(Orientation orientation)
|
||||
{
|
||||
bodyNodes[currentOrientation]?.SetActive(false);
|
||||
currentOrientation = orientation;
|
||||
bodyNodes[orientation]?.SetActive(true);
|
||||
}
|
||||
/// <summary>
|
||||
/// 往对应朝向移动moveSpeed*deltaTime的距离
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
transform.position+=direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
transform.position += direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
}
|
||||
|
||||
public virtual void OnHit(Entity from)
|
||||
@ -156,6 +173,8 @@ namespace Entity
|
||||
|
||||
private void AutoBehave()
|
||||
{
|
||||
if(aiTree == null)
|
||||
return;
|
||||
if (currentJob == null || !currentJob.Running)
|
||||
{
|
||||
currentJob = aiTree.GetJob(this);
|
||||
|
@ -14,9 +14,11 @@ namespace Managers
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
{
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
|
||||
|
||||
//类别,定义名,定义
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
//包id,包
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
//类别,定义
|
||||
public Dictionary<string, List<Define>> anonymousDefines = new();
|
||||
/// <summary>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
@ -46,12 +48,14 @@ namespace Managers
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
// 需要链接的定义、需要链接的字段、链接信息
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
|
||||
string currentPackID = string.Empty;
|
||||
|
||||
void ProcessDefine(Define def)
|
||||
{
|
||||
if (def == null || def.isReferene)
|
||||
return;
|
||||
|
||||
def.packID = currentPackID;
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
@ -89,11 +93,12 @@ namespace Managers
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
currentPackID = pack.Value.packID;
|
||||
foreach (var (typeName, defList) in pack.Value.defines)
|
||||
{
|
||||
if (!defines.ContainsKey(typeName))
|
||||
defines[typeName] = new Dictionary<string, Define>();
|
||||
|
||||
|
||||
foreach (var def in defList)
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
@ -12,6 +13,8 @@ namespace Managers
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
@ -57,6 +60,7 @@ namespace Managers
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -64,6 +68,7 @@ namespace Managers
|
||||
if (pawnDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -79,6 +84,7 @@ namespace Managers
|
||||
if (entityComponent == null)
|
||||
{
|
||||
Debug.LogError($"Error: EntityPrefab component not found on the instantiated object: {entity.name}");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -96,12 +102,32 @@ namespace Managers
|
||||
{
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}");
|
||||
GenerateDefaultEntity(pos);
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
const string factionKey = "default";
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
protected override void OnStart()
|
||||
{
|
||||
factionEntities.Clear();
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
||||
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Data;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
@ -8,17 +10,25 @@ namespace Managers
|
||||
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
|
||||
{
|
||||
public Sprite defaultSprite;
|
||||
|
||||
//包名,图片名
|
||||
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
|
||||
//包名,图片名
|
||||
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
|
||||
|
||||
//包名,文件路径,身体部件名
|
||||
public Dictionary<string, Dictionary<string, Dictionary<string, List<Sprite>>>> bodyTexture = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
if (packagesImages.Count > 0)
|
||||
return;
|
||||
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
|
||||
if(defaultSprite)
|
||||
Debug.Log("加载成功");
|
||||
InitImageDef();
|
||||
InitDrawOrder();
|
||||
packagesImages = null;
|
||||
}
|
||||
|
||||
public void InitImageDef()
|
||||
{
|
||||
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
|
||||
foreach (var ima in imageDef)
|
||||
{
|
||||
@ -38,6 +48,44 @@ namespace Managers
|
||||
|
||||
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void InitDrawOrder()
|
||||
{
|
||||
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
|
||||
if (drawOrderDef == null || drawOrderDef.Length == 0)
|
||||
{
|
||||
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 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);
|
||||
InsertBodySprite(drawOrder.packID, drawOrder.texturePath, name, spr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SplitTextureIntoSprites(
|
||||
@ -131,5 +179,35 @@ namespace Managers
|
||||
var fullName = $"{name}_{index}";
|
||||
return GetSprite(packID, fullName);
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入 Sprite 到指定的嵌套字典中。
|
||||
/// </summary>
|
||||
/// <param name="packageName">包名。</param>
|
||||
/// <param name="filePath">文件路径。</param>
|
||||
/// <param name="bodyPartName">身体部件名。</param>
|
||||
/// <param name="sprite">要插入的 Sprite 对象。</param>
|
||||
public void InsertBodySprite(string packageName, string filePath, string bodyPartName, Sprite sprite)
|
||||
{
|
||||
// 确保第一层字典(包名)存在
|
||||
if (!bodyTexture.ContainsKey(packageName))
|
||||
{
|
||||
bodyTexture[packageName] = new Dictionary<string, Dictionary<string, List<Sprite>>>();
|
||||
}
|
||||
|
||||
// 确保第二层字典(文件路径)存在
|
||||
if (!bodyTexture[packageName].ContainsKey(filePath))
|
||||
{
|
||||
bodyTexture[packageName][filePath] = new Dictionary<string, List<Sprite>>();
|
||||
}
|
||||
|
||||
// 确保第三层字典(身体部件名)存在
|
||||
if (!bodyTexture[packageName][filePath].ContainsKey(bodyPartName))
|
||||
{
|
||||
bodyTexture[packageName][filePath][bodyPartName] = new List<Sprite>();
|
||||
}
|
||||
|
||||
// 将 Sprite 添加到列表中
|
||||
bodyTexture[packageName][filePath][bodyPartName].Add(sprite);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,142 +3,42 @@ using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class ImagePrefab : MonoBehaviour
|
||||
{
|
||||
[Header("Display Settings")] [SerializeField]
|
||||
private Sprite _defaultSprite;
|
||||
public Sprite defaultSprite;
|
||||
|
||||
[SerializeField] private Color _tintColor = Color.white;
|
||||
[SerializeField, Range(0, 1)] private float _alpha = 1f;
|
||||
[SerializeField] private bool _preserveAspect = true;
|
||||
|
||||
private SpriteRenderer _renderer;
|
||||
private Vector2 _originalSize;
|
||||
private bool _isInitialized;
|
||||
|
||||
public Sprite DisplayedSprite => _renderer.sprite;
|
||||
public Color CurrentColor => _renderer.color;
|
||||
private SpriteRenderer spriteRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeRenderer();
|
||||
}
|
||||
|
||||
private void InitializeRenderer()
|
||||
{
|
||||
if (_isInitialized) return;
|
||||
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
|
||||
// 初始尺寸记录
|
||||
if (_defaultSprite != null)
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
if (spriteRenderer == null)
|
||||
{
|
||||
_originalSize = new Vector2(
|
||||
_defaultSprite.rect.width / _defaultSprite.pixelsPerUnit,
|
||||
_defaultSprite.rect.height / _defaultSprite.pixelsPerUnit
|
||||
);
|
||||
Debug.LogError("SpriteRenderer组件未找到,请确保预制体包含该组件!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置初始状态
|
||||
ApplyVisualSettings();
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
private void ApplyVisualSettings()
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
|
||||
// 设置精灵和颜色
|
||||
_renderer.sprite = _defaultSprite;
|
||||
_renderer.color = new Color(_tintColor.r, _tintColor.g, _tintColor.b, _alpha);
|
||||
|
||||
// 保持原始纵横比
|
||||
if (_preserveAspect && _defaultSprite != null)
|
||||
if (defaultSprite != null)
|
||||
{
|
||||
MaintainAspectRatio();
|
||||
spriteRenderer.sprite = defaultSprite;
|
||||
}
|
||||
}
|
||||
|
||||
private void MaintainAspectRatio()
|
||||
{
|
||||
Vector2 currentSize = transform.lossyScale;
|
||||
float aspectRatio = _originalSize.x / _originalSize.y;
|
||||
|
||||
if (currentSize.x == 0 || currentSize.y == 0) return;
|
||||
|
||||
float currentAspect = currentSize.x / currentSize.y;
|
||||
|
||||
if (Mathf.Abs(currentAspect - aspectRatio) > 0.01f)
|
||||
{
|
||||
Vector2 newScale = currentSize;
|
||||
if (currentAspect > aspectRatio)
|
||||
{
|
||||
newScale.x = currentSize.y * aspectRatio;
|
||||
}
|
||||
else
|
||||
{
|
||||
newScale.y = currentSize.x / aspectRatio;
|
||||
}
|
||||
|
||||
transform.localScale = newScale;
|
||||
}
|
||||
}
|
||||
|
||||
// 公共控制方法
|
||||
public void SetSprite(Sprite newSprite)
|
||||
{
|
||||
_defaultSprite = newSprite;
|
||||
if (_defaultSprite != null)
|
||||
if (spriteRenderer != null && newSprite != null)
|
||||
{
|
||||
_originalSize = new Vector2(
|
||||
_defaultSprite.rect.width / _defaultSprite.pixelsPerUnit,
|
||||
_defaultSprite.rect.height / _defaultSprite.pixelsPerUnit
|
||||
);
|
||||
spriteRenderer.sprite = newSprite;
|
||||
}
|
||||
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void SetColor(Color newColor)
|
||||
{
|
||||
_tintColor = newColor;
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void SetAlpha(float newAlpha)
|
||||
{
|
||||
_alpha = Mathf.Clamp01(newAlpha);
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void ToggleVisibility(bool isVisible)
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
_renderer.enabled = isVisible;
|
||||
}
|
||||
|
||||
public void SetSortingLayer(string layerName, int order)
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
_renderer.sortingLayerName = layerName;
|
||||
_renderer.sortingOrder = order;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
// 编辑器模式下实时预览变化
|
||||
if (UnityEditor.EditorApplication.isPlaying)
|
||||
if (spriteRenderer != null)
|
||||
{
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
else if (GetComponent<SpriteRenderer>() != null)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().sprite = _defaultSprite;
|
||||
GetComponent<SpriteRenderer>().color = new Color(_tintColor.r, _tintColor.g, _tintColor.b, _alpha);
|
||||
spriteRenderer.color = newColor;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
@ -13,12 +13,14 @@ namespace Test
|
||||
void Awake()
|
||||
{
|
||||
Managers.DefineManager.Instance.Init();
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var chicken = Managers.DefineManager.Instance.FindDefine<CharacterDef>("testPawn");
|
||||
entityManager.GenerateEntity(chicken,Vector3.zero);
|
||||
entityManager.GenerateDefaultEntity(Vector3.down);
|
||||
Debug.Log(chicken);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user