檢測圖片,修改圖片格式
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
public class TextureCheck {
[MenuItem("檢查圖片/檢查文件夾貼圖不符合的")]
static void 檢查是否有真彩貼圖()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.LogError("選擇一個文件夾");
return;
}
// StringBuilder text = new StringBuilder();
List<string> imageList = new List<string>();
List<string> errorList = new List<string>();
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
if (textureImporter.textureType == TextureImporterType.Sprite)
continue;
//查看貼圖類型
if (textureImporter.textureType == TextureImporterType.Image || textureImporter.textureType == TextureImporterType.Cursor)
{
Debug.Log("類型不符合:" + assetPath + ",現在類型是:" + textureImporter.textureType);
errorList.Add("類型不符合: " + ",現在類型是:" + textureImporter.textureType);
imageList.Add(assetPath);
continue;
}
//查看是否開啓了minmap
if (textureImporter.mipmapEnabled)
{
Debug.Log( assetPath + ",該圖片開啓了minmap");
errorList.Add(",該圖片開啓了minmap");
imageList.Add(assetPath);
continue;
}
//查看壓縮格式
int maxsize = 0;
TextureImporterFormat textureImporterFormat;
string androidPlatform = "Android";
if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
{
if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC_RGB4)//不帶alpha通道的
{
Debug.Log("格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat);
imageList.Add(assetPath);
errorList.Add("格式不符合: " + ",現在格式是:" + textureImporterFormat);
}
if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC2_RGBA8)//帶alpha通道的
{
Debug.Log("格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat);
imageList.Add(assetPath);
errorList.Add("格式不符合: " + ",現在格式是:" + textureImporterFormat);
}
}
//查看壓縮格式
int maxsize2 = 0;
TextureImporterFormat textureImporterFormat2;
string iosPlatform = "iPhone";
if (textureImporter.GetPlatformTextureSettings(iosPlatform, out maxsize2, out textureImporterFormat2))
{
if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat2 != TextureImporterFormat.PVRTC_RGB4)//不帶alpha通道的
{
Debug.Log("IOS格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat2);
imageList.Add(assetPath);
errorList.Add("IOS格式不符合: " + ",現在格式是:" + textureImporterFormat2);
}
if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat2 != TextureImporterFormat.PVRTC_RGBA4)//帶alpha通道的
{
Debug.Log("IOS格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat2);
imageList.Add(assetPath);
errorList.Add("IOS格式不符合: " + ",現在格式是:" + textureImporterFormat2);
}
}
//查看是否是2的次方
Texture target = AssetDatabase.LoadAssetAtPath<Object>(assetPath) as Texture;
var type = Types.GetType("UnityEditor.TextureUtil", "UnityEditor.dll");
MethodInfo[] methodInfos = type.GetMethods();
foreach (var obj in methodInfos)
Debug.Log(obj);
MethodInfo methodInfo = type.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
string isNon = methodInfo.Invoke(null, new object[] { target }).ToString();
if (isNon.Contains("True"))
{
imageList.Add(assetPath);
errorList.Add( "圖片大小不是2的次冪");
Debug.Log(assetPath + ",圖片大小不是2的次冪:" + methodInfo.Invoke(null, new object[] { target }));
}
}
TextureCheckEditorWindow.imageList = imageList;
TextureCheckEditorWindow.errorList = errorList;
TextureCheckEditorWindow.ShowWindow();
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
// Debug.Log(text.ToString());
}
[MenuItem("檢查圖片/轉換文件夾內Android的圖片爲etc")]
static void 轉換Android的圖片爲etc()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.Log(path);
return;
}
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
int count = 0;
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
//處理壓縮格式爲etc
int maxsize = 0;
TextureImporterFormat textureImporterFormat;
string androidPlatform = "Android";
if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
{
if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC_RGB4)//不帶alpha通道的
{
textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.ETC_RGB4);
textureImporter.SaveAndReimport();
count++;
}
if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC2_RGBA8 )//帶alpha通道的
{
textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.ETC2_RGBA8);
textureImporter.SaveAndReimport();
count++;
}
}
androidPlatform = "iPhone";
if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
{
if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.PVRTC_RGB4)//不帶alpha通道的
{
textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.PVRTC_RGB4);
textureImporter.SaveAndReimport();
count++;
}
if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.PVRTC_RGBA4)//帶alpha通道的
{
textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.PVRTC_RGBA4);
textureImporter.SaveAndReimport();
count++;
}
}
}
Debug.Log("壓縮了:" + count + "個圖片");
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
[MenuItem("檢查圖片/文件夾內圖片改爲Advanced")]
static void 文件夾內圖片改爲Advanced()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.Log(path);
return;
}
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
int count = 0;
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
if(textureImporter.textureType==TextureImporterType.Sprite)
continue;
//去掉圖片的mipmap
if (textureImporter.textureType!=TextureImporterType.Advanced)
{
textureImporter.textureType = TextureImporterType.Advanced;
textureImporter.SaveAndReimport();
count++;
}
}
Debug.Log("處理了:" + count + "個圖片");
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
[MenuItem("檢查圖片/去除文件夾內圖片的read write")]
static void 去除文件夾內圖片的readWrite()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.Log(path);
return;
}
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
int count = 0;
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
if (textureImporter.isReadable)
{
textureImporter.isReadable = false;
textureImporter.SaveAndReimport();
count++;
}
}
Debug.Log("處理了:" + count + "個圖片");
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
[MenuItem("檢查圖片/去除文件夾內圖片的mipmap")]
static void 去除文件夾內圖片的mipmap()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.Log(path);
return;
}
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
int count = 0;
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
//去掉圖片的mipmap
if (textureImporter.mipmapEnabled)
{
textureImporter.mipmapEnabled = false;
textureImporter.SaveAndReimport();
count++;
}
}
Debug.Log("處理了:" + count + "個圖片");
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
[MenuItem("檢查圖片/文件夾內圖片大小改爲2的次冪")]
static void 文件夾內圖片大小改爲2的次冪()
{
if (Selection.objects.Length != 1)
{
Debug.LogError("選擇一個文件夾");
return;
}
string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (!Directory.Exists(path))
{
Debug.Log(path);
return;
}
AssetDatabase.StartAssetEditing();
var assets = new List<string>();
int count = 0;
string[] filePath = GetFilePath(Selection.objects);
foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter == null)
continue;
//查看是否是2的次方
Texture target = AssetDatabase.LoadAssetAtPath<Object>(assetPath) as Texture;
var type = Types.GetType("UnityEditor.TextureUtil", "UnityEditor.dll");
//MethodInfo[] methodInfos = type.GetMethods();
//foreach (var obj in methodInfos)
// Debug.Log(obj);
MethodInfo methodInfo = type.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
string isNon = methodInfo.Invoke(null, new object[] { target }).ToString();
if (isNon.Contains("True"))
{
textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
textureImporter.SaveAndReimport();
count++;
}
}
Debug.Log("處理了:" + count + "個圖片");
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
public static string[] GetFilePath(Object[] targets)
{
var folders = new List<string>();
for (int i = 0; i < targets.Length; i++)
{
string assetPath = AssetDatabase.GetAssetPath(targets[i]);
if (Directory.Exists(assetPath))
folders.Add(assetPath);
}
return folders.ToArray();
}
private static void AllFuntion(TextureImporter textureImporter)
{
#if UNITY_EDITOR
var type = typeof(TextureImporterSettings).Assembly.GetType("UnityEditor.TextureImporterSettings");
var info = type.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
//MethodInfo nowAddButton = info[0];
foreach (var t in info)
{
DebugBuild.Log(t.Name);
//foreach (ParameterInfo i in t.GetParameters())
//{
// Debug.Log("方法名" + t.Name + ",屬性:" + i.ParameterType);
// if (t.Name == "GetImportWarnings")
// nowAddButton = t;
//}
}
//foreach (ParameterInfo i in nowAddButton.GetParameters())
// Debug.Log("方法名" + nowAddButton.Name + ",屬性:" + i.ParameterType);
//object[] trans = new object[] { 1,2 };
//DebugBuild.Log(nowAddButton.Invoke(textureImporter, trans));
#endif
}
}