Files
aibis-dream/Assets/Editor/Window/AnimationClipGenerator.cs
T
2025-12-22 21:10:37 +08:00

133 lines
4.9 KiB
C#

using System.IO;
using UnityEditor;
using UnityEngine;
namespace AibisDream.SystemEditor
{
public class AnimationClipGenerator : EditorWindow
{
private Texture2D selectedTexture;
private TextAsset selectedJson;
private DefaultAsset outputFolder;
private string outputPath = "";
// 动画类型选项
private enum AnimationTargetType
{
SpriteRenderer, // Sprite动画
Image // UIImage动画
}
private AnimationTargetType animationType = AnimationTargetType.SpriteRenderer;
[MenuItem("Tools/Animation Clip Generator")]
public static void ShowWindow()
{
GetWindow<AnimationClipGenerator>("Animation Clip Generator");
}
private void OnGUI()
{
GUILayout.Label("Animation Clip Generator", EditorStyles.boldLabel);
EditorGUILayout.Space();
// 图片文件选择
EditorGUILayout.LabelField("图片文件 (PNG/JPG)", EditorStyles.boldLabel);
selectedTexture = (Texture2D)EditorGUILayout.ObjectField(
"Texture", selectedTexture, typeof(Texture2D), false);
EditorGUILayout.Space();
// JSON文件选择
EditorGUILayout.LabelField("JSON文件", EditorStyles.boldLabel);
selectedJson = (TextAsset)EditorGUILayout.ObjectField(
"JSON", selectedJson, typeof(TextAsset), false);
EditorGUILayout.Space();
// 动画类型选择
EditorGUILayout.LabelField("动画类型", EditorStyles.boldLabel);
animationType = (AnimationTargetType)EditorGUILayout.EnumPopup(
"Animation Type", animationType);
EditorGUILayout.HelpBox(
animationType == AnimationTargetType.SpriteRenderer
? "生成用于SpriteRenderer的动画(绑定到m_Sprite属性)"
: "生成用于Unity UI Image的动画(绑定到m_Sprite属性)",
MessageType.Info);
EditorGUILayout.Space();
// 输出目录选择
EditorGUILayout.LabelField("输出目录", EditorStyles.boldLabel);
outputFolder = (DefaultAsset)EditorGUILayout.ObjectField(
"Output Folder", outputFolder, typeof(DefaultAsset), false);
// 显示输出路径
if (outputFolder != null)
{
outputPath = AssetDatabase.GetAssetPath(outputFolder);
EditorGUILayout.HelpBox($"输出路径: {outputPath}", MessageType.Info);
}
else
{
outputPath = "";
}
EditorGUILayout.Space();
// 生成按钮
GUI.enabled = selectedTexture != null && selectedJson != null && outputFolder != null;
if (GUILayout.Button("生成 AnimationClip", GUILayout.Height(30)))
{
GenerateAnimationClips();
}
GUI.enabled = true;
EditorGUILayout.Space();
// 说明信息
EditorGUILayout.HelpBox(
"使用说明:\n" +
"1. 选择PNG或JPG图片文件\n" +
"2. 选择JSON文件(Aseprite格式或手动格式)\n" +
"3. 选择输出目录\n" +
"4. 点击生成按钮\n\n" +
"注意: 如果图片的SpriteMode是Single,工具会自动根据JSON切图。",
MessageType.Info);
}
private void GenerateAnimationClips()
{
if (selectedTexture == null || selectedJson == null || outputFolder == null)
{
EditorUtility.DisplayDialog("错误", "请选择图片文件、JSON文件和输出目录", "确定");
return;
}
string texturePath = AssetDatabase.GetAssetPath(selectedTexture);
string jsonPath = AssetDatabase.GetAssetPath(selectedJson);
// 验证文件扩展名
string textureExt = Path.GetExtension(texturePath).ToLower();
if (textureExt != ".png" && textureExt != ".jpg" && textureExt != ".jpeg")
{
EditorUtility.DisplayDialog("错误", "图片文件必须是PNG或JPG格式", "确定");
return;
}
try
{
bool isImageAnimation = animationType == AnimationTargetType.Image;
AnimationClipGeneratorCore.GenerateClips(texturePath, jsonPath, outputPath, isImageAnimation);
EditorUtility.DisplayDialog("成功", $"AnimationClip已生成到: {outputPath}", "确定");
// 刷新资源数据库
AssetDatabase.Refresh();
}
catch (System.Exception e)
{
EditorUtility.DisplayDialog("错误", $"生成失败: {e.Message}", "确定");
Debug.LogError(e);
}
}
}
}