TexturePacker图集转Unity Sprite Multiple

用大佬代码改的:https://blog.csdn.net/salvare/article/details/82712466
如果导出的Json是Array形式的直接用上面大佬的代码就行,但是我导出的Json是Hash形式的所以要改一改
由于JsonUtility不支持C#中的Dictionary,所以导入第三方库LitJson,下载地址:LitJson.dll,下载完放到Assets\Plugins下

代码放到Assets\Editor下
使用方法:Texture和json放到同目录下,选中Texture文件,点击Unity菜单下的Tools/TexturePacker2UnitySprite

using LitJson;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[System.Serializable]
public class V4 {
    public int x;
    public int y;
    public int w;
    public int h;

    public V4() { }
}

[System.Serializable]
public class V2 {
    public int w;
    public int h;

    public V2() { }
}

[System.Serializable]
public class Frame {
    public V4 frame;
    public bool rotated;
    public bool trimmed;
    public V4 spriteSourceSize;
    public V2 sourceSize;

    public Frame() { }
}

[System.Serializable]
public class Meta {
    public string image;
    public V2 size;
}

[System.Serializable]
public class Frames {
    public Dictionary frames = new Dictionary();
    public Meta meta;
    public Frames() { }
}

public class TexturePackerImporterEditor {
    Frames frames = null;
    Texture2D textureFile = null;

    [MenuItem("Tools/TexturePacker2UnitySprite")]
    static void TexturePacker2UnitySprite() {
        if (null == Selection.activeObject) {
            return;
        }
        Texture2D tmpTexture = Selection.activeObject as Texture2D;
        if (null == tmpTexture) {
            Debug.LogError("Selection.activeObject not Texture2D");
            return;
        }
        TexturePackerImporterEditor textureEditor = new TexturePackerImporterEditor();
        textureEditor.textureFile = tmpTexture;

        string filePath = AssetDatabase.GetAssetPath(textureEditor.textureFile);
        string jsonFilePath = System.IO.Path.ChangeExtension(filePath, "json");
        var jsonFile = AssetDatabase.LoadAssetAtPath(jsonFilePath, typeof(TextAsset)) as TextAsset;
        if (null == jsonFile) {
            Debug.LogErrorFormat("not found {0}", jsonFilePath);
            return;
        }
        textureEditor.frames = JsonMapper.ToObject(jsonFile.text);
        textureEditor.ImportTexture();
    }

    void ImportTexture() {
        string filePath = AssetDatabase.GetAssetPath(textureFile);
        TextureImporter textureImporter = TextureImporter.GetAtPath(filePath) as TextureImporter;

        //生成Sprite
        List spriteMetas = new List();
        foreach (KeyValuePair dicValue in frames.frames) {
            SpriteMetaData spriteMeta = new SpriteMetaData();
            Frame frame = dicValue.Value;
            int width = frame.rotated ? frame.frame.h : frame.frame.w;
            int height = frame.rotated ? frame.frame.w : frame.frame.h;

            int x = frame.frame.x;
            //TexturePacker以左上为原点,Unity以左下为原点
            int y = frames.meta.size.h - height - frame.frame.y;

            spriteMeta.rect = new Rect(x, y, width, height);
            spriteMeta.pivot = spriteMeta.rect.center;
            spriteMeta.name = System.IO.Path.ChangeExtension(dicValue.Key, null);
            spriteMetas.Add(spriteMeta);
        }
        textureImporter.textureType = TextureImporterType.Sprite;
        textureImporter.spriteImportMode = SpriteImportMode.Multiple;
        textureImporter.spritesheet = spriteMetas.ToArray();

        AssetDatabase.ImportAsset(filePath, ImportAssetOptions.ForceUncompressedImport);
    }
}

你可能感兴趣的:(Unity)