TexturepackerGUI&Unity3d native2d Sprite

TexturepackerGUI生成的图集如何在Unity3d里边用native 2d sprite显示切好的小图

a.解析Texturepacker的json用的是Litjson这个库

b.Texturepacker导出图集的时候不能用旋转(因为unity3d支持:(

上码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using LitJson;
public class EAtlasBuild  {


    public class ERect 
    {
        public int x, y;
        public int w, h;
        public ERect(JsonData jsonData) 
        {

            x = (int)jsonData["x"];
            y = (int)jsonData["y"];
            w = (int)jsonData["w"];
            h = (int)jsonData["h"];
        }
    }

    public class ESpriteData 
    {
        public string m_strName = "";
        public ERect frame;
        public bool rotated = false;
        public bool trimmed = false;
        public ERect spriteSourceSize;
        public int sourceWidth;
        public int sourceHeight;

        public ESpriteData(JsonData jsonData,string _name)
        {
            m_strName = _name;
            frame = new ERect(jsonData["frame"]);
            rotated = (bool)jsonData["rotated"];
            trimmed = (bool)jsonData["trimmed"];
            spriteSourceSize = new ERect(jsonData["spriteSourceSize"]);
            JsonData sourceSize = jsonData["sourceSize"];
            sourceWidth = (int)sourceSize["w"];
            sourceHeight = (int)sourceSize["h"];

        }
    }
    [MenuItem("Custom/SetAtlas")]
    static public void SetAtlas() 
    {
        string atlasPath = Application.dataPath + "/_Atlas/UI/UI.png";
        string atlasTxtPath = Application.dataPath + "/_Atlas/UI/UI.txt";
        string jsonStr = File.ReadAllText(atlasTxtPath);
        JsonData frames = JsonMapper.ToObject(jsonStr)["frames"];
        IDictionary dict = frames as IDictionary;
        List<ESpriteData> lstSprite = new List<ESpriteData>();
        
        foreach (string key in dict.Keys) 
        {
            string fileName = key;
            ESpriteData sprite = new ESpriteData(frames[fileName], fileName);
            lstSprite.Add(sprite);
        }
        Debug.Log("count:" + lstSprite.Count);
        string atlasAssetsPath = atlasPath.Substring(atlasPath.IndexOf("Assets"));
        TextureImporter textureImporter = AssetImporter.GetAtPath(atlasAssetsPath) as TextureImporter;
        if (textureImporter != null) 
        {
            Texture2D texture2d = Resources.LoadAssetAtPath<Texture2D>(atlasAssetsPath);


            int width = texture2d.width;
            int height = texture2d.height;

            
            SpriteMetaData[] sprites = new SpriteMetaData[lstSprite.Count];

            for (int i = 0; i < lstSprite.Count; i++) 
            {
                ESpriteData spriteData = lstSprite[i];
                ERect frame = spriteData.frame;
                sprites[i] = new SpriteMetaData();
                sprites[i].name = spriteData.m_strName;
                sprites[i].pivot = new Vector2(0.5f, 0.5f);
                sprites[i].rect = new Rect(
                    frame.x, 
                    height - frame.y - frame.h, 
                    frame.w, frame.h);
            }
            Debug.Log("XD");
            textureImporter.textureType = TextureImporterType.Sprite;
            textureImporter.spriteImportMode = SpriteImportMode.Multiple;
            textureImporter.spritesheet = sprites;
            textureImporter.spritePixelsToUnits = 100;
            textureImporter.filterMode = FilterMode.Point;


            TextureImporterSettings tipSeting = new TextureImporterSettings();
            textureImporter.ReadTextureSettings(tipSeting);
            textureImporter.SetTextureSettings(tipSeting);
            AssetDatabase.ImportAsset(atlasAssetsPath);
        }
    }
}

效果图:

TexturepackerGUI&Unity3d native2d Sprite_第1张图片


你可能感兴趣的:(unity3d,TexturePacker)