#if UNITY_EDITORusing System.IO;
using UnityEditor;using UnityEngine;
using UnityEngine.UI;
public class BatchCreatePrefabInPath{? ??
private const string ORIGIN_DIR = "\\Atlas"; //需要轉(zhuǎn)換的目錄(手動(dòng)修改目錄)? ??
private const string TARGET_DIR = "\\Resources\\prefabs"; //轉(zhuǎn)換后放入prefab的目錄? ??
////// 將目錄下所有圖片轉(zhuǎn)成Sprite prefab
///[MenuItem("Tools/batch/batchCreateSpritePrefabInPath")]? ??
public static void batchCreateSpritePrefabInPath()? ? {? ? ? ??
string targetDir = Application.dataPath + TARGET_DIR;? ? ? ??
string originDir = Application.dataPath + ORIGIN_DIR;? ? ? ??
if (!Directory.Exists(originDir))? ? ? ? {? ? ? ? ? ??
EditorUtility.DisplayDialog("錯(cuò)誤", originDir.Replace("\\", "/") + "目錄不存在", "確定");? ? ? ? ? ? return;? ? ? ? }? ? if (!File.Exists(targetDir)) Directory.CreateDirectory(targetDir);?
//如果目錄不存在創(chuàng)建空的目標(biāo)目錄? ? ? ??
DirectoryInfo originDirInfo = new DirectoryInfo(originDir);? ? ? ??
//創(chuàng)建prefab? ? ? ??
makeSpritePrefabs(originDirInfo.GetFiles("*.jpg", SearchOption.AllDirectories), targetDir);? ? ? ? makeSpritePrefabs(originDirInfo.GetFiles("*.png", SearchOption.AllDirectories), targetDir);? ? ? ? EditorUtility.ClearProgressBar();? ? }? ?
?////// 將目錄下所有圖片轉(zhuǎn)成prefab
///[MenuItem("Tools/batch/batchCreateImagePrefabInPath")]? ??
public static void batchCreateImagePrefabInPath()? ? {? ? ? ??
string targetDir = Application.dataPath + TARGET_DIR;? ? ? ??
string originDir = Application.dataPath + ORIGIN_DIR;? ? ? ??
if (!Directory.Exists(originDir))? ? ? ? {? ? ? ? ? ? EditorUtility.DisplayDialog("錯(cuò)誤", originDir.Replace("\\", "/") + "目錄不存在", "確定");? ? ? ? ? ? return;? ? ? ? }? ? ? ??
if (!File.Exists(targetDir)) Directory.CreateDirectory(targetDir); //如果目錄不存在創(chuàng)建空的目標(biāo)目錄? ? ? ? DirectoryInfo originDirInfo = new DirectoryInfo(originDir);? ? ? ? //創(chuàng)建prefab? ? ? ? makeImagePrefabs(originDirInfo.GetFiles("*.jpg", SearchOption.AllDirectories), targetDir);? ? ? ? makeImagePrefabs(originDirInfo.GetFiles("*.png", SearchOption.AllDirectories), targetDir);? ? ? ? EditorUtility.ClearProgressBar();? ? }? ? ////// 創(chuàng)建image的Prefabs
//////文件數(shù)據(jù)? ? ///目標(biāo)目錄? ??
private static void makeImagePrefabs(FileInfo[] files, string targetDir)? ?
?{? ? ? ? foreach (FileInfo file in files)? ? ? ??
{? ? ? ? ? ??
//獲取全路徑? ? ? ? ? ??
string allPath = file.FullName;? ? ? ? ? ??
MonoBehaviour.print(allPath);? ? ? ? ? ??
//獲取資源路徑? ? ? ? ? ??
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));? ? ? ? ? ??
MonoBehaviour.print("assetPath " + assetPath);? ? ? ? ? ??
//加載貼圖? ? ? ? ? ??
Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite;? ? ? ? ? ??
//創(chuàng)建綁定了貼圖的 GameObject 對(duì)象? ? ? ? ? ??
GameObject go = new GameObject(sprite.name);? ? ? ? ? ??
go.AddComponent().sprite = sprite;? ? ? ? ? ??
go.GetComponent().sizeDelta = new Vector2(sprite.rect.width,sprite.rect.height);? ? ? ? ? ??
EditorUtility.DisplayProgressBar("創(chuàng)建" + sprite.name, "創(chuàng)建" + sprite.name, 1f);? ? ? ? ? ?
?//獲取圖片名稱(chēng)? ? ? ? ? ??
string imageName = assetPath.Replace("Assets" + ORIGIN_DIR + "\\", "");? ? ? ? ? ?
?//去掉后綴? ? ? ? ? ??
imageName = imageName.Substring(0, imageName.IndexOf("."));? ? ? ? ? ??
//得到最終路徑? ? ? ? ? ??
string prefabPath = targetDir + "\\" + imageName + ".prefab";? ? ? ? ? ??
//得到應(yīng)用當(dāng)前目錄的路徑? ? ? ? ? ??
prefabPath = prefabPath.Substring(prefabPath.IndexOf("Assets"));? ? ? ? ? ??
//創(chuàng)建目錄? ? ? ? ? ??
Directory.CreateDirectory(prefabPath.Substring(0, prefabPath.LastIndexOf("\\")));? ? ? ? ? ?
?//生成預(yù)制件? ? ? ? ? ??
PrefabUtility.CreatePrefab(prefabPath.Replace("\\", "/"), go);? ? ? ? ? ??
//銷(xiāo)毀對(duì)象? ? ? ? ? ??
GameObject.DestroyImmediate(go);? ? ? ? }? ? ? ??
EditorUtility.ClearProgressBar();? ? }? ??
////// 創(chuàng)建sprite的Prefabs
//////文件數(shù)據(jù)? ? ///目標(biāo)目錄? ?
?private static void makeSpritePrefabs(FileInfo[] files, string targetDir)? ? {? ? ? ?
?foreach (FileInfo file in files)? ? ? ? {? ? ? ? ? ??
//獲取全路徑? ? ? ? ? ??
string allPath = file.FullName;? ? ? ? ? ??
MonoBehaviour.print(allPath);? ? ? ? ? ?
?//獲取資源路徑? ? ? ? ? ??
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));? ? ? ? ? ??
MonoBehaviour.print("assetPath " + assetPath);? ? ? ? ? ??
//加載貼圖? ? ? ? ? ??
Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite;? ? ? ? ? ??
//創(chuàng)建綁定了貼圖的 GameObject 對(duì)象? ? ? ? ? ??
GameObject go = new GameObject(sprite.name);? ? ? ? ? ??
go.AddComponent().sprite = sprite;
EditorUtility.DisplayProgressBar("創(chuàng)建" + sprite.name, "創(chuàng)建" + sprite.name, 1f);
//獲取圖片名稱(chēng)
string imageName = assetPath.Replace("Assets" + ORIGIN_DIR + "\\", "");
//去掉后綴
imageName = imageName.Substring(0, imageName.IndexOf("."));
//得到最終路徑
string prefabPath = targetDir + "\\" + imageName + ".prefab";
//得到應(yīng)用當(dāng)前目錄的路徑
prefabPath = prefabPath.Substring(prefabPath.IndexOf("Assets"));
//創(chuàng)建目錄
Directory.CreateDirectory(prefabPath.Substring(0, prefabPath.LastIndexOf("\\")));
//生成預(yù)制件
PrefabUtility.CreatePrefab(prefabPath.Replace("\\", "/"), go);
//銷(xiāo)毀對(duì)象
GameObject.DestroyImmediate(go);
}
EditorUtility.ClearProgressBar();
}
}
#endif