Browse Source

Json/SkeletonData两种读取信息的脚本

SZAND\msx_2 7 months ago
parent
commit
69802f8a69

+ 15 - 2
ActionTowerDefense/Assets/Resources/Prefab/Enemy/Enemy_11002.prefab

@@ -254,8 +254,8 @@ MonoBehaviour:
   canHitFly: 1
   isNonAttack: 0
   attackTime: 0
-  totalAttack_summonTime: 0.5
-  totalAttack_marchTime: 0.5
+  totalAttack_summonTime: 2
+  totalAttack_marchTime: 2
   attackType: 0
   bulletPrefab: {fileID: 0}
   shootPos: []
@@ -951,6 +951,7 @@ GameObject:
   - component: {fileID: 2459340336965010942}
   - component: {fileID: 6496896669866891404}
   - component: {fileID: 5617185463097218335}
+  - component: {fileID: 8880658652968145485}
   m_Layer: 8
   m_Name: Spine
   m_TagString: Untagged
@@ -1087,6 +1088,18 @@ MonoBehaviour:
     layerMixModes: 01000000
     layerBlendModes: 01000000
   updateTiming: 1
+--- !u!114 &8880658652968145485
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 7669620205589286043}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 376b76302b3c26b49b3431c17bfeee6f, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
 --- !u!1 &7970625495845499240
 GameObject:
   m_ObjectHideFlags: 0

+ 8 - 0
ActionTowerDefense/Assets/Scripts/Spine.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: f05a0c2560d0a2a41a28e090f59718d1
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 65 - 0
ActionTowerDefense/Assets/Scripts/Spine/SkeletonDataReader.cs

@@ -0,0 +1,65 @@
+using UnityEngine;
+using Spine;
+using Spine.Unity;
+
+public class SkeletonDataReader : MonoBehaviour
+{
+    private SkeletonMecanim skeletonMecanim;
+    private Enemy ene;
+
+    [ContextMenu("Play")]
+    private void WriteData()
+    {
+        ReadData();
+        ene = GetComponentInParent<Enemy>();
+        ene.totalBeRepelValue = 30;
+    }
+
+    private void ReadData()
+    {
+        skeletonMecanim = GetComponent<SkeletonMecanim>();
+        if (skeletonMecanim == null)
+        {
+            Debug.LogError("SkeletonMecanim component not found!");
+            return;
+        }
+
+        // 获取 SkeletonDataAsset
+        SkeletonDataAsset skeletonDataAsset = skeletonMecanim.SkeletonDataAsset;
+        if (skeletonDataAsset == null)
+        {
+            Debug.LogError("SkeletonDataAsset not found!");
+            return;
+        }
+
+        // 获取 SkeletonData
+        SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(true);
+        if (skeletonData == null)
+        {
+            Debug.LogError("SkeletonData not found!");
+            return;
+        }
+
+        // 遍历所有动画
+        foreach (var animation in skeletonData.Animations)
+        {
+            string animationName = animation.Name;
+            float animationDuration = animation.Duration;
+            Debug.Log($"动画名称: {animationName}, 动画时长: {animationDuration}");
+
+            // 遍历动画中的事件
+            foreach (var timeline in animation.Timelines)
+            {
+                if (timeline is EventTimeline eventTimeline)
+                {
+                    foreach (var eventFrame in eventTimeline.Events)
+                    {
+                        string eventName = eventFrame.Name;
+                        float eventTime = eventFrame.Time;
+                        Debug.Log($"事件名称: {eventName}, 事件时间: {eventTime}, 所在动画: {animationName}");
+                    }
+                }
+            }
+        }
+    }
+}

+ 11 - 0
ActionTowerDefense/Assets/Scripts/Spine/SkeletonDataReader.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 376b76302b3c26b49b3431c17bfeee6f
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 82 - 0
ActionTowerDefense/Assets/Scripts/Spine/SpineJsonData.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+
+[Serializable]
+public class SpineAnimationData
+{
+    public JSkeleton skeleton;
+    public List<JBone> bones;
+    public List<JSlot> slots;
+    public List<JSkin> skins;
+    public Dictionary<string, object> events; // 事件定义
+    public Dictionary<string, JAnimation> animations; // 动画数据
+}
+
+[Serializable]
+public class JSkeleton
+{
+    public string hash;
+    public string spine;
+    public float x;
+    public float y;
+    public float width;
+    public float height;
+    public string images;
+    public string audio;
+}
+
+[Serializable]
+public class JBone
+{
+    public string name;
+    public string parent;
+    public string icon;
+    public float length;
+    public float x;
+    public float y;
+    public float scaleX;
+    public float scaleY;
+}
+
+[Serializable]
+public class JSlot
+{
+    public string name;
+    public string bone;
+    public string attachment;
+}
+
+[Serializable]
+public class JSkin
+{
+    public string name;
+    public Dictionary<string, Dictionary<string, JAttachment>> attachments;
+}
+
+[Serializable]
+public class JAttachment
+{
+    public float x;
+    public float y;
+    public float scaleX;
+    public float scaleY;
+    public float rotation;
+    public float width;
+    public float height;
+}
+
+[Serializable]
+public class JAnimation
+{
+    public Dictionary<string, object> slots; // 插槽数据
+    public Dictionary<string, object> bones; // 骨骼数据
+    public List<JAnimationEvent> events; // 事件数据
+    public float duration; // 动画的总时长
+}
+
+[Serializable]
+public class JAnimationEvent
+{
+    public float time; // 事件发生的时间
+    public string name; // 事件名称
+}

+ 11 - 0
ActionTowerDefense/Assets/Scripts/Spine/SpineJsonData.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 2b83ffe41d642a94d91add62a0795978
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 48 - 0
ActionTowerDefense/Assets/Scripts/Spine/SpineJsonReader.cs

@@ -0,0 +1,48 @@
+using UnityEngine;
+using System.IO;
+using Newtonsoft.Json;
+
+public class SpineJsonReader : MonoBehaviour
+{
+    private void Start()
+    {
+        // 读取 JSON 文件
+        string filePath = "Assets/Resources/Spine/Enemy_FireDaoshi/Enemy_FireDaoshi.json";
+        if (File.Exists(filePath))
+        {
+            string jsonContent = File.ReadAllText(filePath);
+
+            // 解析 JSON 数据
+            SpineAnimationData animationData = JsonConvert.DeserializeObject<SpineAnimationData>(jsonContent);
+
+            // 提取事件时间信息
+            ExtractEventTimes(animationData);
+        }
+        else
+        {
+            Debug.LogError("JSON file not found!");
+        }
+    }
+
+    private void ExtractEventTimes(SpineAnimationData animationData)
+    {
+        if (animationData.animations != null)
+        {
+            foreach (var animationEntry in animationData.animations)
+            {
+                string animationName = animationEntry.Key;
+                JAnimation animation = animationEntry.Value;
+
+                Debug.Log($"JAnimation: {animationName}");
+
+                if (animation.events != null)
+                {
+                    foreach (var evt in animation.events)
+                    {
+                        Debug.Log($"Event '{evt.name}' at time: {evt.time}");
+                    }
+                }
+            }
+        }
+    }
+}

+ 11 - 0
ActionTowerDefense/Assets/Scripts/Spine/SpineJsonReader.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c5c695aaa2214534c9ed516d10d6a3d0
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 3 - 1
ActionTowerDefense/Assets/Spine/Runtime/spine-csharp/Event.cs

@@ -51,7 +51,9 @@ namespace Spine {
 		public float Volume { get { return volume; } set { volume = value; } }
 		public float Balance { get { return balance; } set { balance = value; } }
 
-		public Event (float time, EventData data) {
+        public string Name { get; set; }
+
+        public Event (float time, EventData data) {
 			if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
 			this.time = time;
 			this.data = data;

+ 4 - 2
ActionTowerDefense/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs

@@ -78,9 +78,11 @@ namespace Spine.Unity {
 
 		[SerializeField] protected UpdateTiming updateTiming = UpdateTiming.InUpdate;
 		public UpdateTiming UpdateTiming { get { return updateTiming; } set { updateTiming = value; } }
-		#endregion
 
-		public override void Initialize (bool overwrite, bool quiet = false) {
+        public object AnimationState { get; set; }
+        #endregion
+
+        public override void Initialize (bool overwrite, bool quiet = false) {
 			if (valid && !overwrite)
 				return;
 #if UNITY_EDITOR