[MenuItem("Tools/AtlasMaker")]
static private void MakeAtlas()
{
string spriteDir = Application.dataPath + "/Resources/Sprite";
string curDirPath = "";
if (Directory.Exists(spriteDir))
{
Directory.Delete(spriteDir, true);
}
Directory.CreateDirectory(spriteDir);
DirectoryInfo rootDirInfo = new DirectoryInfo(Application.dataPath + "/Atlas");
foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories())
{
foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
{
string allPath = pngFile.FullName;
string midDirPath = pngFile.DirectoryName.Substring(allPath.IndexOf("Atlas"));
midDirPath = midDirPath.Replace("\\", "/").Replace("Atlas", "");
curDirPath = spriteDir + midDirPath;
if (!Directory.Exists(curDirPath))
{
Directory.CreateDirectory(curDirPath);
}
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
if (sprite == null)
{
Debug.LogError("无法找到Sprite:" + assetPath);
continue;
}
GameObject go = new GameObject(sprite.name);
go.AddComponent<SpriteRenderer>().sprite = sprite;
PrefabUtility.CreatePrefab("Assets/Resources/Sprite/" + midDirPath + "/" + sprite.name + ".prefab", go, ReplacePrefabOptions.ConnectToPrefab);
GameObject.DestroyImmediate(go);
}
AssetDatabase.SaveAssets();
}
}
Unity UGUI 图片生成Sprite预制
最新推荐文章于 2024-06-06 17:17:13 发布