using dnlib.DotNet;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace GFGEditor
{
public class TextureAutoSet : EditorWindow
{
[MenuItem("Assets/*****设置文件夹以及子文件夹下面的图片压缩格式为ASTC", priority = 0)]
static void AutoSetASTC()
{
string[] guidArray = Selection.assetGUIDs;
foreach (var item in guidArray)
{
string selectFloder = AssetDatabase.GUIDToAssetPath(item);
DirectoryInfo root = new DirectoryInfo(selectFloder);
GetFloder(root);
}
}
static void GetFloder(DirectoryInfo root)
{
GetFile(root);
//查找子文件夹
DirectoryInfo[] array = root.GetDirectories();
//Debug.Log(root);
foreach (DirectoryInfo item in array)
{
GetFloder(item);
}
}
static void GetFile(DirectoryInfo root)
{
//DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] fileDic = root.GetFiles();
foreach (var file in fileDic)
{
//sDebug.Log(file);
if (!file.FullName.EndsWith(".meta"))
{
//Debug.Log("-------------" + file.FullName);
//Debug.Log(Application.dataPath);
SetPicFormat(file.FullName.Replace(Application.dataPath.Replace("Assets", ""), ""));
}
}
}
static void SetPicFormat(string path)
{
string onePath = GetImageProjectPath(path);
Debug.Log(onePath);
TextureImporter textureImporter = AssetImporter.GetAtPath(onePath) as TextureImporter;
if (textureImporter == null)
return;
if (textureImporter.mipmapEnabled == true)
{
textureImporter.mipmapEnabled = false;
}
ChangeTextureSetting(textureImporter);
}
public static void ChangeTextureSetting(TextureImporter textureImporter)
{
ChangeIphoneSetting(textureImporter);
ChangeAndroidSetting(textureImporter);
textureImporter.SaveAndReimport();
}
static void ChangeIphoneSetting(TextureImporter textureImporter)
{
TextureImporterPlatformSettings settings = textureImporter.GetPlatformTextureSettings("Iphone");
settings.overridden = true;
settings.maxTextureSize = 2048;
settings.format = TextureImporterFormat.ASTC_6x6;
textureImporter.SetPlatformTextureSettings(settings);
}
static void ChangeAndroidSetting(TextureImporter textureImporter)
{
TextureImporterPlatformSettings settings = textureImporter.GetPlatformTextureSettings("Android");
settings.overridden = true;
settings.maxTextureSize = 2048;
settings.format = TextureImporterFormat.ASTC_6x6;
textureImporter.SetPlatformTextureSettings(settings);
}
///
/// 根据固定字符串得到资源路径
///
///
///
static string GetImageProjectPath(string fullName)
{
string result = "";
int index = fullName.LastIndexOf("Assets");
result = fullName.Substring(index, fullName.Length - index);
return result;
}
}
}