Sprite editor slices gallery genie

Source: Internet
Author: User

Suppose there is a big UI Gallery, and we want to cut the small figure in it one by one. If there is a plist file, please refer to my other article "restoring the texturepacker plist file to cut down pictures"

Today we use the unity4.3 or laterSprite EditorTo export the slice genie

Effect

Procedure

1. Prepare a large gallery and import it to the asset/resources/XXX/folder of Unity (Note: The Gallery file must be placed under the Resources file)

2. The default gallery is texture. We need to change its type to Sprite.

3. Modify the sprite mode to multiple and apply it. Then click sprite Editor

4. In the open sprite edirot window, cut the gallery into multiple sprite instances.

5. Compile testexportsprite. CS and put it in the editor directory.

using UnityEngine;using UnityEditor;public class TestExportSprite{        [MenuItem("Assets/导出选中图片为单独png")]    static void ExportSelSprite()    {        string resourcesPath = "Assets/Resources/";        foreach (Object obj in Selection.objects)        {            string selectionPath = AssetDatabase.GetAssetPath(obj);            // 必须最上级是"Assets/Resources/"            if (selectionPath.StartsWith(resourcesPath))            {                string selectionExt = System.IO.Path.GetExtension(selectionPath);                if (selectionExt.Length == 0)                {                    continue;                }                // 得到导出路径                string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);                loadPath = loadPath.Substring(resourcesPath.Length);                // 加载此文件下的所有资源                Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);                if (sprites.Length > 0)                {                    // 创建导出文件夹                    string outPath = Application.dataPath + "/outSprite/" + loadPath;                    System.IO.Directory.CreateDirectory(outPath);                    foreach (Sprite sprite in sprites)                    {                        // 创建单独的纹理                        Texture2D tex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, sprite.texture.format, false);                        tex.SetPixels(sprite.texture.GetPixels((int)sprite.rect.xMin, (int)sprite.rect.yMin,                            (int)sprite.rect.width, (int)sprite.rect.height));                        tex.Apply();                        // 写入成PNG文件                        System.IO.File.WriteAllBytes(outPath + "/" + sprite.name + ".png", tex.EncodeToPNG());                    }                    Debug.Log(string.Format("Export {0} to {1}",loadPath,outPath));                }            }        }        Debug.Log("Export All Sprites Finished");    }}

5. Use the sprite editor to cut the gallery into a Sprite, modify the attributes of the gallery to advanced, and check read/write enabled and transpare.

Otherwise, an error will be reported when you export the slice.

UnityException: Texture ‘UIAtlas‘ is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.UnityEngine.Texture2D.GetPixels (Int32 x, Int32 y, Int32 blockWidth, Int32 blockHeight) (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/TextureBindings.cs:259)TestSaveSprite.SaveSprite () (at Assets/Editor/TestSaveSprite.cs:39)

5. Select uiatlas. PSD in the resources directory, right-click the image, and select "Export selected image as a separate PNG Image ".

Some content reference from: http://blog.csdn.net/akof1314/article/details/38845933

Sprite editor slices gallery genie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.