using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class ModifyTextureType : AssetPostprocessor
{
private static string PATH = "E:\\tcb\\program\\tcb\\tcbclient\\";
///
/// 批量修改
///
[MenuItem("Custom/ModifyTexture/Texture Import Settings")]
private static void Init()
{
LoopSetTexture2();
}
///
/// 获取贴图设置
///
public static TextureImporter GetTextureSettings(string path)
{
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
//Texture Type
textureImporter.textureType = TextureImporterType.Advanced;
//Non power of2
textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
//PlatformTextureSettings
textureImporter.SetPlatformTextureSettings("iPhone", 1024, TextureImporterFormat.PVRTC_RGBA4);
textureImporter.SetPlatformTextureSettings("Android", 1024, TextureImporterFormat.ETC2_RGBA8);
return textureImporter;
}
///
/// 循环设置选择的贴图
///
private static void LoopSetTexture()
{
Object[] textures = GetSelectedTextures();
foreach (Texture2D texture in textures)
{
//获取资源路径
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter texImporter = GetTextureSettings(path);
TextureImporterSettings tis = new TextureImporterSettings();
texImporter.ReadTextureSettings(tis);
texImporter.SetTextureSettings(tis);
AssetDatabase.ImportAsset(path);
}
}
///
/// 获取Resources下的贴图
///
///
private static Object[] GetSelectedTextures()
{
Object[] textureAll;
var textures = Resources.LoadAll("", typeof(Texture2D));
int countAll = textures.Length;
textureAll = new Object[countAll];
for (int i = 0; i < countAll; i++)
{
textureAll[i] = textures[i] as Object;
}
return textureAll;
}
private static void LoopSetTexture2()
{
string[] fileInfo = GetTexturePath();
int length = fileInfo.Length;
for (int i = 0; i < length; i++)
{
//获取资源路径
string path = fileInfo[i];
TextureImporter texImporter = GetTextureSettings(path);
TextureImporterSettings tis = new TextureImporterSettings();
texImporter.ReadTextureSettings(tis);
texImporter.SetTextureSettings(tis);
AssetDatabase.ImportAsset(path);
}
}
private static string[] GetTexturePath()
{
//jpg
ArrayList jpgList = GetResourcesPath("*.jpg");
int jpgLength = jpgList.Count;
//png
ArrayList pngList = GetResourcesPath("*.png");
int pngLength = pngList.Count;
//tga
ArrayList tgaList = GetResourcesPath("*.tga");
int tgaLength = tgaList.Count;
string[] filePath = new string[jpgLength + pngLength + tgaLength];
for (int i = 0; i < jpgLength; i++)
{
filePath[i] = jpgList[i].ToString();
}
for (int i = 0; i < pngLength; i++)
{
filePath[i + jpgLength] = pngList[i].ToString();
}
for (int i = 0; i < tgaLength; i++)
{
filePath[i + jpgLength + pngLength] = tgaList[i].ToString();
}
return filePath;
}
///
/// 获取指定后掇后的文件路径
///
///
private static ArrayList GetResourcesPath(string fileType)
{
DirectoryInfo directoryInfo = new DirectoryInfo(PATH + "Assets\\Resources");
ArrayList filePath = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles(fileType, SearchOption.AllDirectories))
{
string path = fi.DirectoryName + "\\" + fi.Name;
path = path.Remove(0, PATH.Length);
path = path.Replace("\\", "/");
filePath.Add(path);
}
return filePath;
}
}