0
点赞
收藏
分享

微信扫一扫

NGUI图集分解修改

萧萧雨潇潇 2022-05-05 阅读 38
前端

UIAtlasMaker.cs

Unity3d通用工具类之NGUI图集分解 - 走看看

有些图片边界一些alpha通道没有分解出来 在上面基础上 修改了下.

static void ExportTexturePNGFromAtlas(string folderPath, UIAtlas atlas)
    {
        List<UISpriteData> exitSpritesList = atlas.spriteList;
        Texture2D atlasTexture = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);
        int oldwith = atlasTexture.width;
        int oldHeight = atlasTexture.height;
        Color32[] oldPixels = null;
        foreach (var es in exitSpritesList)
        {

            int xmin = Mathf.Clamp(es.x, 0, oldwith);
            int ymin = Mathf.Clamp(es.y, 0, oldHeight);
            int newWidth = Mathf.Clamp(es.width+es.paddingLeft+es.paddingRight, 0, oldwith);
            int newHeight = Mathf.Clamp(es.height+es.paddingBottom+es.paddingTop, 0, oldHeight);

            if (newWidth == 0 || newHeight == 0) continue;
            if (oldPixels == null) oldPixels = atlasTexture.GetPixels32();
            Color32[] newPixels = new Color32[newWidth * newHeight];

            for (int y = 0; y < newHeight; ++y)
            {
                for (int x = 0; x < newWidth; ++x)
                {
                    if (y>=es.paddingBottom && y<newHeight-es.paddingTop && x>=es.paddingLeft && x<newWidth-es.paddingRight)
                    {
                        int newIndex = (newHeight - 1 - y) * newWidth + x;
                        int oldIndex = (oldHeight - 1 - (ymin + y-es.paddingBottom)) * oldwith + (xmin + x-es.paddingLeft);
                        newPixels[newIndex] = oldPixels[oldIndex];
                    }
                    else
                    {
                        int newIndex = (newHeight - 1 - y) * newWidth + x;
                        newPixels[newIndex] = new Color32(0,0,0,0);
                    }
                }
            }

            Texture2D t = new Texture2D(newWidth, newHeight);
            t.SetPixels32(newPixels);
            t.Apply();
            byte[] bytes = t.EncodeToPNG();
            Texture2D.DestroyImmediate(t);
            t = null;
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (FileStream fs = new FileStream(folderPath + "/" + es.name + ".png", FileMode.CreateNew))
            {
                BinaryWriter writer = new BinaryWriter(fs);
                writer.Write(bytes);
            }
        }
    }

举报

相关推荐

0 条评论