fix(editor): 修复 Aseprite JSON 尾随逗号解析问题
- 移除 AsepriteMeta 中未使用的 layers/slices 字段 - 新增 SanitizeJsonForUnity 正则清理尾随逗号 - 修复 Aseprite 导出 JSON 偶发尾随逗号导致 JsonUtility 解析失败
This commit is contained in:
@@ -74,8 +74,6 @@ namespace AibisDream.SystemEditor
|
||||
public AsepriteSize size;
|
||||
public string scale;
|
||||
public List<AsepriteFrameTag> frameTags;
|
||||
public List<object> layers;
|
||||
public List<object> slices;
|
||||
}
|
||||
|
||||
// Aseprite JSON的包装类(因为Unity JsonUtility不支持Dictionary)
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEditor;
|
||||
using UnityEditor.U2D.Sprites;
|
||||
using UnityEngine;
|
||||
@@ -57,8 +58,7 @@ namespace AibisDream.SystemEditor
|
||||
int metaBraceEnd = FindMatchingBrace(jsonContent, metaBraceStart);
|
||||
string metaJson = jsonContent.Substring(metaBraceStart, metaBraceEnd - metaBraceStart + 1);
|
||||
|
||||
// 移除可能的尾随逗号
|
||||
metaJson = metaJson.TrimEnd(',', ' ', '\n', '\r');
|
||||
metaJson = SanitizeJsonForUnity(metaJson);
|
||||
|
||||
AsepriteMeta meta = JsonUtility.FromJson<AsepriteMeta>(metaJson);
|
||||
|
||||
@@ -173,7 +173,7 @@ namespace AibisDream.SystemEditor
|
||||
|
||||
try
|
||||
{
|
||||
AsepriteFrameInfo frameInfo = JsonUtility.FromJson<AsepriteFrameInfo>(frameDataJson);
|
||||
AsepriteFrameInfo frameInfo = JsonUtility.FromJson<AsepriteFrameInfo>(SanitizeJsonForUnity(frameDataJson));
|
||||
if (frameInfo.frame != null)
|
||||
{
|
||||
frames.Add(new FrameData
|
||||
@@ -215,6 +215,14 @@ namespace AibisDream.SystemEditor
|
||||
return int.MaxValue; // 如果无法解析,放在最后
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity JsonUtility 不支持尾随逗号,Aseprite 导出时偶尔会带上。
|
||||
/// </summary>
|
||||
private static string SanitizeJsonForUnity(string json)
|
||||
{
|
||||
return Regex.Replace(json, @",(\s*[}\]])", "$1");
|
||||
}
|
||||
|
||||
private static int FindMatchingBrace(string str, int startIndex)
|
||||
{
|
||||
if (startIndex < 0 || startIndex >= str.Length) return -1;
|
||||
|
||||
Reference in New Issue
Block a user