Unity 匯出切片精靈

來源:互聯網
上載者:User

標籤:unity   2d   

假設有一張png/tga圖集,匯入到Unity,放置目錄"Assets/Resources/UI"(UI檔案夾可替換成其他的,重要的是要在"Assets/Resources/"路徑下),預設為如下設定:

為了可以使用Unity內建的精靈切割,要將紋理類型改成"Sprite","Sprite Mode"改成"Multiple","Format"改成"Truecolor",點擊"Apply"按鈕進行應用。
接著,點擊"Sprite Editor"開啟精靈編輯器,點擊左上方的"Slice"按鈕,彈出切片設定,再次點擊裡面的"Slice"按鈕,就會自動對圖片進行切割,如所示:
在對切割不完整的地方進行修正後,點擊右上方的"Apply"按鈕,進行儲存。可以看到Project視圖下這個圖集,已經被分割出許多小圖了,如所示:
接下來,因為要對圖片進行讀寫操作,要更改圖片的屬性才能進行,否則會提示如下:
  • UnityException: Texture ‘testUI‘ is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
將圖片紋理類型更改為"Advanced",將"Read/Write Enabled"屬性進行打勾,如所示:
建立一個指令檔,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using UnityEditor;

public class TestSaveSprite
{
    [MenuItem("Tools/匯出精靈")]
    static void SaveSprite()
    {
        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;
                }

                // 從路徑"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)
                {
                    // 建立匯出檔案夾
                    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("SaveSprite to " + outPath);
                }
            }
        }
        Debug.Log("SaveSprite Finished");
    }
}
在Unity編輯器將會看到Tools菜單下多了"匯出精靈"項,選中圖集,然後點擊"匯出精靈"功能表項目,即可匯出子圖成功。如所示:


Unity 匯出切片精靈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.