网上很多代码设置图片压缩格式的案例,都很简单,提供思路,随时百度。
本篇文章是为了满足项目需求,加了一些功能:
1】基本的,选择文件夹/某个文件,批量设置该文件夹下所有有效文件的压缩格式
2】自行选择平台以及压缩格式
3】根据是否有Alpha通道自行处理格式(RGB的格式仍然转成对应的RGB)
4】辅助功能:获取选择文件夹下/某个文件,某平台下所有文件的压缩格式并合并分析(项目中很多手误导致很多格式设置不正确)
本文涉及大部分的Unity Editor的UI绘制API,互相学习。
效果如下:
获取所有信息打印:
本文主要使用到的API一览
GUILayout.Label("请先选择一个文件夹!!! "); //绘制一行字
EditorGUILayout.EnumPopup(curTF, guiStyle); //绘制一个下拉框
EditorGUILayout.ToggleLeft("是否将RGB强制转成RGBA", isConvertRGBA); //绘制一个toggle
GUILayout.Button("开始设置") //绘制一个按钮
Selection.assetGUIDs //获取选择的资源文件的GUID,可多选
AssetDatabase.GetAssetPath(Selection.objects[0])); //获取所选第一个文件的路径
DirectoryInfo root = new DirectoryInfo(path); //根据路径创建一个目录
root.GetDirectories(); //获取该目录下的所有目录
root.GetFiles(); //获取该目录下的所有文件
//取出该路径文件的Textureimporter
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
//获取设置
var setting = importer.GetPlatformTextureSettings(((EPlatform)curPl).ToString());
//修改设置
setting.format = (TextureImporterFormat)curTF;
//应用设置
importer.SetPlatformTextureSettings(setting);
//保存修改
AssetDatabase.ImportAsset(path);
下面是可用于项目的完整代码文件,直接复制到项目中就可以使用,具体解释都加了注释。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
public enum EPlatform
{
iPhone,
Android,
}
public class SpritesEditorManager : EditorWindow {
//保存当前设置的格式
public TextureFormat curTF = TextureFormat.ASTC_RGBA_6x6, setTF;
public GUIStyle guiStyle;
//保存当前设置的平台
public EPlatform curPl, setPl;
//是否强制设置成RGBA, false : 使用原有的RGB
public bool isConvertRGBA = true;
//是否是一个文件
public bool _isFile;
new void Show()
{
//设置绘制下拉框的格式
guiStyle = new GUIStyle(EditorStyles.popup);
guiStyle.fontSize = 10;
guiStyle.fixedHeight = 20;
guiStyle.fixedWidth = 200;
}
void OnGUI()
{
if(Selection.objects.Length <= 0)
{
GUILayout.Label("请先选择一个文件夹!!! ");
}
else
{
GUILayout.Label("当前选中的文件夹: " + AssetDatabase.GetAssetPath(Selection.objects[0]));
}
GUILayout.Label("");
GUILayout.Label("设置平台: ");
setPl = (EPlatform)EditorGUILayout.EnumPopup(curPl, guiStyle);
GUILayout.Label("");
GUILayout.Label("设置格式: ");
setTF = (TextureFormat)EditorGUILayout.EnumPopup(curTF, guiStyle);
isConvertRGBA = EditorGUILayout.ToggleLeft("是否将RGB强制转成RGBA", isConvertRGBA);
GUILayout.Label("");
PrintSetting();
if (GUILayout.Button("开始设置"))
{
if(!CheckSelection())
return;
ParseTexture2DFormat(true);
Debug.Log("完成");
}
GUILayout.Label("");
if(GUILayout.Button("获取当前文件夹下所有文件当前平台的压缩格式"))
{
if(!CheckSelection())
return;
allSettings = new Dictionary<string, List<string>>();
ParseTexture2DFormat(false);
//打印日志
foreach(var dic in allSettings)
{
StringBuilder sb = new StringBuilder();
foreach(var path in dic.Value)
{
// Debug.LogFormat("path: {0}", path);
sb.Append(path);
sb.Append("\n");
}
Debug.LogFormat("<color=#ff0000>类型{0}累计{1}个</color>\n{2}", dic.Key, dic.Value.Count, sb.ToString());
sb.Remove(0, sb.Length);
}
allSettings.Clear();
Debug.Log("完成");
}
}
[MenuItem("编辑助手/设置图片压缩格式(整个文件夹)")]
static void SetTextureFormat()
{
EditorWindow.GetWindow<SpritesEditorManager>(false, "设置", true).Show();
}
//@isFix: 是否修改(false : 只打印信息)
public void ParseTexture2DFormat(bool isFix)
{
string[] guidArray = Selection.assetGUIDs;
foreach (var item in guidArray)
{
string selectFloder = AssetDatabase.GUIDToAssetPath(item);
Debug.LogFormat("<color=#ff0000>parse selectFloder: {0} </color>", selectFloder);
DirectoryInfo root = new DirectoryInfo(selectFloder);
ParseFloder(root, isFix);
}
}
public void ParseFloder(DirectoryInfo root, bool isFix)
{
ParseFile(root, isFix);
//查找子文件夹, 这里如果不判断的话,单个文件会进入死循环
if(!_isFile)
{
DirectoryInfo[] array = root.GetDirectories();
foreach (DirectoryInfo item in array)
{
ParseFloder(item, isFix);
}
}
}
public void ParseFile(DirectoryInfo root, bool isFix)
{
// Debug.Log("-------------root.FullName: " + root.FullName);
//DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] fileDic = root.GetFiles();
foreach (var file in fileDic)
{
if (file.FullName.EndsWith(".png") || file.FullName.EndsWith(".jpg") || file.FullName.EndsWith(".tga") ||
file.FullName.EndsWith(".psd") || file.FullName.EndsWith(".PSD") || file.FullName.EndsWith(".exr") ||
file.FullName.EndsWith(".tif"))
{
// Debug.Log("-------------file.FullName: " + file.FullName);
// Debug.Log(Application.dataPath);
// Debug.Log(Application.dataPath.Replace("Assets",""));
string parseName = file.FullName.Replace('\\', '/').Replace(Application.dataPath.Replace("Assets",""),"");
if(isFix)
SetPicFormat(parseName);
else
PrintPicFormat(parseName);
}
}
}
// private TextureImporterPlatformSettings tips6x6 = new TextureImporterPlatformSettings();
//设置图片格式
public void SetPicFormat(string path)
{
//path 要相对于Asset的相对路径
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
// if (importer.mipmapEnabled == true)
// {
// importer.mipmapEnabled = false;
// }
var setting = importer.GetPlatformTextureSettings(((EPlatform)curPl).ToString());
//判断图片大小
// Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
// if(texture == null)
// Debug.LogError("texture == null");
// int textureSize = Mathf.Max(texture.height, texture.width);
// //Debug.Log(textureSize);
// int SizeType = FitSize(textureSize);
//强制转换的设置完退出
if(isConvertRGBA)
{
setting.format = (TextureImporterFormat)curTF;
importer.SetPlatformTextureSettings(setting);
Debug.LogFormat("<<<Set ConvertRGBA path{0}, format: {1}: ", path, curTF);
//保存设置
AssetDatabase.ImportAsset(path);
return;
}
//判断透明通道
if(importer.DoesSourceTextureHaveAlpha())
{
setting.format = (TextureImporterFormat)curTF;
importer.SetPlatformTextureSettings(setting);
AssetDatabase.ImportAsset(path);
}
else
{
setting.format = ConverFormat((TextureImporterFormat)curTF);
importer.SetPlatformTextureSettings(setting);
AssetDatabase.ImportAsset(path);
}
Debug.LogFormat("<<<Set path{0}, format: {1}: ", path, setting.format);
}
//<格式, List<路径>>
Dictionary<string, List<string>> allSettings;
public void PrintPicFormat(string path)
{
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
var setting = importer.GetPlatformTextureSettings(((EPlatform)curPl).ToString());
string formatName = setting.format.ToString();
// Debug.LogFormat("path: {0}, format:{1}", path, formatName);
if(allSettings.ContainsKey(formatName))
allSettings[formatName].Add(path);
else
{
allSettings[formatName] = new List<string>();
allSettings[formatName].Add(path);
}
}
static int[] formatSize = new int[]{32,64,128,256,512,1024,2048};
static int FitSize(int picValue)
{
foreach (var one in formatSize)
{
if (picValue <= one)
{
return one;
}
}
return 1024;
}
public bool CheckSelection()
{
if(Selection.objects.Length <= 0)
{
Debug.LogError("请先选择一个文件!!! ");
return false;
}
_isFile = false;
string selectPath = AssetDatabase.GetAssetPath(Selection.objects[0]);
if(File.GetAttributes(selectPath).CompareTo(FileAttributes.Directory) == 1)
{
Debug.LogError("选择了一个文件!!!");
_isFile = true;
}
return true;
}
//格式转换
public TextureImporterFormat ConverFormat(TextureImporterFormat oriFormat)
{
switch(oriFormat)
{
case TextureImporterFormat.ASTC_RGBA_4x4:
return TextureImporterFormat.ASTC_RGB_4x4;
case TextureImporterFormat.ASTC_RGBA_6x6:
return TextureImporterFormat.ASTC_RGB_6x6;
case TextureImporterFormat.ASTC_RGBA_8x8:
return TextureImporterFormat.ASTC_RGB_8x8;
default:
return oriFormat;
}
}
public void PrintSetting()
{
if(setPl != curPl)
{
curPl = setPl;
Debug.LogError("当前平台:" + curPl);
}
if(setTF != curTF)
{
curTF = setTF;
Debug.LogError("当前格式" + curTF);
}
}
}