UnityEditor动态生成图集SpriteAtlas,并包含文件夹图片
//首先读取json
[MenuItem("工具/创建Atlas")]
static void AtlasCreate()
{
string rpath = "Assets/AssetAll/ps";
DirectoryInfo direction = new DirectoryInfo(rpath);
DirectoryInfo[] directs = direction.GetDirectories();//文件夹
DirectoryInfo dir;
int i, j;
for (i = 0; i < directs.Length; i++)
{
dir = directs[i];
string dataPath = dir.FullName;= System.IO.Path.GetFullPath(".");
//创建图集
string atlas = dataPath + "\\" + dir.Name + ".spriteatlas";
if (File.Exists(atlas))
{
Debug.Log("图集找到" + atlas);
}
else
{
SpriteAtlas sa = new SpriteAtlas();
SpriteAtlasPackingSettings packSet = new SpriteAtlasPackingSettings()
{
blockOffset = 1,
enableRotation = false,
enableTightPacking = false,
padding = 4,
};
sa.SetPackingSettings(packSet);
SpriteAtlasTextureSettings textureSet = new SpriteAtlasTextureSettings()
{
readable = true,
generateMipMaps = false,
sRGB = true,
filterMode = FilterMode.Bilinear,
};
sa.SetTextureSettings(textureSet);
atlas = rpath + "/" + dir.Name + "/" + dir.Name + ".spriteatlas";
AssetDatabase.CreateAsset(sa, atlas);
//图片的文件夹加入图集。
Object texture = AssetDatabase.LoadMainAssetAtPath(rpath + "/" + dir.Name);
SpriteAtlasExtensions.Add(sa, new Object[] { texture });
AssetDatabase.SaveAssets();
Debug.Log("图集创建:" + atlas);
//设置预设名称
AssetImporter atlasAssetImporter = AssetImporter.GetAtPath(atlas); //得到Asset
atlasAssetImporter.assetBundleName = dir.Name;
atlasAssetImporter.assetBundleVariant = "ps";
}
}
}