</pre><pre name="code" class="csharp">
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 功能:引擎内图片高级选项批量修改
/// 平台:Unity4.6.4/C#
/// 设计模式:命令模式(15)
/// FileName:TextureManager
/// Creator: Thewen
/// CreateTime:2016/4/20 15:26:45
/// Description:
/// History:
/// </summary>
///<summary>
///命令基类 为子类提供处理具体逻辑的逻辑类
///</summary>
public abstract class Command
{
protected Receiver _receiver;
public Command(Receiver receiver)
{
this._receiver = receiver;
}
public abstract void ExecuteAction();
}
/// <summary>
/// 命令模式关键类 (处理逻辑主体)
/// </summary>
public class Receiver
{
TextureImporterSettings _importsettings=new TextureImporterSettings();
bool _iscompress;
string _texturetype;
string _path;
TextureImporterFormat _iosformat;
TextureImporterFormat _androidformat;
//默认
public Receiver(string texturetype, string path)
{
this._texturetype = texturetype;
this._path = path;
this._importsettings.npotScale = TextureImporterNPOTScale.ToNearest;
this._importsettings.ApplyTextureType(TextureImporterType.Advanced, false);
this._importsettings.readable = false;
this._importsettings.mipmapEnabled = true;
this._importsettings.lightmap = false;
this._iscompress = true;
}
public Receiver(string texturetype, string path,bool iscompress,bool mipmapenabled ,bool lightmap)
{
this._texturetype = texturetype;
this._path = path;
this._importsettings.npotScale = TextureImporterNPOTScale.ToNearest;
this._importsettings.ApplyTextureType(TextureImporterType.Advanced, false);
this._importsettings.readable = false;
this._importsettings.mipmapEnabled = mipmapenabled;
this._importsettings.lightmap = lightmap;
this._iscompress = iscompress;
}
/// <summary>
/// 设置图片的高级属性
/// </summary>
public void SetTextureAnvanceSetting( )
{
ArrayList filePath = TextureManager.GetAssetPath(_texturetype, _path);
for (int i = 0; i < filePath.Count; i++)
{
TextureManager.SetAdvanceForPath(filePath[i].ToString(),_iscompress,_importsettings,_iosformat,_androidformat);
}
}
}
/// <summary>
/// 命令执行类 (便于命令的集中管理,为实现撤销于恢复提供可能)
/// </summary>
public class Invoker
{
public Command _command;
public List<Command> _commandlist;
public Invoker(Command command)
{
this._command = command;
}
public Invoker(List<Command> commandlist)
{
this._commandlist = commandlist;
}
public void ExecuteCommandList()
{
for (int i = 0; i < _commandlist.Count; i++)
{
_commandlist[i].ExecuteAction();
}
//_commandlist.ExecuteAction();
}
public void ExecuteCommand()
{
_command.ExecuteAction();
}
}
//命令1 设置图片的高级选项
public class SetAdvanceCommand : Command
{
public SetAdvanceCommand(Receiver receiver):base(receiver)
{
}
public override void ExecuteAction()
{
_receiver.SetTextureAnvanceSetting();
}
}
public class TextureManager: AssetPostprocessor
{
#region 目录菜单接口定义
[MenuItem("TextureManager/压缩/批量贴图Advance设置")]
static void SetTextureAdvance()
{
string fileType = "*.png";
string fileTypeTGA = "*.tga";
//通用命令执行 执行列表的需求
List<Command> Commandlist = new List<Command>();
//Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\CH\\CH_Evil_Bolt"))); //单独测试
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\CH")));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\EVN")));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\OBJ")));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\CH")));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\EVN")));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\OBJ")));
Invoker invoker = new Invoker(Commandlist);
invoker.ExecuteCommandList();
}
[MenuItem("TextureManager/压缩/批量贴图FX设置")]
static void SetTextureFX()
{
string fileType = "*.png";
string fileTypeTGA = "*.tga";
//通用命令执行 执行列表的需求
List<Command> Commandlist = new List<Command>();
//Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX\\Texture\\Flares", true, false, false))); //单独测试
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX", true, false, false)));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\FX", true, false, false)));
Invoker invoker = new Invoker(Commandlist);
invoker.ExecuteCommandList();
}
[MenuItem("TextureManager/压缩/批量贴图AdvanceLightMap设置")]
static void SetTextureLightMap()
{
string fileType = "*.exr";
//通用命令执行 执行列表的需求
List<Command> Commandlist = new List<Command>();
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKScene", true, false,true)));
Invoker invoker = new Invoker(Commandlist);
invoker.ExecuteCommandList();
}
[MenuItem("TextureManager/压缩/批量贴图AdvanceT4M设置")]
static void SetTextureT4M()
{
string fileType = "*.png";
List<Command> Commandlist = new List<Command>();
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\T4MOBJ\\Terrains\\Texture", true, false, false)));
Invoker invoker = new Invoker(Commandlist);
invoker.ExecuteCommandList();
//SetAdvanceByType(fileType, "\\T4MOBJ\\Terrains\\Texture", "closemipmap");
}
[MenuItem("TextureManager/RGBA32/单张图片修改")]
static void SetRGBA32Texture()
{
var select = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);
string filepath= AssetDatabase.GetAssetPath(select[0] as Texture2D);
TextureImporterSettings importsettings = new TextureImporterSettings();
importsettings.npotScale = TextureImporterNPOTScale.ToNearest;
importsettings.ApplyTextureType(TextureImporterType.Advanced, false);
importsettings.readable = false;
importsettings.mipmapEnabled = false;
importsettings.lightmap = false;
SetAdvanceForPath(filepath,false, importsettings,new TextureImporterFormat(),new TextureImporterFormat() );
}
[MenuItem("TextureManager/RGBA32/UI批量修改")]
static void SetRGBA32TextureAll()
{
string fileType = "*.png";
string fileTypeTGA = "*.tga";
List<Command> Commandlist = new List<Command>();
//Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX\\Texture\\Flares", true, false, false))); //单独测试
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\UI", false, false, false)));
Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\UI", false, false, false)));
Invoker invoker = new Invoker(Commandlist);
invoker.ExecuteCommandList();
}
#endregion
#region TextureManager 图片处理公开方法
public static void SetAdvanceForPath(string filePath, bool isCompress, TextureImporterSettings importSettings, TextureImporterFormat iosFormat, TextureImporterFormat androidFormat)
{
Texture2D m2d = AssetDatabase.LoadAssetAtPath(filePath, typeof(Texture2D)) as Texture2D;
Debug.Log(m2d.width + "*" + m2d.height);
//判断是否是压缩格式
if (isCompress)
{
if (IsETCOrPVRTCFormat(m2d, m2d.format))
{
return;
}
}
else
{
if (IsRPGA(m2d, m2d.format))
{
return;
}
}
TextureImporter texImporter = GetTextureSettings(filePath);//AssetImporter.GetAtPath(filePath) as TextureImporter; //TextureManager.GetTextureSettings(filePath);
TextureImporterSettings tis = new TextureImporterSettings();
texImporter.ReadTextureSettings(tis);
tis.npotScale = TextureImporterNPOTScale.ToNearest;
//tis = _importsettings;//这种写法有出莫名问题(不提倡)
tis.npotScale = importSettings.npotScale;
tis.ApplyTextureType(TextureImporterType.Advanced, false);
tis.readable = importSettings.readable;
tis.mipmapEnabled = importSettings.mipmapEnabled;
tis.lightmap = importSettings.lightmap;
//注释掉Unity面板会有警告提示出现
//if (m2d.width != m2d.height)
//{
// //tis.wrapMode = TextureWrapMode.Clamp; //对于不是 NOPT(是“non power of two”的缩写)的贴图 wrap mode 不能被设置为Repeat
//}
texImporter.SetTextureSettings(tis);
if (TextureManager.JudgeTransparentPic(m2d.format))
{
if (isCompress)
{
androidFormat = TextureImporterFormat.ETC2_RGBA8;
iosFormat = TextureImporterFormat.PVRTC_RGBA4;
}
else
{
androidFormat = iosFormat = TextureImporterFormat.RGBA32;
}
}
else
{
if (isCompress)
{
androidFormat = TextureImporterFormat.ETC_RGB4;
iosFormat = TextureImporterFormat.PVRTC_RGB4;
}
else
{
iosFormat = androidFormat = TextureImporterFormat.RGB24;
}
}
int mSize = Mathf.Max(m2d.width, m2d.height);
mSize = Mathf.ClosestPowerOfTwo(mSize);
texImporter.SetPlatformTextureSettings("Default", mSize, TextureImporterFormat.AutomaticCompressed);
texImporter.SetPlatformTextureSettings("Android", mSize, androidFormat);
texImporter.SetPlatformTextureSettings("iPhone", mSize, iosFormat);
AssetDatabase.ImportAsset(filePath);
}
public static ArrayList GetAssetPath(string fileType, string _Path)
{
string DirectoryPath = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets"));
DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath + _Path);
ArrayList filePath = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles(fileType, SearchOption.AllDirectories))
{
string path = fi.DirectoryName + "\\" + fi.Name;
path = path.Remove(0, DirectoryPath.Length);
path = path.Replace("\\", "/");
//Texture2D m2d = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
//if(JudgeTransparentPic(m2d.format))
filePath.Add(path);
}
return filePath;
}
public static bool IsETCOrPVRTCFormat(Texture2D _m2d, TextureFormat _format)
{
string selfFormat = _format.ToString();
if (selfFormat == TextureFormat.ETC_RGB4.ToString() ||
selfFormat == TextureFormat.ETC2_RGBA8.ToString() ||
selfFormat == TextureFormat.PVRTC_RGB4.ToString() ||
selfFormat == TextureFormat.PVRTC_RGBA4.ToString())
{
string path = AssetDatabase.GetAssetPath(_m2d);
TextureImporter texImporter = GetTextureSettings(path);// AssetImporter.GetAtPath(path) as TextureImporter;// GetTextureSettings(path);
if (texImporter.textureType != TextureImporterType.Advanced)
{
return false;
}
return true;
}
return false;
}
public static bool IsRPGA(Texture2D _m2d, TextureFormat _format)
{
string selfFormat = _format.ToString();
if (selfFormat == TextureFormat.RGBA32.ToString() ||
selfFormat == TextureFormat.RGB24.ToString())
{
string path = AssetDatabase.GetAssetPath(_m2d);
TextureImporter texImporter = GetTextureSettings(path);// AssetImporter.GetAtPath(path) as TextureImporter;//GetTextureSettings(path);
if (texImporter.textureType != TextureImporterType.Advanced)
{
return false;
}
return true;
}
return false;
}
/// <summary>
/// 判断贴图是否拥有透明通道
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
public static bool JudgeTransparentPic(TextureFormat format)
{
string selfFormat = format.ToString();
string[] transparentFormat =
{
TextureFormat.Alpha8.ToString(),TextureFormat.ARGB32.ToString(),
TextureFormat.ARGB4444.ToString(),TextureFormat.ATC_RGBA8.ToString(),
TextureFormat.ATF_RGBA_JPG.ToString(),TextureFormat.BGRA32.ToString(),
TextureFormat.PVRTC_RGBA2.ToString(),TextureFormat.PVRTC_RGBA4.ToString(),
TextureFormat.RGBA32.ToString(),TextureFormat.RGBA4444.ToString(),
TextureFormat.DXT5.ToString(),TextureFormat.ETC2_RGBA1.ToString(),
TextureFormat.ETC2_RGBA8.ToString(),
};
for (int i = 0; i < transparentFormat.Length; i++)
{
if (selfFormat == transparentFormat[i])
{
return true;
}
}
return false;
}
/// <summary>
/// 获取贴图设置 1024 size 这是一种保护
/// </summary>
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", 2048, TextureImporterFormat.PVRTC_RGBA4);
textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8);
return textureImporter;
}
#endregion
}