Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using SimpleJSON;
- using System.Collections.Generic;
- using System.Linq;
- public class CAsepriteImporter
- {
- public static string USE_TAGS = "Aseprite_use_tags";
- class Frame
- {
- public ObjectReferenceKeyframe frame;
- public float length;
- }
- public static bool UseTags
- {
- get
- {
- return EditorPrefs.GetBool(USE_TAGS, false);
- }
- set
- {
- EditorPrefs.SetBool(USE_TAGS, value);
- }
- }
- [MenuItem("Assets/RhoTools/Aseprite/Import sheet")]
- [MenuItem("RhoTools/Aseprite/Import sheet", false, 1)]
- public static void Import()
- {
- string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
- if (tAssetPath.ToLower().EndsWith(".json"))
- {
- // Load texture
- string tTexturePath = tAssetPath.Replace("json", "png");
- TextureImporter tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
- if (tTexture == null)
- {
- tTexturePath = tAssetPath.Replace("json", "gif");
- tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
- }
- // Change metadata
- if (tTexture != null)
- {
- JSONNode tData = CJSonReader.Load(tAssetPath);
- JSONNode tFrames = tData["frames"];
- JSONNode tMeta = tData["meta"];
- //int tWidth = tMeta["size"]["w"].AsInt;
- int tHeight = tMeta["size"]["h"].AsInt;
- List<SpriteMetaData> tMetaData = new List<SpriteMetaData>();
- tTexture.textureType = TextureImporterType.Sprite;
- tTexture.spriteImportMode = SpriteImportMode.Multiple;
- tTexture.filterMode = FilterMode.Point;
- List<string> tKeys = new List<string>();
- tKeys.AddRange(tFrames.Keys);
- tKeys.Sort();
- Dictionary<string, int> tAnimations = new Dictionary<string, int>();
- foreach (string tKey in tKeys)
- {
- JSONNode tNode = tFrames[tKey];
- SpriteMetaData tThisData = new SpriteMetaData();
- string tTag = GetTag(tKey);
- if (!UseTags || tTag != "")
- {
- string tName = "";
- if (UseTags)
- {
- if (tAnimations.ContainsKey(tTag))
- tAnimations[tTag] += 1;
- else
- tAnimations.Add(tTag, 0);
- tName = tTag + "_" + tAnimations[tTag];
- }
- else
- tName = tKey;
- tThisData.name = tName;
- JSONNode tRect = tNode["frame"];
- tThisData.rect = new Rect(
- tRect["x"].AsFloat,
- tRect["y"].AsFloat,
- tRect["w"].AsFloat,
- tRect["h"].AsFloat
- );
- tThisData.rect.y = tHeight - tThisData.rect.y - tThisData.rect.height;
- tMetaData.Add(tThisData);
- }
- }
- tTexture.spritesheet = tMetaData.ToArray();
- AssetDatabase.ImportAsset(tTexturePath, ImportAssetOptions.ForceUpdate);
- }
- }
- }
- public static string GetTag(string aName)
- {
- string[] tSplit = aName.Split('_');
- if (tSplit.Length > 0)
- return tSplit[0];
- return "";
- }
- [MenuItem("Assets/RhoTools/Aseprite/Import sheet", true)]
- [MenuItem("RhoTools/Aseprite/Import sheet", true)]
- public static bool ImportValidation()
- {
- string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
- return tAssetPath.ToLower().EndsWith(".json");
- }
- [MenuItem("Assets/RhoTools/Aseprite/Use tagged sheet", false)]
- [MenuItem("RhoTools/Aseprite/Use tagged sheet", false, 2)]
- static void MenuUseTags()
- {
- Menu.SetChecked("Assets/RhoTools/Aseprite/Use tagged sheet", !UseTags);
- Menu.SetChecked("RhoTools/Aseprite/Use tagged sheet", !UseTags);
- UseTags = !UseTags;
- }
- [MenuItem("Assets/RhoTools/Aseprite/Use tagged sheet", true)]
- [MenuItem("RhoTools/Aseprite/Use tagged sheet", true)]
- static bool MenuUseTagsVerification()
- {
- Menu.SetChecked("Assets/RhoTools/Aseprite/Use tagged sheet", UseTags);
- Menu.SetChecked("RhoTools/Aseprite/Use tagged sheet", UseTags);
- return true;
- }
- [MenuItem("Assets/RhoTools/Aseprite/Import animation from sheet")]
- [MenuItem("RhoTools/Aseprite/Import animation from sheet", false, 3)]
- public static void ImportAnimation()
- {
- string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
- if (tAssetPath.ToLower().EndsWith(".json"))
- {
- // Load texture
- string tTexturePath = tAssetPath.Replace("json", "png");
- TextureImporter tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
- if (tTexture == null)
- {
- tTexturePath = tAssetPath.Replace("json", "gif");
- tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
- }
- // Change metadata
- if (tTexture != null)
- {
- JSONNode tData = CJSonReader.Load(tAssetPath);
- JSONNode tFrames = tData["frames"];
- tTexture.textureType = TextureImporterType.Sprite;
- tTexture.spriteImportMode = SpriteImportMode.Multiple;
- tTexture.filterMode = FilterMode.Point;
- List<string> tKeys = new List<string>();
- tKeys.AddRange(tFrames.Keys);
- tKeys.Sort();
- Dictionary<string, int> tAnimationsIndexes = new Dictionary<string, int>();
- Dictionary<string, List<Frame>> tAnimations = new Dictionary<string, List<Frame>>();
- Sprite[] tSprites = AssetDatabase.LoadAllAssetsAtPath(tTexturePath)
- .OfType<Sprite>().ToArray();
- foreach (string tKey in tKeys)
- {
- string tTag = GetTag(tKey);
- if (tTag != "")
- {
- List<Frame> tFrameList;
- if (tAnimations.ContainsKey(tTag))
- {
- tFrameList = tAnimations[tTag];
- }
- else
- {
- tAnimationsIndexes.Add(tTag, 0);
- tFrameList = new List<Frame>();
- tAnimations.Add(tTag, tFrameList);
- }
- ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
- if (tFrameList.Count > 0)
- {
- float tTime = 0;
- for (int i = 0; i < tFrameList.Count; i++)
- {
- tTime += tFrameList[i].length;
- }
- kf.time = tTime;
- }
- else
- kf.time = 0;
- kf.value = GetSprite(tSprites, tTag + "_" + tAnimationsIndexes[tTag].ToString());
- Frame tFrame = new Frame();
- tFrame.frame = kf;
- tFrame.length = tFrames[tKey]["duration"].AsFloat / 1000f;
- tAnimations[tTag].Add(tFrame);
- tAnimationsIndexes[tTag] += 1;
- }
- }
- foreach (string tKey in tAnimations.Keys)
- {
- List<Frame> tAnimation = tAnimations[tKey];
- int tFramecount = tAnimation.Count;
- AnimationClip tClip = new AnimationClip();
- tClip.frameRate = 60;
- tClip.wrapMode = WrapMode.Loop;
- EditorCurveBinding curveBinding = new EditorCurveBinding();
- curveBinding.type = typeof(SpriteRenderer);
- curveBinding.propertyName = "m_Sprite";
- ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[tFramecount + 1];
- for (int i = 0; i < tFramecount; i++)
- {
- keyFrames[i] = tAnimation[i].frame;
- }
- ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
- kf.time = keyFrames[tFramecount - 1].time + tAnimation[0].length;
- kf.value = tAnimation[0].frame.value;
- keyFrames[tFramecount] = kf;
- tClip.wrapMode = WrapMode.Once;
- AnimationUtility.SetObjectReferenceCurve(tClip, curveBinding, keyFrames);
- tClip.name = tKey;
- AssetDatabase.CreateAsset(tClip, "Assets/Art/Animations/" + tKey + ".anim");
- AssetDatabase.SaveAssets();
- }
- }
- }
- }
- static Sprite GetSprite(Sprite[] aList, string aName)
- {
- for (int i = 0; i < aList.Length; i++)
- {
- Sprite tSprite = aList[i];
- if (tSprite.name == aName)
- return tSprite;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement