# 透過 unity 拆圖
### 根據參考教學的輸出步驟
1. 匯入 plist 讀取工具
https://github.com/nekocode/Unity3D-TextureAtlasSlicer-Cocos2d-x
2. 將這段 script 放到 unity 專案內
```unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class AtlasTool : Editor {
[MenuItem("Tools/Slice Atlas")]
public static void SliceAtlas()
{
if(!Directory.Exists("atlasout"))
{
Directory.CreateDirectory ("atlasout");
}
var dir = new DirectoryInfo ("Assets/AtlasTool");
var imgs = dir.GetFiles ("*.png");
for (int i = 0; i < imgs.Length; i++) {
DealPng (imgs[i]);
}
}
public static void DealPng(FileInfo file)
{
var outdir = "atlasout/" + file.Name.Replace (".png", "");
if(!Directory.Exists(outdir))
{
Directory.CreateDirectory (outdir);
}
string path = "Assets/AtlasTool/" + file.Name;
var assets2 = AssetDatabase.LoadAllAssetsAtPath (path);
for (int i = 0; i < assets2.Length; i++) {
Debug.LogError (assets2[i]);
if(assets2[i] is Sprite)
{
var sp = assets2 [i] as Sprite;
Texture2D t2d = new Texture2D ((int)sp.rect.width, (int)sp.rect.height, TextureFormat.RGBA32, false);
var aslasTexture = sp.texture;
t2d.SetPixels (aslasTexture.GetPixels((int)sp.rect.x, (int)sp.rect.y, (int)sp.rect.width, (int)sp.rect.height));
t2d.Apply ();
File.WriteAllBytes (outdir +"/" +sp.name +".png", t2d.EncodeToPNG());
}
}
}
}
```
3. 將 spritesheet 的 **png** 和 **plist** 檔案放到 **Asset/AtlasTool**
4. 設定圖片
* Texture Type -> Sprite (2D and UI)
* Sprite Mode -> Multiple
* Advanced -> 勾選 **Read/Write Enabled**
* Compression -> None
* 按下 Apply
![](https://i.imgur.com/UxEYvg6.png)
5. 圖片右鍵 -> Slice Sprite Using Coscos2d-x Plist
![](https://i.imgur.com/TWPTvD0.png)
6. 選圖片打開 Sprite Editor, 按 slice 自動切割圖片範圍, 再按 Apply 儲存
7. Tools -> Slice Atlas
![](https://i.imgur.com/RiilMXd.png)
8. 輸出圖片在 **Assets** 同層的 **atlasout** 資料夾
---
### 疑難排解
* 輸出的碎檔, 檔名並非 plist 設定的名稱
:::success
先設定圖片成 sprite, 再按 **Slice Sprite Using Coscos2d-x Plist** 這樣才會沿用 plist 內設定的名稱
:::
---
### 參考教學
* [利用UNITY切割图集](https://www.freesion.com/article/2032633991)
---
###### tags: `教學`