Last active
July 15, 2022 18:39
-
-
Save Saishy/a825049dff153c6c40c5fa7933adcbc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using UnityEditor; | |
| using UnityEditorInternal; | |
| using UnityEngine; | |
| /** | |
| * REQUIRES: https://github.com/Bunny83/SimpleJSON | |
| * Automatically slices a texture whose sprite mode is set to multiple, based on a json file generated by aseprite. | |
| * Json file needs to contain the "frames" key (check the Tags option), and be set to Array. | |
| */ | |
| public class AsepriteAutomaticSlicer { | |
| [MenuItem("Assets/Aseprite JSON Slicer")] | |
| private static void Slice() { | |
| Texture2D tex = Selection.activeObject as Texture2D; | |
| string path = AssetDatabase.GetAssetPath(tex); | |
| //string fileName = Path.GetFileNameWithoutExtension(path); | |
| string filePathWithoutExtension = path.Split('.')[0]; | |
| TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter; | |
| TextAsset jsonFile = AssetDatabase.LoadAssetAtPath<TextAsset>(filePathWithoutExtension + ".json"); | |
| if (jsonFile == null) { | |
| Debug.LogWarning($"{filePathWithoutExtension}.json could not be found."); | |
| return; | |
| } | |
| List<SpriteMetaData> metas = new List<SpriteMetaData>(); | |
| SimpleJSON.JSONNode sJSON = SimpleJSON.JSON.Parse(jsonFile.text); | |
| int rectNum = 0; | |
| int maxRects = sJSON["frames"].Count; | |
| int textHeight = sJSON["meta"]["size"]["h"].AsInt; | |
| for (rectNum = 0; rectNum < maxRects; rectNum++) { | |
| SpriteMetaData nSMD = new SpriteMetaData(); | |
| nSMD.rect = new Rect(sJSON["frames"][rectNum]["frame"]["x"].AsInt, textHeight - sJSON["frames"][rectNum]["frame"]["y"].AsInt - sJSON["frames"][rectNum]["frame"]["h"].AsInt, sJSON["frames"][rectNum]["frame"]["w"].AsInt, sJSON["frames"][rectNum]["frame"]["h"].AsInt); | |
| //nSMD.name = $"{fileName}_{rectNum}"; | |
| nSMD.name = sJSON["frames"][rectNum]["filename"].Value; | |
| metas.Add(nSMD); | |
| } | |
| importer.spritesheet = metas.ToArray(); | |
| EditorUtility.SetDirty(importer); | |
| importer.SaveAndReimport(); | |
| } | |
| [MenuItem("Assets/Aseprite JSON Slicer", true)] | |
| private static bool SliceValidation() { | |
| return Selection.activeObject is Texture2D; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment