It would be great to have a little bit of meta data included in the JSON exports so I can quickly validate that the JSON file being imported is in fact Spine data. Also maybe the version of Spine it was created with.
example of TexturePacker's meta block:
"meta": {
"app": "http://www.texturepacker.com",
"version": "1.0",
"image": "DudeCell.png",
"format": "RGBA8888",
"size": {"w":512,"h":1024},
"scale": "0.5",
"smartupdate": "$TexturePacker:SmartUpdate:1a3e06660e6f9855aca66037128ecde4$"
}
(note they also have a hash to determine whether anything has changed and needs to be updated)
Can't attach *.cs files apparently... but here ya go.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text;
using Spine;
public class SpineIngestHelper {
const float defaultMix = 0.2f;
const string defaultShader = "Spine/Skeleton";
[MenuItem("Assets/Ingest/Spine Project")]
static void IngestSpineProject(){
TextAsset spineJson = null;
TextAsset atlasText = null;
foreach(UnityEngine.Object o in Selection.objects){
if(o.GetType() != typeof(TextAsset))
continue;
string fileName = Path.GetFileName(AssetDatabase.GetAssetPath(o));
if(fileName.EndsWith(".json"))
spineJson = (TextAsset)o;
else if(fileName.EndsWith(".atlas.txt"))
atlasText = (TextAsset)o;
}
if(spineJson == null){
EditorUtility.DisplayDialog("Error!", "Spine JSON file not found in selection!", "OK");
return;
}
string primaryName = Path.GetFileNameWithoutExtension(spineJson.name);
string assetPath = Path.GetDirectoryName( AssetDatabase.GetAssetPath(spineJson));
if(atlasText == null){
string atlasPath = assetPath + "/" + primaryName + ".atlas.txt";
atlasText = (TextAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(TextAsset));
}
if(spineJson != null && atlasText != null){
AtlasAsset atlasAsset = AtlasAsset.CreateInstance<AtlasAsset>();
atlasAsset.atlasFile = atlasText;
atlasAsset.materials = new Material[1];
string materialPath = assetPath + "/" + primaryName + "_Material.mat";
Material mat = new Material(Shader.Find(defaultShader));
string[] atlasLines = atlasText.text.Split(new char[2]{'\r', '\n'}, System.StringSplitOptions.RemoveEmptyEntries);
string texturePath = assetPath + "/" + atlasLines[0];
Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath);
texImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
texImporter.mipmapEnabled = false;
AssetDatabase.SaveAssets();
mat.mainTexture = texture;
AssetDatabase.CreateAsset(mat, materialPath);
AssetDatabase.SaveAssets();
atlasAsset.materials[0] = mat;
AssetDatabase.CreateAsset(atlasAsset, assetPath + "/" + primaryName + "_Atlas.asset");
AssetDatabase.SaveAssets();
SkeletonDataAsset skelDataAsset = SkeletonDataAsset.CreateInstance<SkeletonDataAsset>();
skelDataAsset.atlasAsset = atlasAsset;
skelDataAsset.skeletonJSON = spineJson;
skelDataAsset.fromAnimation = new string[0];
skelDataAsset.toAnimation = new string[0];
skelDataAsset.duration = new float[0];
skelDataAsset.defaultMix = defaultMix;
AssetDatabase.CreateAsset(skelDataAsset, assetPath + "/" + primaryName + "_SkeletonData.asset");
AssetDatabase.SaveAssets();
}
else{
EditorUtility.DisplayDialog("Error!", "Atlas file not found in selection!", "OK");
return;
}
}
}