Sprites,Editor使圖片生成prefab
阿新 • • 發佈:2018-11-17
/// <summary>
/// 使表情生成prefab
/// </summary>
[MenuItem("SpritesPacker/_使互動表情生成prefabs-別亂點")]
public static void MakeStickerAtlas()
{
UnityEngine.Debug.Log("碎圖 表情--用時再開啟");
return;//用時再開啟
string parentPath = Application.dataPath + "/Resources/Prefab/Stickers" ;
if (Directory.Exists(parentPath) == false)
Directory.CreateDirectory(parentPath);
//圖片原路徑
DirectoryInfo uiStickers = new DirectoryInfo(Application.dataPath + "/Art/UI/Stickers");
foreach (DirectoryInfo item in uiStickers.GetDirectories())//可以有子資料夾
{
string tempPath = parentPath + "/" + Path.GetFileName(item.FullName);
if (Directory.Exists(parentPath + Path.GetFileName(item.FullName)) == false)
{//prefab生成在該目錄中
Directory.CreateDirectory(parentPath + "/" + Path.GetFileName(item.FullName));
}
foreach (FileInfo pngItem in item.GetFiles("*.png", SearchOption.AllDirectories))
{
string allPath = pngItem.FullName;
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
GameObject go = new GameObject(sprite.name);
go.AddComponent<SpriteRenderer>().sprite = sprite;
allPath = tempPath + "/" + sprite.name + ".prefab";
string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));
PrefabUtility.CreatePrefab(prefabPath, go);
GameObject.DestroyImmediate(go);
}
}
}