using System.IO;
using UnityEditor;
using UnityEngine;
public class ChangeTextureData
{
private static string[] paths = new string[]
{
Application.dataPath + "/Res/ab/base/image",
};
[MenuItem("Tools/ChangeTexture")]
static void Change()
{
for (int i = 0; i < paths.Length; i++)
{
Pack(paths[i]);
}
AssetDatabase.Refresh();
}
private static void Pack(string path)
{
DirectoryInfo infos = new DirectoryInfo(path);
FileSystemInfo[] files = infos.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
if (files[i] is DirectoryInfo)
{
Pack(files[i].FullName);
}
else
{
if (files[i].FullName.EndsWith(".png") || files[i].FullName.EndsWith(".jpg"))
{
ChangeTexture(files[i].FullName, files[i].Name);
}
}
}
}
static void ChangeTexture(string path,string imgname)
{
string[] strs1 = path.Split('\\');
string temppath = "";
for (int i = 0; i < strs1.Length-1; i++)
{
temppath = temppath + strs1[i] + @"\";
}
string _source = Replace(temppath);
_source = _source + imgname;
string _assetPath1 = "Assets" + _source.Substring(Application.dataPath.Length);
TextureImporter importer = AssetImporter.GetAtPath(_assetPath1) as TextureImporter;
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(_assetPath1);
int textureSize = Mathf.Max(texture.height, texture.width);
int SizeType = FitSize(textureSize);
#region 设置纹理方式一
TextureImporterFormat and1 = TextureImporterFormat.ETC2_RGBA8;
TextureImporterFormat and2 = TextureImporterFormat.ETC2_RGB4;
TextureImporterFormat ios1 = TextureImporterFormat.ASTC_6x6;
TextureImporterFormat ios2 = TextureImporterFormat.ASTC_6x6;
TextureImporterPlatformSettings and = importer.GetPlatformTextureSettings("Android");
and.overridden = true;
and.maxTextureSize = SizeType;
TextureImporterPlatformSettings ios = importer.GetPlatformTextureSettings("iPhone");
ios.overridden = true;
ios.maxTextureSize = SizeType;
if (importer.DoesSourceTextureHaveAlpha())
{
and.format = and1;
ios.format = ios1;
}
else
{
and.format = and2;
ios.format = ios2;
}
importer.SetPlatformTextureSettings(and);
importer.SetPlatformTextureSettings(ios);
importer.SaveAndReimport();
#endregion
#region 设置纹理方式二
//if (importer.DoesSourceTextureHaveAlpha())
//{
// importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_6x6);
// importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGBA8);
//}
//else
//{
// importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_6x6);
// importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGB4);
//}
//importer.SaveAndReimport();
#endregion
}
static int[] formatSize = new int[] { 32, 64, 128, 256, 512, 1024, 2048, 4096 };
static int FitSize(int picValue)
{
foreach (var one in formatSize)
{
if (picValue <= one)
{
return one;
}
}
return 1024;
}
private static string Replace(string str)
{
string s = str.Replace("\\", @"/");
return s;
}
}
Unity 批量设置图片的纹理格式
于 2022-06-22 16:05:42 首次发布