[工具]Unity Utils 圖片轉(zhuǎn)換成預設(shè)體

#if UNITY_EDITORusing System.IO;

using UnityEditor;using UnityEngine;

using UnityEngine.UI;

public class BatchCreatePrefabInPath{? ??

private const string ORIGIN_DIR = "\\Atlas"; //需要轉(zhuǎn)換的目錄(手動修改目錄)? ??

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("錯誤", originDir.Replace("\\", "/") + "目錄不存在", "確定");? ? ? ? ? ? return;? ? ? ? }? ? if (!File.Exists(targetDir)) Directory.CreateDirectory(targetDir);?

//如果目錄不存在創(chuàng)建空的目標目錄? ? ? ??

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("錯誤", originDir.Replace("\\", "/") + "目錄不存在", "確定");? ? ? ? ? ? return;? ? ? ? }? ? ? ??

if (!File.Exists(targetDir)) Directory.CreateDirectory(targetDir); //如果目錄不存在創(chuàng)建空的目標目錄? ? ? ? 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ù)? ? ///目標目錄? ??

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 對象? ? ? ? ? ??

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);? ? ? ? ? ?

?//獲取圖片名稱? ? ? ? ? ??

string imageName = assetPath.Replace("Assets" + ORIGIN_DIR + "\\", "");? ? ? ? ? ?

?//去掉后綴? ? ? ? ? ??

imageName = imageName.Substring(0, imageName.IndexOf("."));? ? ? ? ? ??

//得到最終路徑? ? ? ? ? ??

string prefabPath = targetDir + "\\" + imageName + ".prefab";? ? ? ? ? ??

//得到應用當前目錄的路徑? ? ? ? ? ??

prefabPath = prefabPath.Substring(prefabPath.IndexOf("Assets"));? ? ? ? ? ??

//創(chuàng)建目錄? ? ? ? ? ??

Directory.CreateDirectory(prefabPath.Substring(0, prefabPath.LastIndexOf("\\")));? ? ? ? ? ?

?//生成預制件? ? ? ? ? ??

PrefabUtility.CreatePrefab(prefabPath.Replace("\\", "/"), go);? ? ? ? ? ??

//銷毀對象? ? ? ? ? ??

GameObject.DestroyImmediate(go);? ? ? ? }? ? ? ??

EditorUtility.ClearProgressBar();? ? }? ??

////// 創(chuàng)建sprite的Prefabs

//////文件數(shù)據(jù)? ? ///目標目錄? ?

?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 對象? ? ? ? ? ??

GameObject go = new GameObject(sprite.name);? ? ? ? ? ??

go.AddComponent().sprite = sprite;

EditorUtility.DisplayProgressBar("創(chuàng)建" + sprite.name, "創(chuàng)建" + sprite.name, 1f);

//獲取圖片名稱

string imageName = assetPath.Replace("Assets" + ORIGIN_DIR + "\\", "");

//去掉后綴

imageName = imageName.Substring(0, imageName.IndexOf("."));

//得到最終路徑

string prefabPath = targetDir + "\\" + imageName + ".prefab";

//得到應用當前目錄的路徑

prefabPath = prefabPath.Substring(prefabPath.IndexOf("Assets"));

//創(chuàng)建目錄

Directory.CreateDirectory(prefabPath.Substring(0, prefabPath.LastIndexOf("\\")));

//生成預制件

PrefabUtility.CreatePrefab(prefabPath.Replace("\\", "/"), go);

//銷毀對象

GameObject.DestroyImmediate(go);

}

EditorUtility.ClearProgressBar();

}

}

#endif

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容