|
|
@@ -7,6 +7,7 @@ using System.Linq;
|
|
|
using System.IO;
|
|
|
using System;
|
|
|
using Spine;
|
|
|
+using UnityEditor.Animations;
|
|
|
|
|
|
[InitializeOnLoad]
|
|
|
public class SkeletonDataMonitor
|
|
|
@@ -298,5 +299,71 @@ public class SkeletonDataMonitor
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ [MenuItem("Tools/Spine/Generate Simple Animator")]
|
|
|
+ public static void GenerateSimpleAnimator()
|
|
|
+ {
|
|
|
+ SkeletonDataAsset skeletonData = Selection.activeObject as SkeletonDataAsset;
|
|
|
+ if (skeletonData == null)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("请先选择一个SkeletonDataAsset");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 创建Animator Controller
|
|
|
+ string skeletonFolder = Path.GetDirectoryName(AssetDatabase.GetAssetPath(skeletonData));
|
|
|
+ string controllerName = $"{skeletonData.name}_Controller.controller";
|
|
|
+ string path = $"{skeletonFolder}/{controllerName}";
|
|
|
+ if (File.Exists(path))
|
|
|
+ {
|
|
|
+ AssetDatabase.DeleteAsset(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ var controller = AnimatorController.CreateAnimatorControllerAtPath(path);
|
|
|
+
|
|
|
+ // 2. 获取Spine动画数据
|
|
|
+ var skeleton = skeletonData.GetSkeletonData(true);
|
|
|
+
|
|
|
+ // 3. 为每个动画创建空Clip并设置正确时长
|
|
|
+ foreach (var spineAnim in skeleton.Animations)
|
|
|
+ {
|
|
|
+ AnimationClip clip = new AnimationClip();
|
|
|
+ clip.name = spineAnim.Name;
|
|
|
+
|
|
|
+ // 设置Clip时长与Spine动画一致
|
|
|
+ SetClipLength(clip, spineAnim.Duration);
|
|
|
+
|
|
|
+ // 添加到Controller
|
|
|
+ AssetDatabase.AddObjectToAsset(clip, controller);
|
|
|
+ var state = controller.layers[0].stateMachine.AddState(clip.name);
|
|
|
+ state.motion = clip;
|
|
|
+ }
|
|
|
+
|
|
|
+ AssetDatabase.SaveAssets();
|
|
|
+ Debug.Log($"已生成包含 {skeleton.Animations.Count} 个动画的控制器");
|
|
|
+ }
|
|
|
+
|
|
|
+ static void SetClipLength(AnimationClip clip, float duration)
|
|
|
+ {
|
|
|
+ // 创建一个空曲线来设置时长
|
|
|
+ AnimationCurve curve = new AnimationCurve();
|
|
|
+ curve.AddKey(0, 0);
|
|
|
+ curve.AddKey(duration, 0);
|
|
|
+
|
|
|
+ // 绑定到任意属性(这里使用假属性)
|
|
|
+ AnimationUtility.SetEditorCurves(clip, new[] {
|
|
|
+ new EditorCurveBinding {
|
|
|
+ path = "",
|
|
|
+ type = typeof(GameObject),
|
|
|
+ propertyName = "m_IsActive"
|
|
|
+ }
|
|
|
+ }, new[] { curve });
|
|
|
+ }
|
|
|
+
|
|
|
+ [MenuItem("Tools/Spine/Generate Simple Animator", true)]
|
|
|
+ static bool ValidateGenerate()
|
|
|
+ {
|
|
|
+ return Selection.activeObject is SkeletonDataAsset;
|
|
|
+ }
|
|
|
}
|
|
|
#endif
|