本篇文章实现了三个功能:①批量修改图片资源的长宽以满足%4②批量启用Crunch Compression③设置TextureType为default并关闭MipMaps
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Collections.Generic;
using System.Drawing;
public class ImportTextureSetting
{
public class image_cls{
public string new_path = "";
public string old_path = "";
public int max_size = 2048;
public bool chg_size = false;
public bool is_have = true;
public image_cls(string p1, string p2)
{
new_path = p1;
old_path = p2;
}
}
static List<image_cls> change_list = new List<image_cls>();
static string temp_dir = Application.dataPath + "/temp_NewImg/";
static string[] ImageType = new string[]{".bmp",".jpg",".png"};
static void AddFileList(List<string> filePaths, string path)
{
for(int j = 0; j < ImageType.Length; ++j)
{
if (ImageType[j] == Path.GetExtension(path))
{
//排除一些不需要进行尺寸调整的图片资源,比如遮罩,背景等等
if (Path.GetFileNameWithoutExtension (path) != "2D_map_zhezhao"
&& Path.GetFileNameWithoutExtension (path) != "2D_map_beijing")
filePaths.Add (path);
break;
}
}
}
const string ModifyThSizeTag = "Assets/Tools/Texture Tool/修改图片尺寸(满足Unity%4)";
[MenuItem(ModifyThSizeTag, false, 62)]
public static void SetXGCCTextureTag()
{
if (Directory.Exists (temp_dir)) {
Directory.Delete (temp_dir, true);
}
Directory.CreateDirectory (temp_dir);
change_list = new List<image_cls>();
string[] assetGUIDArray = Selection.assetGUIDs;
if (assetGUIDArray.Length > 0)
{
List<string> filePaths = new List<string> ();
for(int i = 0; i < assetGUIDArray.Length; ++i)
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDArray[i]);
if (Directory.Exists(assetPath))
{
string[] folder = new string[] { assetPath };
//将文件夹下所有资源作为选择资源
string[] paths = AssetDatabase.FindAssets(null, folder);
foreach(var p in paths)
{
string ppath = AssetDatabase.GUIDToAssetPath(p);
if ( !Directory.Exists(ppath) )
{
AddFileList(filePaths, ppath);
}
}
}
else
{
AddFileList(filePaths, assetPath);
}
}
EditorUtility.DisplayProgressBar("1.YSTextureTag ", "YSTextureTag...", 0);
var count = 0;
for (int i = 0; i < filePaths.Count; i++) {
var filePath = filePaths [i];
count++;
EditorUtility.DisplayProgressBar("1.YSTextureTag " + "(" + count + "/" + filePaths.Count + ")", filePath, count / (float)filePaths.Count);
int width = 0;
int height = 0;
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(filePath);
width = texture2D.width;
height = texture2D.height;
// Debug.Log(width + ":" + height + "path = " + filePath);
int max_s = width;
int min_s = height;
if (max_s >= 32 || min_s >= 32) {
bool is_jh = false;
if (max_s < height) {
is_jh = true;
min_s = width;
max_s = height;
}
var name = Path.GetFileNameWithoutExtension (filePath);
string new_path = temp_dir + name + ".png";
image_cls img_data = new image_cls(new_path, filePath);
change_list.Add(img_data);
if (max_s % 4 != 0 && min_s % 4 == 0)
{
if (max_s % 4 < 2)
{
max_s = max_s - max_s % 4;
}
else
{
max_s = max_s + 4 - max_s % 4;
}
img_data.chg_size = true;
ChangeImage(new_path, filePath, min_s, max_s, is_jh, img_data);
}
else if (max_s % 4 == 0 && min_s % 4 != 0)
{
if (min_s % 4 < 2)
{
min_s = min_s - min_s % 4;
}
else
{
min_s = min_s + 4 - min_s % 4;
}
img_data.chg_size = true;
ChangeImage(new_path, filePath, min_s, max_s, is_jh, img_data);
}
else if (max_s % 4 != 0 && min_s % 4 != 0)
{
if (min_s % 4 < 2)
{
min_s = min_s - min_s % 4;
max_s = max_s - max_s % 4;
}
else
{
min_s = min_s + 4 - min_s % 4;
max_s = max_s + 4 - max_s % 4;
}
img_data.chg_size = true;
ChangeImage(new_path, filePath, min_s, max_s, is_jh, img_data);
}
}
}
EditorUtility.ClearProgressBar();
EditorUtility.DisplayProgressBar("2.File Copy ", "2.File Copy ...", 0);
count = 0;
foreach (image_cls pp in change_list) {
count++;
EditorUtility.DisplayProgressBar("2.File Copy " + "(" + count + "/" + change_list.Count + ")", pp.old_path, count / (float)change_list.Count);
if (pp.is_have && pp.chg_size) {
System.IO.File.Copy(pp.new_path, pp.old_path, true);
AssetDatabase.ImportAsset(pp.old_path);
}
}
if (Directory.Exists (temp_dir)) {
Directory.Delete (temp_dir, true);
}
AssetDatabase.Refresh();
//count = 0;
//EditorUtility.DisplayProgressBar("3.Set Image ", "3.Set Image ..." , 0);
//foreach (image_cls pp in change_list) {
// count++;
// EditorUtility.DisplayProgressBar("3.Set Image " + "(" + count + "/" + change_list.Count + ")" , pp.old_path, count / (float)change_list.Count);
// if (pp.is_have && pp.max_size != null ) {
// TextureImporter textureImporter = AssetImporter.GetAtPath(pp.old_path) as TextureImporter;
// TextureImporterPlatformSettings platformSettings = textureImporter.GetDefaultPlatformTextureSettings();
// platformSettings.maxTextureSize = pp.max_size;
// textureImporter.SetPlatformTextureSettings(platformSettings);
// AssetDatabase.ImportAsset(pp.old_path);
// }
//}
EditorUtility.ClearProgressBar();
}
}
static public System.Drawing.Image GetFile(string path)
{
FileStream stream = File.OpenRead(path);
int fileLength = 0;
fileLength = (int)stream.Length;
Byte[] image = new Byte[fileLength];
stream.Read(image, 0, fileLength);
System.Drawing.Image result = System.Drawing.Image.FromStream(stream);
stream.Close();
return result;
}
static public void ChangeImage(string new_path, string old_path, int min_s, int max_s, bool is_jh, image_cls img_cls)
{
Debug.Log("min_s = " + min_s + " max_s = " + max_s);
System.Drawing.Image img = GetFile(old_path);
Size s;
if (is_jh) {
s = new Size(min_s, max_s);
}
else {
s = new Size(max_s, min_s);
}
try
{
Bitmap bit = new Bitmap(img, s);
SaveImage(bit, new_path, old_path);
// var palette = bit.Palette;
}
catch
{
img_cls.is_have = false;
Debug.LogError("old_path=" + old_path);
}
}
static public void SaveImage(Bitmap bit, string new_path, string old_path)
{
bit.Save(new_path);
bit.Dispose();
//Debug.Log("大小调整:" + old_path);
AssetDatabase.ImportAsset(new_path);
}
const string UseCrunchCompressionTag = "Assets/Tools/Texture Tool/启用CrunchCompression";
[MenuItem(UseCrunchCompressionTag, false, 63)]
public static void UseCrunchCompression()
{
string[] assetGUIDArray = Selection.assetGUIDs;
if (assetGUIDArray.Length > 0)
{
List<string> filePaths = new List<string>();
for (int i = 0; i < assetGUIDArray.Length; ++i)
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDArray[i]);
if (Directory.Exists(assetPath))
{
string[] folder = new string[] { assetPath };
//将文件夹下所有资源作为选择资源
string[] paths = AssetDatabase.FindAssets(null, folder);
foreach (var p in paths)
{
string ppath = AssetDatabase.GUIDToAssetPath(p);
if (!Directory.Exists(ppath))
{
AddFileList(filePaths, ppath);
}
}
}
else
{
AddFileList(filePaths, assetPath);
}
}
var count = 0;
EditorUtility.DisplayProgressBar("3.Set Image ", "3.Set Image ...", 0);
foreach (string path in filePaths)
{
count++;
EditorUtility.DisplayProgressBar("3.Set Image " + "(" + count + "/" + filePaths.Count + ")", path, count / (float)filePaths.Count);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
TextureImporterPlatformSettings platformSettings = textureImporter.GetDefaultPlatformTextureSettings();
platformSettings.crunchedCompression = true;
platformSettings.compressionQuality = 100;
textureImporter.SetPlatformTextureSettings(platformSettings);
AssetDatabase.ImportAsset(path);
}
EditorUtility.ClearProgressBar();
}
}
const string CloseMipMapTag = "Assets/Tools/Texture Tool/设置default并关闭MipMaps";
[MenuItem(CloseMipMapTag, false, 63)]
public static void CloseMipMap()
{
string[] assetGUIDArray = Selection.assetGUIDs;
if (assetGUIDArray.Length > 0)
{
List<string> filePaths = new List<string>();
for (int i = 0; i < assetGUIDArray.Length; ++i)
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDArray[i]);
if (Directory.Exists(assetPath))
{
string[] folder = new string[] { assetPath };
//将文件夹下所有资源作为选择资源
string[] paths = AssetDatabase.FindAssets(null, folder);
foreach (var p in paths)
{
string ppath = AssetDatabase.GUIDToAssetPath(p);
if (!Directory.Exists(ppath))
{
AddFileList(filePaths, ppath);
}
}
}
else
{
AddFileList(filePaths, assetPath);
}
}
var count = 0;
EditorUtility.DisplayProgressBar("3.Set Image ", "3.Set Image ...", 0);
foreach (string path in filePaths)
{
count++;
EditorUtility.DisplayProgressBar("3.Set Image " + "(" + count + "/" + filePaths.Count + ")", path, count / (float)filePaths.Count);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.textureType = TextureImporterType.Default;
textureImporter.mipmapEnabled = false;
AssetDatabase.ImportAsset(path);
}
EditorUtility.ClearProgressBar();
}
}
}
添加代码后,就可以看到如下图所示:①修改图片尺寸(满足Unity%4)②启用CrunchCompression③设置default并关闭MipMaps
如果有以下报错,那就需要添加System.Drawing.dll (此.dll在Unity安装目录的\lib\mono\unityjit下)
The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
The type or namespace name `Bitmap' could not be found. Are you missing an assembly reference?