using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class TextureAutoSet : EditorWindow
{
[MenuItem("Assets/检查图片压缩格式", priority = 0)]
static void AutoSetASTC()
{
string[] guidArray = Selection.assetGUIDs;
Debug.Log(guidArray.Length);
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();
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(".png") || file.FullName.EndsWith(".jpg") || file.FullName.EndsWith(".tga") ||
file.FullName.EndsWith(".psd") || file.FullName.EndsWith(".PSD") || file.FullName.EndsWith(".exr") ||
file.FullName.EndsWith(".tif"))
{
//Debug.Log("-------------" + file.FullName);
//Debug.Log(Application.dataPath);
SetPicFormat(file.FullName.Replace('\\','/').Replace(Application.dataPath.Replace("Assets", ""), ""));
}
}
}
static void SetPicFormat(string path)
{
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer==null)
{
return;
}
var tempsss = importer.GetPlatformTextureSettings("Android");
bool isNeedFormat = false;
switch (tempsss.format)
{
case TextureImporterFormat.ASTC_RGB_4x4:
case TextureImporterFormat.ASTC_RGB_5x5:
case TextureImporterFormat.ASTC_RGB_6x6:
case TextureImporterFormat.ASTC_RGB_8x8:
case TextureImporterFormat.ASTC_RGB_10x10:
case TextureImporterFormat.ASTC_RGB_12x12:
case TextureImporterFormat.ASTC_RGBA_4x4:
case TextureImporterFormat.ASTC_RGBA_5x5:
case TextureImporterFormat.ASTC_RGBA_6x6:
case TextureImporterFormat.ASTC_RGBA_8x8:
case TextureImporterFormat.ASTC_RGBA_10x10:
case TextureImporterFormat.ASTC_RGBA_12x12:
Debug.Log(path);
isNeedFormat = true;
break;
default:
break;
}
if (!isNeedFormat || !tempsss.overridden)
{
Debug.Log("设置图片格式"+path);
tempsss.overridden = true;
tempsss.format = TextureImporterFormat.ASTC_RGBA_6x6;
tempsss.maxTextureSize = 1024;
importer.SetPlatformTextureSettings(tempsss);
importer.SaveAndReimport();
}
}
}
网友评论