纯代码 没什么好说的
using System.IO;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.U2D;
namespace Framework
{
/// <summary>
/// 图集生成
/// </summary>
public class BuildSpriteAtlas : Editor
{
/// <summary>
/// 图集磁盘路径
/// </summary>
public static string SpritePath = Application.dataPath + "/GameData/Art/Sprite";
[MenuItem("YSFramework/Build SpriteAtlas")]
public static void BuildAtlas()
{
Debug.Log("Start Build SpriteAtlas Path=" + SpritePath);
var pathDir = new DirectoryInfo(SpritePath);
//循环目录下的文件和子目录 但是不包含子目录下的文件和子目录
foreach (var item in pathDir.GetFileSystemInfos("*.*", SearchOption.TopDirectoryOnly))
{
var parentDir = item as DirectoryInfo;
//空值就代表它是文件
if (parentDir != null)
{
var file = new FileInfo(parentDir.FullName + "/" + parentDir.Name + ".spriteatlas");
if (file.Exists)
{
File.Delete(file.FullName);
}
var atlas = new SpriteAtlas();// 设置参数 可根据项目具体情况进行设置
SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
{
blockOffset = 1,
enableRotation = false,
enableTightPacking = false,
padding = 2,
};
atlas.SetPackingSettings(packSetting);
SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
{
readable = false,
generateMipMaps = false,
sRGB = true,
filterMode = FilterMode.Bilinear,
};
atlas.SetTextureSettings(textureSetting);
TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
{
maxTextureSize = 2048,
format = TextureImporterFormat.Automatic,
crunchedCompression = true,
textureCompression = TextureImporterCompression.Compressed,
compressionQuality = 50,
};
atlas.SetPlatformSettings(platformSetting);
var assetPath = "Assets/GameData/Art/Sprite/";
AssetDatabase.CreateAsset(atlas, assetPath + parentDir.Name + "/" + file.Name);
// 2、添加文件夹
Object obj = AssetDatabase.LoadAssetAtPath(assetPath + parentDir.Name, typeof(Object));
atlas.Add(new[] { obj });
AssetDatabase.SaveAssets();
}
}
Debug.Log("Build SpriteAtlas Success!");
//回收资源
System.GC.Collect();
//刷新编辑器
AssetDatabase.Refresh();
}
}
}