傳統方式修改是通過TextureImporter的方法,先把Texture加載出來再修改,但是這樣的話很浪費時間;我的方法是直接修改Texture的.meta文件,速度比之前的快一倍。
[MenuItem("Assets/設置文件夾以及子文件夾下面的圖片壓縮格式爲ETC", priority = 0)]
static void AutoSetASTC()
{
Debug.Log("開始");
string[] guid = Selection.assetGUIDs;
List<string> lstPath = new List<string>();
for (int i = 0; i < guid.Length; i++)
{
string dirPath = AssetDatabase.GUIDToAssetPath(guid[i]);
lstPath.Add(dirPath);
}
string[] textures = AssetDatabase.FindAssets("t:Texture", lstPath.ToArray());
for (int i = 0; i < textures.Length; i++)
{
EditorUtility.DisplayProgressBar("Texture", i + "/" + textures.Length, (float)i / textures.Length);
//修改Meta文件
bool bModify = false;
string texPath = AssetDatabase.GUIDToAssetPath(textures[i]);
TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
string metaPath = texPath + ".meta";
List<string> metaContent = new List<string>();
using (StreamReader sr = new StreamReader(metaPath))
{
bool bAndroid = false;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
bAndroid = line.Contains("buildTarget") ? (line.Contains("Android") ? true : false) : bAndroid;
if (bAndroid && line.IndexOf("textureFormat:") != -1)
{
string[] textureMetas = line.Split(':');
if (textureMetas.Length == 2)
{
int texFormat = int.Parse(textureMetas[1]);
if (texFormat >= (int)TextureImporterFormat.ASTC_RGB_4x4 && texFormat <= (int)TextureImporterFormat.ASTC_RGB_12x12)
{
bModify = true;
texFormat = (int)TextureImporterFormat.ETC2_RGB4;
line = string.Format("{0}: {1}", textureMetas[0], texFormat.ToString());
}
else if (texFormat >= (int)TextureImporterFormat.ASTC_RGBA_4x4 && texFormat <= (int)TextureImporterFormat.ASTC_RGBA_12x12)
{
bModify = true;
texFormat = (int)TextureImporterFormat.ETC2_RGBA8;
line = string.Format("{0}: {1}", textureMetas[0], texFormat.ToString());
}
}
}
if (bAndroid && line.IndexOf("overridden:") != -1)
{
string[] textureMetas = line.Split(':');
if (textureMetas.Length == 2)
{
int texFormat = int.Parse(textureMetas[1]);
if (bModify)
bModify = texFormat == 1 ? true : false;
}
}
metaContent.Add(line);
}
sr.Close();
}
if (bModify)
{
File.Delete(metaPath);
using (StreamWriter sw = new StreamWriter(metaPath))
{
foreach (string line in metaContent)
{
sw.WriteLine(line);
}
sw.Close();
}
}
metaContent.Clear();
//對Texture進行修改
//int maxTexSize;
//int compressionQuality;
//TextureImporterFormat texFormat;
//string texPath = AssetDatabase.GUIDToAssetPath(textures[i]);
//TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
//TextureImporterPlatformSettings settingPlatform = texImporter.GetPlatformTextureSettings("Android");
//if (texImporter.GetPlatformTextureSettings("Android", out maxTexSize, out texFormat, out compressionQuality))
//{
// //texFormat = texImporter.DoesSourceTextureHaveAlpha() ? (texFormat != TextureImporterFormat.ETC2_RGBA8 ? TextureImporterFormat.ETC2_RGBA8 : texFormat)
// // : (texFormat != TextureImporterFormat.ETC2_RGB4 ? TextureImporterFormat.ETC2_RGB4 : texFormat);
// //settingPlatform.format = texFormat;
// texFormat = texImporter.DoesSourceTextureHaveAlpha() ? (texFormat != TextureImporterFormat.ASTC_RGBA_8x8 ? TextureImporterFormat.ASTC_RGBA_8x8 : texFormat)
// : (texFormat != TextureImporterFormat.ASTC_RGB_8x8 ? TextureImporterFormat.ASTC_RGB_8x8 : texFormat);
// settingPlatform.format = texFormat;
//}
//texImporter.SetPlatformTextureSettings(settingPlatform);
//texImporter.SaveAndReimport();
}
AssetDatabase.Refresh();
Debug.Log("End");
EditorUtility.ClearProgressBar();
}
直接拷貝就能用。