需求:需要针对texture做修改,首先需要找到所有大于1024的图集;针对texture的部分参数做处理;并都加上白名单
分析:俩个功能单独做,都需要针对特定的文件夹做搜索,然后要对白名单进行过滤,第一个拿到对象然后进行尺寸的判定并返回就可以了;第二个是过滤掉之后并对参数进行修改,然后保存。
逻辑不复杂,直接上代码,大体逻辑和之前的audio文件的优化差不多,感兴趣的话可以看看上一篇
直接上代码了
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class ModifyAllTextureSetting : EditorWindow
{
public static string FilePath = "Assets";
public static List<string> allTexture = new List<string>();//所有的Texture
public static List<string> all1024Texture = new List<string>(); //大于1024尺寸的图片
public static List<string> allModifyTexture = new List<string>(); //所有需要修改的图片
Vector2 mScroll = Vector2.zero;
//需要修改参数的文件夹
static string[] modifySettingPath = new string[]{
"Assets/MeiShuZiYuan/Atlas",
"Assets/MeiShuZiYuan/Font",
"Assets/MeiShuZiYuan_iOS/Atlas",
"Assets/MeiShuZiYuan_iOS/Font",
};
//1024图片白名单
static string[] whiteList1024 = new string[]{
"Assets/MeiShuZiYuan/Atlas",
"Assets/MeiShuZiYuan/Font",
"Assets/BundleFiles",
};
//修改图片白名单
static string[] whiteListDefaultSetting = new string[]{
"Assets/MeiShuZiYuan_iOS/Atlas",
"Assets/MeiShuZiYuan_iOS/Font",
"Assets/MeiShuZiYuan/Font",
"Atlas_Activities",
};
void OnGUI()
{
if(all1024Texture == null)
{
return;
}
mScroll = GUILayout.BeginScrollView(mScroll);
if(all1024Texture != null && all1024Texture.Count>0)
{
if(NGUIEditorTools.DrawHeader("All1024Texture"))
{
int count = 0;
foreach(var item in all1024Texture)
{
count +=1;
Texture tex = AssetDatabase.LoadAssetAtPath(item,typeof(Texture))as Texture;
GUIContent label2 = new GUIContent(count +"-"+item,item);
EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth*0.5f;
EditorGUILayout.ObjectField(label2,tex,typeof(Texture),false);
}
}
}
if(allModifyTexture != null && allModifyTexture.Count>0)
{
if(NGUIEditorTools.DrawHeader("AllModifyTexture"))
{
int count = 0;
foreach(var item in allModifyTexture)
{
count +=1;
Texture tex = AssetDatabase.LoadAssetAtPath(item,typeof(Texture))as Texture;
GUIContent label2 = new GUIContent(count +"-"+item,item);
EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth*0.7f;
EditorGUILayout.ObjectField(label2,tex,typeof(Texture),false);
}
}
}
GUILayout.EndScrollView();
}
[MenuItem("Assets/处理Texture/检测Texture超过1024的")]
public static void ScanAllFile()
{
//遍历所有的texture
//判断尺寸大小
//加入1024的图片
allTexture.Clear();
all1024Texture.Clear();
string projectPath = Application.dataPath;
projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
try
{
GetTexturesPath(projectPath,FilePath,"Texture",ref allTexture);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
for(int i = 0;i < allTexture.Count;i++)
{
EditorUtility.DisplayProgressBar("Check 1024Textures","Current 1024Texture :"+i+"/"+allTexture.Count,(float)i/allTexture.Count);
try
{
bool flag = false;
for (int j =0;j < whiteList1024.Length; j++)
{
if(allTexture[i].Contains(whiteList1024[j]))
{
flag = true;
break;
}
}
if(!flag)
{
BegainCheck1024Texs(allTexture[i]);
}
}
catch (System.Exception e)
{
EditorUtility.ClearProgressBar();
Debug.LogError(e);
}
}
EditorUtility.ClearProgressBar();
EditorWindow.GetWindow<ModifyAllTextureSetting>(false,"ModifyAllTextureSetting",true).Show();
Debug.Log("Check Success");
}
//开始检查Tex
public static void BegainCheck1024Texs(string texPath)
{
Texture tex = AssetDatabase.LoadAssetAtPath(texPath,typeof(Texture))as Texture;
if(tex.width>1024 || tex.height>1024)
{
all1024Texture.Add(texPath);
}
// Debug.Log("all1024Texture-----count-----"+all1024Texture.Count);
}
//开始修改参数
public static void BegainModifyTexs(string texPath)
{
CompressTexture(texPath);
}
public static void CompressTexture(string path)
{
Texture tex = AssetDatabase.LoadAssetAtPath(path,typeof(Texture))as Texture;
// 根据路径获得文件目录,设置图集的packagingTag
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if(!textureImporter)
{
Debug.LogError("textureImporter==============null");
return;
}
if (textureImporter.textureType != TextureImporterType.Default)
{
Debug.LogWarning("This texture is not Default Type: " + textureImporter.assetPath, textureImporter);
return;
}
bool flag = false;
if(!textureImporter.isReadable)
{
textureImporter.isReadable = false;
flag = true;
}
if(!textureImporter.streamingMipmaps)
{
textureImporter.streamingMipmaps = false;
flag = true;
}
if(!textureImporter.mipmapEnabled)
{
textureImporter.mipmapEnabled = false;
flag = true;
}
if(textureImporter.wrapMode != TextureWrapMode.Clamp)
{
textureImporter.wrapMode = TextureWrapMode.Clamp;
flag = true;
}
if(textureImporter.filterMode != FilterMode.Bilinear)
{
textureImporter.filterMode = FilterMode.Bilinear;
flag = true;
}
TextureImporterPlatformSettings settingDefault = textureImporter.GetDefaultPlatformTextureSettings();
if(settingDefault.format != TextureImporterFormat.Automatic)
{
settingDefault.format = TextureImporterFormat.Automatic;
flag = true;
}
if(settingDefault.compressionQuality != (int)TextureCompressionQuality.Normal)
{
settingDefault.compressionQuality = (int)TextureCompressionQuality.Normal;
flag = true;
}
if(flag)
{
allModifyTexture.Add(path);
textureImporter.SaveAndReimport();
}
}
//获取所有的图片文件的path信息
public static void GetTexturesPath(string projectPath,string targetFilePath,string searchType,ref List<string> array)
{
if (Directory.Exists(targetFilePath))
{
string[] guids;
//搜索
guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath });
foreach (string guid in guids)
{
string source = AssetDatabase.GUIDToAssetPath(guid);
string[] allStr = source.Split('.');
if(source.EndsWith(".bmp")|| source.EndsWith(".jpeg") || source.EndsWith(".jpg") || source.EndsWith(".png"))
{
allTexture.Add(source);
}
}
}
}
[MenuItem("Assets/处理Texture/设置图片Default参数")]
public static async void ApplyAllPlatformTexSetting()
{
allTexture.Clear();
string projectPath = Application.dataPath;
// Debug.Log("projectPath-------------"+projectPath);
projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
try
{
GetTexturesPathFromSpecifyPath(projectPath,FilePath,"Texture",ref allTexture,modifySettingPath);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
for(int i = 0;i < allTexture.Count;i++)
{
EditorUtility.DisplayProgressBar("Modify AllTextures","Current Need Modify Texture :"+i+"/"+allTexture.Count,(float)i/allTexture.Count);
try
{
bool flag = false;
for (int j =0;j < whiteListDefaultSetting.Length; j++)
{
if(allTexture[i].Contains(whiteListDefaultSetting[j]))
{
flag = true;
break;
}
}
if(!flag)
{
BegainModifyTexs(allTexture[i]);
}
}
catch (System.Exception e)
{
EditorUtility.ClearProgressBar();
Debug.LogError(e);
}
}
EditorUtility.ClearProgressBar();
EditorWindow.GetWindow<ModifyAllTextureSetting>(false,"ModifyAllTextureSetting",true).Show();
Debug.Log("Modify Success");
}
//获取指定文件夹的所有的图片文件的path信息
public static void GetTexturesPathFromSpecifyPath(string projectPath,string targetFilePath,string searchType,ref List<string> array,string[] searchPath)
{
if (Directory.Exists(targetFilePath))
{
string[] guids;
//搜索
guids = AssetDatabase.FindAssets("t:" + searchType, searchPath);
foreach (string guid in guids)
{
string source = AssetDatabase.GUIDToAssetPath(guid);
string[] allStr = source.Split('.');
if(source.EndsWith(".bmp")|| source.EndsWith(".jpeg") || source.EndsWith(".jpg") || source.EndsWith(".png"))
{
allTexture.Add(source);
}
}
}
}
}