From 179123f66095b8c4ab2787a2d1fa5dbc6644d75b Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Wed, 23 Jul 2025 22:54:00 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20feat:=E5=9B=BE=E5=83=8F=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=A4=B1=E8=B4=A5=E6=97=B6=E5=8A=A0=E8=BD=BD=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=9B=BE=E5=83=8F=EF=BC=8C=E7=94=9F=E6=88=90=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E8=BA=AB=E4=BD=93=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{动画测试.controller.meta => Default.meta} | 6 +- .../Default.png => Default/DefaultImage.png} | Bin .../DefaultImage.png.meta} | 18 +-- Client/Assets/Resources/animationTest.anim | 53 ------ .../Assets/Resources/animationTest.anim.meta | 8 - Client/Assets/Resources/动画测试.controller | 72 --------- Client/Assets/Scenes/Test.unity | 116 +++++++------- Client/Assets/Scripts/Data/CharacterDefine.cs | 76 ++++----- Client/Assets/Scripts/Data/PawnDefine.cs | 38 +---- Client/Assets/Scripts/Entity/Entity.cs | 58 ++++++- .../Assets/Scripts/Entity/SpriteAnimator.cs | 151 ++++++++++++++++++ .../Scripts/Entity/SpriteAnimator.cs.meta | 3 + .../Scripts/Managers/PackagesImageManager.cs | 15 +- Client/Data/Core/Define/Map/Map.xml | 2 +- Client/Data/Core/Define/Pawn/Character.xml | 8 +- 15 files changed, 329 insertions(+), 295 deletions(-) rename Client/Assets/Resources/{动画测试.controller.meta => Default.meta} (52%) rename Client/Assets/Resources/{Image/Default.png => Default/DefaultImage.png} (100%) rename Client/Assets/Resources/{Image/Default.png.meta => Default/DefaultImage.png.meta} (92%) delete mode 100644 Client/Assets/Resources/animationTest.anim delete mode 100644 Client/Assets/Resources/animationTest.anim.meta delete mode 100644 Client/Assets/Resources/动画测试.controller create mode 100644 Client/Assets/Scripts/Entity/SpriteAnimator.cs create mode 100644 Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta diff --git a/Client/Assets/Resources/动画测试.controller.meta b/Client/Assets/Resources/Default.meta similarity index 52% rename from Client/Assets/Resources/动画测试.controller.meta rename to Client/Assets/Resources/Default.meta index 4eaf488..c34fae6 100644 --- a/Client/Assets/Resources/动画测试.controller.meta +++ b/Client/Assets/Resources/Default.meta @@ -1,8 +1,8 @@ fileFormatVersion: 2 -guid: 42771044e260c3e48b00e50abb7e01e3 -NativeFormatImporter: +guid: 4bb313b12a5e29448937a854eb2201f1 +folderAsset: yes +DefaultImporter: externalObjects: {} - mainObjectFileID: 9100000 userData: assetBundleName: assetBundleVariant: diff --git a/Client/Assets/Resources/Image/Default.png b/Client/Assets/Resources/Default/DefaultImage.png similarity index 100% rename from Client/Assets/Resources/Image/Default.png rename to Client/Assets/Resources/Default/DefaultImage.png diff --git a/Client/Assets/Resources/Image/Default.png.meta b/Client/Assets/Resources/Default/DefaultImage.png.meta similarity index 92% rename from Client/Assets/Resources/Image/Default.png.meta rename to Client/Assets/Resources/Default/DefaultImage.png.meta index 0c31100..0f54e39 100644 --- a/Client/Assets/Resources/Image/Default.png.meta +++ b/Client/Assets/Resources/Default/DefaultImage.png.meta @@ -6,7 +6,7 @@ TextureImporter: serializedVersion: 13 mipmaps: mipMapMode: 0 - enableMipMap: 1 + enableMipMap: 0 sRGBTexture: 1 linearTexture: 0 fadeOut: 0 @@ -37,24 +37,24 @@ TextureImporter: filterMode: 1 aniso: 1 mipBias: 0 - wrapU: 0 - wrapV: 0 + wrapU: 1 + wrapV: 1 wrapW: 0 - nPOTScale: 1 + nPOTScale: 0 lightmap: 0 compressionQuality: 50 - spriteMode: 0 + spriteMode: 1 spriteExtrude: 1 spriteMeshType: 1 alignment: 0 spritePivot: {x: 0.5, y: 0.5} - spritePixelsToUnits: 100 + spritePixelsToUnits: 32 spriteBorder: {x: 0, y: 0, z: 0, w: 0} spriteGenerateFallbackPhysicsShape: 1 alphaUsage: 1 - alphaIsTransparency: 0 + alphaIsTransparency: 1 spriteTessellationDetail: -1 - textureType: 0 + textureType: 8 textureShape: 1 singleChannelComponent: 0 flipbookRows: 1 @@ -100,7 +100,7 @@ TextureImporter: customData: physicsShape: [] bones: [] - spriteID: + spriteID: 5e97eb03825dee720800000000000000 internalID: 0 vertices: [] indices: diff --git a/Client/Assets/Resources/animationTest.anim b/Client/Assets/Resources/animationTest.anim deleted file mode 100644 index fd75743..0000000 --- a/Client/Assets/Resources/animationTest.anim +++ /dev/null @@ -1,53 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!74 &7400000 -AnimationClip: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_Name: animationTest - serializedVersion: 7 - m_Legacy: 0 - m_Compressed: 0 - m_UseHighQualityCurve: 1 - m_RotationCurves: [] - m_CompressedRotationCurves: [] - m_EulerCurves: [] - m_PositionCurves: [] - m_ScaleCurves: [] - m_FloatCurves: [] - m_PPtrCurves: [] - m_SampleRate: 60 - m_WrapMode: 0 - m_Bounds: - m_Center: {x: 0, y: 0, z: 0} - m_Extent: {x: 0, y: 0, z: 0} - m_ClipBindingConstant: - genericBindings: [] - pptrCurveMapping: [] - m_AnimationClipSettings: - serializedVersion: 2 - m_AdditiveReferencePoseClip: {fileID: 0} - m_AdditiveReferencePoseTime: 0 - m_StartTime: 0 - m_StopTime: 1 - m_OrientationOffsetY: 0 - m_Level: 0 - m_CycleOffset: 0 - m_HasAdditiveReferencePose: 0 - m_LoopTime: 1 - m_LoopBlend: 0 - m_LoopBlendOrientation: 0 - m_LoopBlendPositionY: 0 - m_LoopBlendPositionXZ: 0 - m_KeepOriginalOrientation: 0 - m_KeepOriginalPositionY: 1 - m_KeepOriginalPositionXZ: 0 - m_HeightFromFeet: 0 - m_Mirror: 0 - m_EditorCurves: [] - m_EulerEditorCurves: [] - m_HasGenericRootTransform: 0 - m_HasMotionFloatCurves: 0 - m_Events: [] diff --git a/Client/Assets/Resources/animationTest.anim.meta b/Client/Assets/Resources/animationTest.anim.meta deleted file mode 100644 index 6bf5af9..0000000 --- a/Client/Assets/Resources/animationTest.anim.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8bdfdaa2024b283409a4d6a4095c02c9 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 7400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Client/Assets/Resources/动画测试.controller b/Client/Assets/Resources/动画测试.controller deleted file mode 100644 index 3b9f4f5..0000000 --- a/Client/Assets/Resources/动画测试.controller +++ /dev/null @@ -1,72 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!1102 &-5314867381662587561 -AnimatorState: - serializedVersion: 6 - m_ObjectHideFlags: 1 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_Name: animationTest - m_Speed: 1 - m_CycleOffset: 0 - m_Transitions: [] - m_StateMachineBehaviours: [] - m_Position: {x: 50, y: 50, z: 0} - m_IKOnFeet: 0 - m_WriteDefaultValues: 1 - m_Mirror: 0 - m_SpeedParameterActive: 0 - m_MirrorParameterActive: 0 - m_CycleOffsetParameterActive: 0 - m_TimeParameterActive: 0 - m_Motion: {fileID: 7400000, guid: 8bdfdaa2024b283409a4d6a4095c02c9, type: 2} - m_Tag: - m_SpeedParameter: - m_MirrorParameter: - m_CycleOffsetParameter: - m_TimeParameter: ---- !u!1107 &-991703144119142111 -AnimatorStateMachine: - serializedVersion: 6 - m_ObjectHideFlags: 1 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_Name: Base Layer - m_ChildStates: - - serializedVersion: 1 - m_State: {fileID: -5314867381662587561} - m_Position: {x: 200, y: 0, z: 0} - m_ChildStateMachines: [] - m_AnyStateTransitions: [] - m_EntryTransitions: [] - m_StateMachineTransitions: {} - m_StateMachineBehaviours: [] - m_AnyStatePosition: {x: 50, y: 20, z: 0} - m_EntryPosition: {x: 50, y: 120, z: 0} - m_ExitPosition: {x: 800, y: 120, z: 0} - m_ParentStateMachinePosition: {x: 800, y: 20, z: 0} - m_DefaultState: {fileID: -5314867381662587561} ---- !u!91 &9100000 -AnimatorController: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_Name: "\u52A8\u753B\u6D4B\u8BD5" - serializedVersion: 5 - m_AnimatorParameters: [] - m_AnimatorLayers: - - serializedVersion: 5 - m_Name: Base Layer - m_StateMachine: {fileID: -991703144119142111} - m_Mask: {fileID: 0} - m_Motions: [] - m_Behaviours: [] - m_BlendingMode: 0 - m_SyncedLayerIndex: -1 - m_DefaultWeight: 0 - m_IKPass: 0 - m_SyncedLayerAffectsTiming: 0 - m_Controller: {fileID: 9100000} diff --git a/Client/Assets/Scenes/Test.unity b/Client/Assets/Scenes/Test.unity index 0001e43..ac3b707 100644 --- a/Client/Assets/Scenes/Test.unity +++ b/Client/Assets/Scenes/Test.unity @@ -267,7 +267,7 @@ Transform: m_GameObject: {fileID: 309861628} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.17242, y: 0.05575, z: 0} + m_LocalPosition: {x: 0.17242, y: 0.05575, z: 1} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -333,60 +333,6 @@ Transform: - {fileID: 9462743} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1 &865935899 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 865935901} - - component: {fileID: 865935900} - m_Layer: 0 - m_Name: "\u52A8\u753B\u6D4B\u8BD5" - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!95 &865935900 -Animator: - serializedVersion: 7 - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 865935899} - m_Enabled: 1 - m_Avatar: {fileID: 0} - m_Controller: {fileID: 9100000, guid: 42771044e260c3e48b00e50abb7e01e3, type: 2} - m_CullingMode: 0 - m_UpdateMode: 0 - m_ApplyRootMotion: 0 - m_LinearVelocityBlending: 0 - m_StabilizeFeet: 0 - m_AnimatePhysics: 0 - m_WarningMessage: - m_HasTransformHierarchy: 1 - m_AllowConstantClipSamplingOptimization: 1 - m_KeepAnimatorStateOnDisable: 0 - m_WriteDefaultValuesOnDisable: 0 ---- !u!4 &865935901 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 865935899} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1057087086 GameObject: m_ObjectHideFlags: 0 @@ -692,6 +638,64 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: entityManager: {fileID: 2036983433} +--- !u!1001 &1730922965 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + serializedVersion: 3 + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalPosition.x + value: -7.6344624 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalPosition.y + value: -0.584403 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 887327274103887133, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + propertyPath: m_Name + value: EntityPrefab + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_RemovedGameObjects: + - {fileID: 3562938223049611414, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} + m_AddedGameObjects: [] + m_AddedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3} --- !u!1 &2036983430 GameObject: m_ObjectHideFlags: 0 @@ -807,4 +811,4 @@ SceneRoots: - {fileID: 309861630} - {fileID: 2036983432} - {fileID: 1371068282} - - {fileID: 865935901} + - {fileID: 1730922965} diff --git a/Client/Assets/Scripts/Data/CharacterDefine.cs b/Client/Assets/Scripts/Data/CharacterDefine.cs index d28152e..0f1ef71 100644 --- a/Client/Assets/Scripts/Data/CharacterDefine.cs +++ b/Client/Assets/Scripts/Data/CharacterDefine.cs @@ -28,55 +28,43 @@ namespace Data public class DrawingOrderDef : Define { - public List drawNodes = new(); + public DrawNodeDef drawingOrder_down; + public DrawNodeDef drawingOrder_up; + public DrawNodeDef drawingOrder_left; + public DrawNodeDef drawingOrder_right; public string texturePath; - public override bool Init(XElement xmlDef) + + public DrawNodeDef GetDrawingOrder(Orientation orientation) { - base.Init(xmlDef); + // 定义一个临时变量用于存储结果 + DrawNodeDef result = null; - var nodes = xmlDef.Elements("DrawNodeDef"); - var xElements = nodes as XElement[] ?? nodes.ToArray(); - if (!xElements.Any()) - return false; - foreach (var node in xElements) + // 根据传入的 Orientation 获取对应的 DrawingOrderDef + switch (orientation) { - var drawNode = new DrawNodeDef(); - drawNode.Init(node); - drawNodes.Add(drawNode); - } - texturePath= xmlDef.Element("texturePath")?.Value; - return true;; - } - // 重载 == 运算符 - public static bool operator ==(DrawingOrderDef a, DrawingOrderDef b) - { - if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true - if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false - - return AreEqual(a, b); - } - - // 重载 != 运算符 - public static bool operator !=(DrawingOrderDef a, DrawingOrderDef b) - { - return !(a == b); - } - - // 判断两个 DrawingOrderDef 是否相等 - private static bool AreEqual(DrawingOrderDef a, DrawingOrderDef b) - { - // 比较 drawNodes 的数量 - if (a.drawNodes.Count != b.drawNodes.Count) - return false; - - // 递归比较每个 DrawNodeDef - for (int i = 0; i < a.drawNodes.Count; i++) - { - if (!DrawNodeDef.AreEqual(a.drawNodes[i], b.drawNodes[i])) - return false; + case Orientation.Down: + result = drawingOrder_down; + break; + case Orientation.Up: + result = drawingOrder_up; + break; + case Orientation.Left: + result = drawingOrder_left; + break; + case Orientation.Right: + result = drawingOrder_right; + break; + default: + throw new ArgumentException("Invalid orientation value."); } - return true; + // 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充 + if (result == null) result = drawingOrder_down; + + // 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充 + if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right; + + return result; } } @@ -92,7 +80,7 @@ namespace Data { base.Init(xmlDef); - nodeName = xmlDef.Attribute("name")?.Value; + nodeName = xmlDef.Attribute("name")?.Value??"noName"; drawNodeType = Enum.TryParse(xmlDef.Attribute("type")?.Value, true, out DrawNodeType typeResult) ? typeResult : DrawNodeType.Image; diff --git a/Client/Assets/Scripts/Data/PawnDefine.cs b/Client/Assets/Scripts/Data/PawnDefine.cs index 8d6729b..79afdc9 100644 --- a/Client/Assets/Scripts/Data/PawnDefine.cs +++ b/Client/Assets/Scripts/Data/PawnDefine.cs @@ -8,48 +8,12 @@ namespace Data public class PawnDef : Define { public AttributesDef attributes; - public DrawingOrderDef - drawingOrder_down, - drawingOrder_up, - drawingOrder_left, - drawingOrder_right; + public DrawingOrderDef drawingOrder; public BehaviorTreeDef behaviorTree; public string affiliation; - - public DrawingOrderDef GetDrawingOrder(Orientation orientation) - { - // 定义一个临时变量用于存储结果 - DrawingOrderDef result = null; - // 根据传入的 Orientation 获取对应的 DrawingOrderDef - switch (orientation) - { - case Orientation.Down: - result = drawingOrder_down; - break; - case Orientation.Up: - result = drawingOrder_up; - break; - case Orientation.Left: - result = drawingOrder_left; - break; - case Orientation.Right: - result = drawingOrder_right; - break; - default: - throw new ArgumentException("Invalid orientation value."); - } - - // 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充 - if (result == null) result = drawingOrder_down; - - // 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充 - if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right; - - return result; - } } public class MonsterDef:PawnDef { diff --git a/Client/Assets/Scripts/Entity/Entity.cs b/Client/Assets/Scripts/Entity/Entity.cs index 46e04cd..6a488fc 100644 --- a/Client/Assets/Scripts/Entity/Entity.cs +++ b/Client/Assets/Scripts/Entity/Entity.cs @@ -44,9 +44,61 @@ namespace Entity attributes = pawnDef.attributes.Clone(); aiTree = ConvertToAIBase(pawnDef.behaviorTree); affiliation=pawnDef.affiliation; - //定义的tag有限,不用这个了 - // if(!string.IsNullOrEmpty(affiliation)) - // gameObject.CompareTag(affiliation); + InitBody(pawnDef.drawingOrder); + } + + public virtual void InitBody(DrawingOrderDef drawingOrder) + { + // 定义方向枚举和对应的 GetDrawingOrder 调用 + Orientation[] orientations = { Orientation.Down, Orientation.Up, Orientation.Left, Orientation.Right }; + + foreach (var orientation in orientations) + { + // 获取当前方向的绘图节点 + var drawNode = drawingOrder.GetDrawingOrder(orientation); + + if (drawNode == null) continue; + var directionRoot = new GameObject(orientation.ToString()); + directionRoot.transform.SetParent(body.transform, false); + + InitBodyPart(drawNode, directionRoot); + } + } + + // 递归初始化单个绘图节点及其子节点 + public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent) + { + if(drawNode==null) return; + // 创建新的 GameObject 表示当前节点 + var nodeObject = new GameObject(drawNode.nodeName); + + // 设置节点的父对象 + nodeObject.transform.SetParent(parent.transform, false); + + // 设置节点的位置 + nodeObject.transform.localPosition = new Vector3(drawNode.position.x, drawNode.position.y, 0); + + // 根据节点类型设置其他特性(例如动画或图片) + switch (drawNode.drawNodeType) + { + case DrawNodeType.Image: + var spriteRenderer = nodeObject.AddComponent(); + spriteRenderer.color = Color.white; // 默认颜色 + break; + + case DrawNodeType.Animation: + var animator = nodeObject.AddComponent(); + animator.runtimeAnimatorController = null; // 需要手动设置动画控制器 + break; + default: + throw new ArgumentOutOfRangeException(); + } + + // 递归初始化子节点 + foreach (var child in drawNode.children) + { + InitBodyPart(child, nodeObject); + } } public void Tick() { diff --git a/Client/Assets/Scripts/Entity/SpriteAnimator.cs b/Client/Assets/Scripts/Entity/SpriteAnimator.cs new file mode 100644 index 0000000..44873dc --- /dev/null +++ b/Client/Assets/Scripts/Entity/SpriteAnimator.cs @@ -0,0 +1,151 @@ +using UnityEngine; + +namespace Entity +{ +public class SpriteAnimator : MonoBehaviour +{ + // 引用 SpriteRenderer 组件 + [SerializeField] private SpriteRenderer spriteRenderer; + + // 精灵列表 + [SerializeField] private Sprite[] sprites; + + // 动画帧率 (Frames Per Second) + [SerializeField] private float fps = 10f; + + // 是否暂停动画 + [SerializeField] private bool isPaused = false; + + // 暂停时显示的静态精灵索引 (-1 表示不显示静态精灵) + [SerializeField] private int staticSpriteIndex = -1; + + // 当前帧索引 + private int currentFrameIndex = 0; + + // 帧间隔时间 + private float frameInterval; + + // 计时器 + private float timer; + + private void Start() + { + // 初始化帧间隔时间 + frameInterval = 1f / fps; + + // 如果指定了静态精灵索引,则直接显示静态精灵 + if (staticSpriteIndex >= 0 && staticSpriteIndex < sprites.Length) + { + ShowStaticSprite(staticSpriteIndex); + } + else + { + // 否则从第一个精灵开始 + UpdateSprite(); + } + } + + private void Update() + { + // 如果暂停并且没有设置静态精灵,则不更新 + if (isPaused && staticSpriteIndex == -1) return; + + // 如果暂停并设置了静态精灵,则直接显示静态精灵 + if (isPaused && staticSpriteIndex >= 0) + { + ShowStaticSprite(staticSpriteIndex); + return; + } + + // 更新计时器 + timer += Time.deltaTime; + + // 如果达到下一帧的时间间隔 + if (timer >= frameInterval) + { + timer -= frameInterval; // 重置计时器 + UpdateSprite(); // 更新精灵 + } + } + + /// + /// 更新当前显示的精灵 + /// + private void UpdateSprite() + { + if (sprites.Length == 0) return; // 如果没有精灵,则退出 + + // 设置当前帧的精灵 + spriteRenderer.sprite = sprites[currentFrameIndex]; + + // 循环播放:更新到下一帧 + currentFrameIndex = (currentFrameIndex + 1) % sprites.Length; + } + + /// + /// 显示静态精灵 + /// + /// 静态精灵的索引 + private void ShowStaticSprite(int index) + { + if (index < 0 || index >= sprites.Length) + { + Debug.LogWarning("静态精灵索引超出范围!"); + return; + } + + // 显示指定的静态精灵 + spriteRenderer.sprite = sprites[index]; + } + + /// + /// 开始播放动画 + /// + public void PlayAnimation() + { + isPaused = false; // 取消暂停 + staticSpriteIndex = -1; // 清除静态精灵索引 + } + + /// + /// 暂停动画并显示静态精灵 + /// + /// 静态精灵的索引 + public void PauseAnimationWithStaticSprite(int index) + { + if (index < 0 || index >= sprites.Length) + { + Debug.LogWarning("静态精灵索引超出范围!"); + return; + } + + isPaused = true; // 暂停动画 + staticSpriteIndex = index; // 设置静态精灵索引 + } + + /// + /// 暂停动画但不显示静态精灵 + /// + public void PauseAnimation() + { + isPaused = true; // 暂停动画 + staticSpriteIndex = -1; // 不显示静态精灵 + } + + /// + /// 设置帧率 + /// + /// 新的帧率 + public void SetFPS(float newFps) + { + if (newFps <= 0) + { + Debug.LogWarning("帧率必须大于 0!"); + return; + } + + fps = newFps; + frameInterval = 1f / fps; // 更新帧间隔时间 + } +} +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta b/Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta new file mode 100644 index 0000000..de74368 --- /dev/null +++ b/Client/Assets/Scripts/Entity/SpriteAnimator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 713a1742950a45d8b4be258a82321e45 +timeCreated: 1753282330 \ No newline at end of file diff --git a/Client/Assets/Scripts/Managers/PackagesImageManager.cs b/Client/Assets/Scripts/Managers/PackagesImageManager.cs index 750743f..df4a20d 100644 --- a/Client/Assets/Scripts/Managers/PackagesImageManager.cs +++ b/Client/Assets/Scripts/Managers/PackagesImageManager.cs @@ -7,6 +7,8 @@ namespace Managers { public class PackagesImageManager : Utils.Singleton { + public Sprite defaultSprite; + public Dictionary> packagesImages = new(); public Dictionary> sprites = new(); @@ -14,7 +16,9 @@ namespace Managers { if (packagesImages.Count > 0) return; - + defaultSprite = Resources.Load("Default/DefaultImage"); + if(defaultSprite) + Debug.Log("加载成功"); var imageDef = Managers.DefineManager.Instance.QueryDefinesByType(); foreach (var ima in imageDef) { @@ -113,14 +117,13 @@ namespace Managers return sprite; } } - else + else if (sprites.TryGetValue(packID, out var dict)) { - if (!sprites.TryGetValue(packID, out var dict)) return null; - dict.TryGetValue(name, out var sprite); - return sprite; + if (dict.TryGetValue(name, out var sprite)) + return sprite; } - return null; + return defaultSprite; } public Sprite GetSprite(string packID, string name, int index) diff --git a/Client/Data/Core/Define/Map/Map.xml b/Client/Data/Core/Define/Map/Map.xml index 48aa780..cccc224 100644 --- a/Client/Data/Core/Define/Map/Map.xml +++ b/Client/Data/Core/Define/Map/Map.xml @@ -22,7 +22,7 @@ - + diff --git a/Client/Data/Core/Define/Pawn/Character.xml b/Client/Data/Core/Define/Pawn/Character.xml index 690ba6a..4e20f6b 100644 --- a/Client/Data/Core/Define/Pawn/Character.xml +++ b/Client/Data/Core/Define/Pawn/Character.xml @@ -13,10 +13,12 @@ 用于测试功能的角色 TestCharacter player - + Resources/Character/CatGirl - - + + + +