美文网首页
Texture Type---Sprite & 图片分割

Texture Type---Sprite & 图片分割

作者: 老汪032588 | 来源:发表于2019-07-23 17:25 被阅读0次

Sprite Mode:

1. Single:单图。 

2. Multiple:多图。 

3. Polygon:多边形,在SpriteEditor里使用多边形裁剪精灵。 

Pixels Per Unit:每单位像素数,在世界场景中,每单位距离有多少个 像素。

Mesh Type:

1. FullRect:矩形。

 2. Tight:紧凑的,根据Alpha通道生成Mesh。

ExtrudeEdge:拉伸边缘。

Pivot:轴心(仅Single),精灵内部坐标的原点。

                                                                                   分割图片


Sprite Mode:Multiple

点击Sprite Editor →点击Slice出现下面这个界面

Type:

Automatic:自动分割,一般都用这个

Gird By Cell Size:按像素分割

Gird By Cell Count:按列(Column)和行(Row)分割

选择好后按Slice就分割了,按住Ctrl可让分割出来的小图更清晰,可以点击小图自己修改。最后按Apply就分割好了。

注意下分割好后跟老版本的区别是不会再单独显示出来,需要点击图片右边的小三角形就能看到。

很多时候我们需要将这些分割出来的保存为图片。下面将代码贴出来,资源放在Assets/Resources/UI/下

using UnityEngine;

using UnityEditor;

public class TestSaveSprite

{

    [MenuItem("Tools/导出精灵")]

    static void SaveSprite()

    {

        int num = 0;

        string resourcesPath = "Assets/Resources/";

        if (Selection.objects.Length == 0)

        {

            Debug.LogError("Please Select Picture");

            return;

        }

        foreach (Object obj in Selection.objects)

        {

            string selectionPath = AssetDatabase.GetAssetPath(obj);

            // 必须最上级是"Assets/Resources/"

            if (!selectionPath.StartsWith(resourcesPath))

            {

                continue;

            }

            string selectionExt = System.IO.Path.GetExtension(selectionPath);

            if (selectionExt.Length == 0)

            {

                continue;

            }

            // 从路径"Assets/Resources/UI/testUI.png"得到路径"UI/testUI"

            string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);

            loadPath = loadPath.Substring(resourcesPath.Length);

            // 加载此文件下的所有资源

            Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);

            if (sprites.Length == 0)

            {

                continue;

            }

            // 创建导出文件夹

            string outPath = Application.dataPath + "/outSprite/" + loadPath;

            System.IO.Directory.CreateDirectory(outPath);

            foreach (Sprite sprite in sprites)

            {

                // 创建单独的纹理

                Texture2D myimage = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);

                //abc_0:(x:2.00, y:400.00, width:103.00, height:112.00)

                for (int y = (int)sprite.rect.y; y < sprite.rect.y + sprite.rect.height; y++)//Y轴像素

                {

                    for (int x = (int)sprite.rect.x; x < sprite.rect.x + sprite.rect.width; x++)

                        myimage.SetPixel(x - (int)sprite.rect.x, y - (int)sprite.rect.y, sprite.texture.GetPixel(x, y));

                }

                //转换纹理到EncodeToPNG兼容格式

                if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)

                {

                    Texture2D newTexture = new Texture2D(myimage.width, myimage.height);

                    newTexture.SetPixels(myimage.GetPixels(0), 0);

                    myimage = newTexture;

                }

                myimage.alphaIsTransparency = true;

                System.IO.File.WriteAllBytes(outPath + "/" + sprite.name + ".png", myimage.EncodeToPNG());

                num++;

            }

            Debug.Log("SaveSprite to " + outPath);

        }

        Debug.Log("SaveSprite Finished  Export Num = " + num);

    }

}

放到工程里面就可以在Tools看到,注意,先点击刚刚分割好的图片(源图),再点击导出精灵,才能保存成功。

相关文章

网友评论

      本文标题:Texture Type---Sprite & 图片分割

      本文链接:https://www.haomeiwen.com/subject/chealctx.html