需求:需要針對(duì)texture做修改,首先需要找到所有大于1024的圖集;針對(duì)texture的部分參數(shù)做處理;并都加上白名單
分析:倆個(gè)功能單獨(dú)做,都需要針對(duì)特定的文件夾做搜索耀盗,然后要對(duì)白名單進(jìn)行過濾认然,第一個(gè)拿到對(duì)象然后進(jìn)行尺寸的判定并返回就可以了;第二個(gè)是過濾掉之后并對(duì)參數(shù)進(jìn)行修改,然后保存酌伊。
邏輯不復(fù)雜腾窝,直接上代碼,大體邏輯和之前的audio文件的優(yōu)化差不多居砖,感興趣的話可以看看上一篇
直接上代碼了
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;
//需要修改參數(shù)的文件夾
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/檢測(cè)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);
}
//開始修改參數(shù)
public static void BegainModifyTexs(string texPath)
{
CompressTexture(texPath);
}
public static void CompressTexture(string path)
{
Texture tex = AssetDatabase.LoadAssetAtPath(path,typeof(Texture))as Texture;
// 根據(jù)路徑獲得文件目錄虹脯,設(shè)置圖集的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/設(shè)置圖片Default參數(shù)")]
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);
}
}
}
}
}