using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEditor;
|
using System.IO;
|
using System;
|
using UnityEditor.U2D;
|
using UnityEngine.U2D;
|
|
|
public class UpdateSpritePackingSetting
|
{
|
static string rootPath = Application.dataPath + "/ResourcesOut/Sprite";
|
static string spriteRelativePath = "Assets/ResourcesOut/Sprite";
|
|
[MenuItem("程序/Sprite/标准化Sprite设置")]
|
public static void UpdateAllSpritePackingSetting()
|
{
|
var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
|
var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
|
|
if (so == null)
|
{
|
return;
|
}
|
|
foreach (var setting in so.spriteSettings)
|
{
|
var directoryPath = StringUtility.Contact(rootPath, Path.DirectorySeparatorChar, setting.folderName);
|
|
if (!Directory.Exists(directoryPath))
|
{
|
continue;
|
}
|
|
var files = new DirectoryInfo(directoryPath).GetFiles("*.png", SearchOption.TopDirectoryOnly);
|
|
var total = files.Length;
|
var count = 0;
|
|
|
|
foreach (var file in files)
|
{
|
try
|
{
|
var folderName = setting.folderName;
|
var importerPath = spriteRelativePath + "/" + folderName + "/" + file.Name;
|
var importer = AssetImporter.GetAtPath(importerPath);
|
|
if (importer == null)
|
{
|
continue;
|
}
|
|
var textureImporter = importer as TextureImporter;
|
if (textureImporter == null)
|
{
|
continue;
|
}
|
bool isChange = false;
|
if (textureImporter.textureType != TextureImporterType.Sprite)
|
{
|
textureImporter.textureType = TextureImporterType.Sprite;
|
isChange = true;
|
}
|
|
if (textureImporter.spriteImportMode != SpriteImportMode.Single)
|
{
|
textureImporter.spriteImportMode = SpriteImportMode.Single;
|
isChange = true;
|
}
|
|
if (isChange)
|
{
|
AssetDatabase.ImportAsset(importerPath);
|
EditorUtility.SetDirty(textureImporter);
|
}
|
|
count++;
|
|
EditorUtility.DisplayProgressBar(StringUtility.Contact("设置图集标签:", setting.folderName), file.Name, count / (float)total);
|
}
|
catch (Exception ex)
|
{
|
Debug.Log(ex);
|
EditorUtility.ClearProgressBar();
|
}
|
}
|
|
}
|
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
|
EditorUtility.ClearProgressBar();
|
|
SpritePack();
|
}
|
|
[MenuItem("程序/Sprite/打包BuiltIn图集")]
|
public static void SpritePack_BuiltIn()
|
{
|
string rootPath_BuiltIn = Application.dataPath + "/ResourcesOut/BuiltIn/Sprites";
|
string spriteRelativePath_BuiltIn = "Assets/ResourcesOut/BuiltIn/Sprites";
|
var builtInSpritePath = spriteRelativePath_BuiltIn + "/sprites.spriteatlasv2";
|
SpriteAtlasAsset spriteAtlas = SpriteAtlasAsset.Load(builtInSpritePath);
|
|
// 不存在则创建后更新图集
|
if (spriteAtlas == null)
|
{
|
var setting = new SpriteSettingConfig.SpriteSetting()
|
{
|
folderName = "sprites",
|
blockOffset = 1,
|
enableRotation = false,
|
enableTightPacking = false,
|
padding = 4,
|
readable = false,
|
generateMipMaps = false,
|
sRGB = true,
|
filterMode = FilterMode.Bilinear,
|
platformSettings = new SpriteSettingConfig.PlatformSetting[]
|
{
|
new SpriteSettingConfig.PlatformSetting()
|
{
|
name = "Android",
|
maxTextureSize = 2048,
|
textureCompression = TextureImporterCompression.Compressed,
|
textureFormat = TextureImporterFormat.ASTC_6x6
|
},
|
new SpriteSettingConfig.PlatformSetting()
|
{
|
name = "iPhone",
|
maxTextureSize = 2048,
|
textureCompression = TextureImporterCompression.Compressed,
|
textureFormat = TextureImporterFormat.ASTC_6x6
|
}
|
}
|
};
|
spriteAtlas = CreateSpriteAtlas(spriteRelativePath_BuiltIn, setting);
|
}
|
List<string> excludePngs = new List<string>() { "Launch_1.png", "Launch_2.png", "Launch_3.png", "LoginBackGround.png", "TB_DL_Logo.png" };
|
|
SpriteAtlas spriteAtlasTmp = AssetDatabase.LoadAssetAtPath(builtInSpritePath, typeof(UnityEngine.Object)) as SpriteAtlas;
|
UpdateAtlas(spriteAtlas, rootPath_BuiltIn, excludePngs, builtInSpritePath);
|
|
AssetDatabase.Refresh();
|
|
Debug.Log("打包BuiltIn图集执行结束");
|
}
|
|
[MenuItem("程序/Sprite/打包图集")]
|
public static void SpritePack()
|
{
|
var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
|
var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
|
|
if (so == null)
|
{
|
return;
|
}
|
|
int index = 0;
|
foreach (SpriteSettingConfig.SpriteSetting setting in so.spriteSettings)
|
{
|
var directoryPath = StringUtility.Contact(rootPath, Path.DirectorySeparatorChar, setting.folderName);
|
|
if (!Directory.Exists(directoryPath))
|
{
|
continue;
|
}
|
|
|
spritefilePathName = setting.folderName;
|
|
var filePath = spriteRelativePath + "/" + spritefilePathName + ".spriteatlasv2";
|
SpriteAtlasAsset spriteAtlas = SpriteAtlasAsset.Load(filePath);
|
|
// 不存在则创建后更新图集
|
if (spriteAtlas == null)
|
{
|
spriteAtlas = CreateSpriteAtlas(spriteRelativePath, setting);
|
}
|
|
string spriteFilePath = rootPath + "/" + spritefilePathName;
|
|
|
UpdateAtlas(spriteAtlas, spriteFilePath, new List<string>(), filePath);
|
|
// 打包进度
|
EditorUtility.DisplayProgressBar("打包图集中...", "正在处理:" + spritefilePathName, index / so.spriteSettings.Length);
|
index++;
|
}
|
|
EditorUtility.ClearProgressBar();
|
AssetDatabase.Refresh();
|
UpdateSpriteSetting.SetAllSpriteAssetBundleName();
|
|
Debug.Log("打包图集执行结束");
|
|
}
|
|
|
|
/// <summary>
|
/// 每个需要打图集的文件夹名 -- 即图集名
|
/// </summary>
|
private static string spritefilePathName;
|
|
/// <summary>
|
/// 创建图集
|
/// </summary>
|
/// <param name="atlasName">图集名字</param>
|
private static SpriteAtlasAsset CreateSpriteAtlas(string outPath, SpriteSettingConfig.SpriteSetting setting)
|
{
|
string atlasPath = outPath + "/" + setting.folderName + ".spriteatlasv2";
|
CreateEmptySpriteAtlas(atlasPath);
|
|
|
SpriteAtlasImporter atlasImporter = SpriteAtlasImporter.GetAtPath(atlasPath) as SpriteAtlasImporter;
|
|
#region 图集基础设置
|
|
SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
|
{
|
blockOffset = setting.blockOffset,
|
enableRotation = setting.enableRotation,
|
enableTightPacking = setting.enableTightPacking,
|
padding = setting.padding,
|
};
|
atlasImporter.packingSettings = packSetting;
|
|
#endregion
|
|
#region 图集纹理设置
|
|
SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings()
|
{
|
readable = setting.readable,
|
generateMipMaps = setting.generateMipMaps,
|
sRGB = setting.sRGB,
|
filterMode = setting.filterMode,
|
};
|
atlasImporter.textureSettings = textureSettings;
|
|
#endregion
|
|
#region 分平台设置图集格式
|
|
foreach (SpriteSettingConfig.PlatformSetting platformSetting in setting.platformSettings)
|
{
|
TextureImporterPlatformSettings settingPlatform = atlasImporter.GetPlatformSettings(platformSetting.name);
|
settingPlatform.overridden = true;
|
settingPlatform.maxTextureSize = platformSetting.maxTextureSize;
|
settingPlatform.textureCompression = platformSetting.textureCompression;
|
settingPlatform.format = platformSetting.textureFormat;
|
atlasImporter.SetPlatformSettings(settingPlatform);
|
}
|
|
#endregion
|
atlasImporter.includeInBuild = true;
|
atlasImporter.SaveAndReimport();
|
//atlas.Add(objectsForPack);
|
SpriteAtlasAsset atlas = SpriteAtlasAsset.Load(atlasPath);
|
|
|
return atlas;
|
}
|
|
static void CreateEmptySpriteAtlas(string atlasPath)
|
{
|
SpriteAtlasAsset.Save(new SpriteAtlasAsset(), atlasPath);
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
}
|
|
|
|
/// <summary>
|
/// 每个图集的所有图片路径 -- 记得用之前清空
|
/// </summary>
|
private static List<string> textureFullName = new List<string>();
|
|
/// <summary>
|
/// 更新图集内容
|
/// </summary>
|
/// <param name="atlas">图集</param>
|
static void UpdateAtlas(SpriteAtlasAsset atlas, string spriteFilePath, List<string> excludePng, string filePath)
|
{
|
textureFullName.Clear();
|
FileName(spriteFilePath, excludePng);
|
|
SpriteAtlas spriteAtlasTmp = AssetDatabase.LoadAssetAtPath(filePath, typeof(UnityEngine.Object)) as SpriteAtlas;
|
|
// 获取图集下图片
|
List<UnityEngine.Object> packables = new List<UnityEngine.Object>(spriteAtlasTmp.GetPackables());
|
|
List<UnityEngine.Object> objectsForPack = new List<UnityEngine.Object>();
|
foreach (string item in textureFullName)
|
{
|
// 加载指定目录
|
UnityEngine.Object spriteObj = AssetDatabase.LoadAssetAtPath(item, typeof(UnityEngine.Object));
|
//Debug.Log("存png和jpg后缀的图片: " + item + " , " + !packables.Contains(spriteObj));
|
if (!packables.Contains(spriteObj))
|
{
|
atlas.Add(new UnityEngine.Object[] { spriteObj });
|
}
|
objectsForPack.Add(spriteObj);
|
}
|
//删除packables下不存在的图片
|
foreach (var item in packables)
|
{
|
if (!objectsForPack.Contains(item))
|
{
|
atlas.Remove(new UnityEngine.Object[] { item });
|
Debug.Log(filePath + " 删除不存在的图片: " + item.name);
|
}
|
}
|
|
|
SpriteAtlasAsset.Save(atlas, filePath);
|
}
|
|
/// <summary>
|
/// 递归文件夹下的图
|
/// </summary>
|
/// <param name="folderPath"></param>
|
static void FileName(string folderPath, List<string> excludePng)
|
{
|
DirectoryInfo info = new DirectoryInfo(folderPath);
|
string str = Application.dataPath.Replace(@"/", @"\");
|
|
foreach (DirectoryInfo item in info.GetDirectories())
|
{
|
FileName(item.FullName, excludePng);
|
}
|
foreach (FileInfo item in info.GetFiles())
|
{
|
// 存png和jpg后缀的图片
|
if (item.FullName.EndsWith(".png", StringComparison.Ordinal) && !excludePng.Contains(item.Name))
|
{
|
textureFullName.Add("Assets" + item.FullName.Replace(str, ""));
|
}
|
if (item.FullName.EndsWith(".PNG", StringComparison.Ordinal))
|
{
|
Debug.LogError("请将图片后缀改为小写: " + item.FullName);
|
}
|
}
|
}
|
|
|
|
}
|