Advertisement
freakrho

CAsepriteImporter

Nov 13th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.77 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using SimpleJSON;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class CAsepriteImporter
  8. {
  9.     public static string USE_TAGS = "Aseprite_use_tags";
  10.  
  11.     class Frame
  12.     {
  13.         public ObjectReferenceKeyframe frame;
  14.         public float length;
  15.     }
  16.  
  17.  
  18.     public static bool UseTags
  19.     {
  20.         get
  21.         {
  22.             return EditorPrefs.GetBool(USE_TAGS, false);
  23.         }
  24.         set
  25.         {
  26.             EditorPrefs.SetBool(USE_TAGS, value);
  27.         }
  28.     }
  29.  
  30.     [MenuItem("Assets/RhoTools/Aseprite/Import sheet")]
  31.     [MenuItem("RhoTools/Aseprite/Import sheet", false, 1)]
  32.     public static void Import()
  33.     {
  34.         string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  35.         if (tAssetPath.ToLower().EndsWith(".json"))
  36.         {
  37.             // Load texture
  38.             string tTexturePath = tAssetPath.Replace("json", "png");
  39.             TextureImporter tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
  40.             if (tTexture == null)
  41.             {
  42.                 tTexturePath = tAssetPath.Replace("json", "gif");
  43.                 tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
  44.             }
  45.  
  46.             // Change metadata
  47.             if (tTexture != null)
  48.             {
  49.                 JSONNode tData = CJSonReader.Load(tAssetPath);
  50.                 JSONNode tFrames = tData["frames"];
  51.                 JSONNode tMeta = tData["meta"];
  52.                 //int tWidth = tMeta["size"]["w"].AsInt;
  53.                 int tHeight = tMeta["size"]["h"].AsInt;
  54.                 List<SpriteMetaData> tMetaData = new List<SpriteMetaData>();
  55.                 tTexture.textureType = TextureImporterType.Sprite;
  56.                 tTexture.spriteImportMode = SpriteImportMode.Multiple;
  57.                 tTexture.filterMode = FilterMode.Point;
  58.                 List<string> tKeys = new List<string>();
  59.                 tKeys.AddRange(tFrames.Keys);
  60.                 tKeys.Sort();
  61.  
  62.                 Dictionary<string, int> tAnimations = new Dictionary<string, int>();
  63.  
  64.                 foreach (string tKey in tKeys)
  65.                 {
  66.                     JSONNode tNode = tFrames[tKey];
  67.                     SpriteMetaData tThisData = new SpriteMetaData();
  68.                     string tTag = GetTag(tKey);
  69.                     if (!UseTags || tTag != "")
  70.                     {
  71.                         string tName = "";
  72.                         if (UseTags)
  73.                         {
  74.                             if (tAnimations.ContainsKey(tTag))
  75.                                 tAnimations[tTag] += 1;
  76.                             else
  77.                                 tAnimations.Add(tTag, 0);
  78.                             tName = tTag + "_" + tAnimations[tTag];
  79.                         }
  80.                         else
  81.                             tName = tKey;
  82.                         tThisData.name = tName;
  83.                         JSONNode tRect = tNode["frame"];
  84.                         tThisData.rect = new Rect(
  85.                             tRect["x"].AsFloat,
  86.                             tRect["y"].AsFloat,
  87.                             tRect["w"].AsFloat,
  88.                             tRect["h"].AsFloat
  89.                         );
  90.                         tThisData.rect.y = tHeight - tThisData.rect.y - tThisData.rect.height;
  91.                         tMetaData.Add(tThisData);
  92.                     }
  93.                 }
  94.                 tTexture.spritesheet = tMetaData.ToArray();
  95.                 AssetDatabase.ImportAsset(tTexturePath, ImportAssetOptions.ForceUpdate);
  96.             }
  97.         }
  98.     }
  99.  
  100.     public static string GetTag(string aName)
  101.     {
  102.         string[] tSplit = aName.Split('_');
  103.         if (tSplit.Length > 0)
  104.             return tSplit[0];
  105.         return "";
  106.     }
  107.  
  108.     [MenuItem("Assets/RhoTools/Aseprite/Import sheet", true)]
  109.     [MenuItem("RhoTools/Aseprite/Import sheet", true)]
  110.     public static bool ImportValidation()
  111.     {
  112.         string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  113.         return tAssetPath.ToLower().EndsWith(".json");
  114.     }
  115.  
  116.     [MenuItem("Assets/RhoTools/Aseprite/Use tagged sheet", false)]
  117.     [MenuItem("RhoTools/Aseprite/Use tagged sheet", false, 2)]
  118.     static void MenuUseTags()
  119.     {
  120.         Menu.SetChecked("Assets/RhoTools/Aseprite/Use tagged sheet", !UseTags);
  121.         Menu.SetChecked("RhoTools/Aseprite/Use tagged sheet", !UseTags);
  122.         UseTags = !UseTags;
  123.     }
  124.  
  125.     [MenuItem("Assets/RhoTools/Aseprite/Use tagged sheet", true)]
  126.     [MenuItem("RhoTools/Aseprite/Use tagged sheet", true)]
  127.     static bool MenuUseTagsVerification()
  128.     {
  129.         Menu.SetChecked("Assets/RhoTools/Aseprite/Use tagged sheet", UseTags);
  130.         Menu.SetChecked("RhoTools/Aseprite/Use tagged sheet", UseTags);
  131.         return true;
  132.     }
  133.  
  134.     [MenuItem("Assets/RhoTools/Aseprite/Import animation from sheet")]
  135.     [MenuItem("RhoTools/Aseprite/Import animation from sheet", false, 3)]
  136.     public static void ImportAnimation()
  137.     {
  138.         string tAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  139.         if (tAssetPath.ToLower().EndsWith(".json"))
  140.         {
  141.             // Load texture
  142.             string tTexturePath = tAssetPath.Replace("json", "png");
  143.             TextureImporter tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
  144.             if (tTexture == null)
  145.             {
  146.                 tTexturePath = tAssetPath.Replace("json", "gif");
  147.                 tTexture = AssetImporter.GetAtPath(tTexturePath) as TextureImporter;
  148.             }
  149.  
  150.             // Change metadata
  151.             if (tTexture != null)
  152.             {
  153.                 JSONNode tData = CJSonReader.Load(tAssetPath);
  154.                 JSONNode tFrames = tData["frames"];
  155.                 tTexture.textureType = TextureImporterType.Sprite;
  156.                 tTexture.spriteImportMode = SpriteImportMode.Multiple;
  157.                 tTexture.filterMode = FilterMode.Point;
  158.                 List<string> tKeys = new List<string>();
  159.                 tKeys.AddRange(tFrames.Keys);
  160.                 tKeys.Sort();
  161.  
  162.                 Dictionary<string, int> tAnimationsIndexes = new Dictionary<string, int>();
  163.                 Dictionary<string, List<Frame>> tAnimations = new Dictionary<string, List<Frame>>();
  164.                 Sprite[] tSprites = AssetDatabase.LoadAllAssetsAtPath(tTexturePath)
  165.                     .OfType<Sprite>().ToArray();
  166.  
  167.                 foreach (string tKey in tKeys)
  168.                 {
  169.                     string tTag = GetTag(tKey);
  170.                     if (tTag != "")
  171.                     {
  172.                         List<Frame> tFrameList;
  173.                         if (tAnimations.ContainsKey(tTag))
  174.                         {
  175.                             tFrameList = tAnimations[tTag];
  176.                         }
  177.                         else
  178.                         {
  179.                             tAnimationsIndexes.Add(tTag, 0);
  180.                             tFrameList = new List<Frame>();
  181.                             tAnimations.Add(tTag, tFrameList);
  182.                         }
  183.                         ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
  184.                         if (tFrameList.Count > 0)
  185.                         {
  186.                             float tTime = 0;
  187.                             for (int i = 0; i < tFrameList.Count; i++)
  188.                             {
  189.                                 tTime += tFrameList[i].length;
  190.                             }
  191.                             kf.time = tTime;
  192.                         }
  193.                         else
  194.                             kf.time = 0;
  195.                         kf.value = GetSprite(tSprites, tTag + "_" + tAnimationsIndexes[tTag].ToString());
  196.                         Frame tFrame = new Frame();
  197.                         tFrame.frame = kf;
  198.                         tFrame.length = tFrames[tKey]["duration"].AsFloat / 1000f;
  199.                         tAnimations[tTag].Add(tFrame);
  200.                         tAnimationsIndexes[tTag] += 1;
  201.                     }
  202.                 }
  203.  
  204.                 foreach (string tKey in tAnimations.Keys)
  205.                 {
  206.                     List<Frame> tAnimation = tAnimations[tKey];
  207.                     int tFramecount = tAnimation.Count;
  208.  
  209.                     AnimationClip tClip = new AnimationClip();
  210.                     tClip.frameRate = 60;
  211.                     tClip.wrapMode = WrapMode.Loop;
  212.  
  213.                     EditorCurveBinding curveBinding = new EditorCurveBinding();
  214.                     curveBinding.type = typeof(SpriteRenderer);
  215.                     curveBinding.propertyName = "m_Sprite";
  216.  
  217.                     ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[tFramecount + 1];
  218.  
  219.                     for (int i = 0; i < tFramecount; i++)
  220.                     {
  221.                         keyFrames[i] = tAnimation[i].frame;
  222.                     }
  223.                     ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
  224.                     kf.time = keyFrames[tFramecount - 1].time + tAnimation[0].length;
  225.                     kf.value = tAnimation[0].frame.value;
  226.                     keyFrames[tFramecount] = kf;
  227.  
  228.                     tClip.wrapMode = WrapMode.Once;
  229.                     AnimationUtility.SetObjectReferenceCurve(tClip, curveBinding, keyFrames);
  230.  
  231.                     tClip.name = tKey;
  232.                     AssetDatabase.CreateAsset(tClip, "Assets/Art/Animations/" + tKey + ".anim");
  233.                     AssetDatabase.SaveAssets();
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239.     static Sprite GetSprite(Sprite[] aList, string aName)
  240.     {
  241.         for (int i = 0; i < aList.Length; i++)
  242.         {
  243.             Sprite tSprite = aList[i];
  244.             if (tSprite.name == aName)
  245.                 return tSprite;
  246.         }
  247.         return null;
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement