using UnityEngine;
using System.Collections;
using UnityEditor;
/// <summary>
/// 批量图片资源导入设置
/// 使用说明: 选择需要批量设置的贴图,
/// 单击DuanMenu/Texture Import Settings,
/// 打开窗口后选择对应参数,
/// 点击Set Texture ImportSettings,
/// 稍等片刻,--批量设置成功。
/// </summary>
public class TextureImportSetting : EditorWindow
{
/// <summary>
/// 临时存储int[]
/// </summary>
private int[] IntArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
//AnisoLevel
private int AnisoLevel = 1;
//Filter Mode
private int FilterModeInt = 0;
private string[] FilterModeString = new string[] { "Point", "Bilinear", "Trilinear" };
//Wrap Mode
private int WrapModeInt = 0;
private string[] WrapModeString = new string[] { "Repeat", "Clamp" };
//Texture Type
private int TextureTypeInt = 0;
private string[] TextureTypeString = new string[] { "Texture", "Normal Map", "GUI", "Refelection", "Cookie", "Lightmap", "Advanced" };
//Max Size
private int MaxSizeInt = 5;
private string[] MaxSizeString = new string[] { "32", "64", "128", "256", "512", "1024", "2048", "4096" };
//Format
private int FormatInt = 0;
private string[] FormatString = new string[] { "Compressed", "16 bits", "true color" };
/// <summary>
/// 创建、显示窗体
/// </summary>
[@MenuItem("DuanMenu/Texture Import Settings")]
private static void Init()
{
TextureImportSetting window = (TextureImportSetting)EditorWindow.GetWindow(typeof(TextureImportSetting), true, "TextureImportSetting");
window.Show();
}
/// <summary>
/// 显示窗体里面的内容
/// </summary>
private void OnGUI()
{
//AnisoLevel
GUILayout.BeginHorizontal();
GUILayout.Label("Aniso Level ");
AnisoLevel = EditorGUILayout.IntSlider(AnisoLevel, 0, 9);
GUILayout.EndHorizontal();
//Filter Mode
FilterModeInt = EditorGUILayout.IntPopup("Filter Mode", FilterModeInt, FilterModeString, IntArray);
//Wrap Mode
WrapModeInt = EditorGUILayout.IntPopup("Wrap Mode", WrapModeInt, WrapModeString, IntArray);
//Texture Type
TextureTypeInt = EditorGUILayout.IntPopup("Texture Type", TextureTypeInt, TextureTypeString, IntArray);
//Max Size
MaxSizeInt = EditorGUILayout.IntPopup("Max Size", MaxSizeInt, MaxSizeString, IntArray);
//Format
FormatInt = EditorGUILayout.IntPopup("Format", FormatInt, FormatString, IntArray);
if (GUILayout.Button("Set Texture ImportSettings"))
LoopSetTexture();
}
/// <summary>
/// 获取贴图设置
/// </summary>
public TextureImporter GetTextureSettings(string path)
{
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
//AnisoLevel
textureImporter.anisoLevel = AnisoLevel;
//Filter Mode
switch (FilterModeInt)
{
case 0:
textureImporter.filterMode = FilterMode.Point;
break;
case 1:
textureImporter.filterMode = FilterMode.Bilinear;
break;
case 2:
textureImporter.filterMode = FilterMode.Trilinear;
break;
}
//Wrap Mode
switch (WrapModeInt)
{
case 0:
textureImporter.wrapMode = TextureWrapMode.Repeat;
break;
case 1:
textureImporter.wrapMode = TextureWrapMode.Clamp;
break;
}
//Texture Type
switch (TextureTypeInt)
{
case 0:
textureImporter.textureType = TextureImporterType.Image;
break;
case 1:
textureImporter.textureType = TextureImporterType.Bump;
break;
case 2:
textureImporter.textureType = TextureImporterType.GUI;
break;
case 3:
textureImporter.textureType = TextureImporterType.Reflection;
break;
case 4:
textureImporter.textureType = TextureImporterType.Cookie;
break;
case 5:
textureImporter.textureType = TextureImporterType.Lightmap;
break;
case 6:
textureImporter.textureType = TextureImporterType.Advanced;
break;
}
//Max Size
switch (MaxSizeInt)
{
case 0:
textureImporter.maxTextureSize = 32;
break;
case 1:
textureImporter.maxTextureSize = 64;
break;
case 2:
textureImporter.maxTextureSize = 128;
break;
case 3:
textureImporter.maxTextureSize = 256;
break;
case 4:
textureImporter.maxTextureSize = 512;
break;
case 5:
textureImporter.maxTextureSize = 1024;
break;
case 6:
textureImporter.maxTextureSize = 2048;
break;
case 7:
textureImporter.maxTextureSize = 4096;
break;
}
//Format
switch (FormatInt)
{
case 0:
textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;
break;
case 1:
textureImporter.textureFormat = TextureImporterFormat.Automatic16bit;
break;
case 2:
textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
break;
}
return textureImporter;
}
/// <summary>
/// 循环设置选择的贴图
/// </summary>
private void LoopSetTexture()
{
Object[] textures = GetSelectedTextures();
Selection.objects = new Object[0];
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);
}
}
/// <summary>
/// 获取选择的贴图
/// </summary>
/// <returns></returns>
private Object[] GetSelectedTextures()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
}
--------------
資料來源: http://www.cnblogs.com/fortomorrow/archive/2012/11/05/unity11.html
-------------------
更改後版本...
但是 Unity Editor 還是有很多不足的地方,如: 由 Editor Windows 開啟 Sprite Editor 可能就沒有辦法囉,因為目前小弟暫時找不到方法可以解決。
Script(C#):
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ImportSetting : EditorWindow{
private int[] intArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private int anisoLevel = 1;
private int filterModeInt = 0;
private string[] filterModeString = new string[] { "Point", "Bilinear", "Trilinear" };
private int wrapModeInt = 0;
private string[] warpModeSering = new string[] { "Repeat", "Clamp" };
private int textrueTypeInt = 0;
private string[] textrueTypeSetting = new string[] { "Textrue", "Normal Map", "GUI", "Refelection", "Cookie", "Lightmap", "Sprite", "Advanced" };
private int maxSizeInt = 0;
private string[] maxSizeString = new string[] { "32", "64", "128", "256", "512", "1024", "2048", "4096" };
private int formatInt = 0;
private string[] formatString = new string[] { "Compressed", "16 bits", "true color" };
// Texture 和 Cookie
private bool alphaFromGraycal = false, alphaIsTransparen = false;
// Sprite
private int spriteModeInt = 0;
private string[] spriteModeString = new string[] { "Single", "Multiple" };
private string packingTag = "";
private float pixelsPerUnit = 0;
GUIStyle style = new GUIStyle();
[MenuItem("遊戲控制器視窗/匯入素材設定視窗")]
static void Init() {
ImportSetting windows = (ImportSetting)EditorWindow.GetWindow(typeof(ImportSetting), true, "素材控制視窗");
windows.Show();
}
void OnGUI() {
style.fontSize = 18;
style.normal.textColor = Color.white;
// Texture Type;
GUILayout.BeginArea(new Rect(0, 0, 300, 100));
GUILayout.BeginHorizontal();
textrueTypeInt = EditorGUILayout.IntPopup("Texture Type(紋理屬性)", textrueTypeInt, textrueTypeSetting, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 30, 300, 30));
GUILayout.BeginHorizontal();
GUILayout.Label("紋理基本設定設定", style);
GUILayout.EndHorizontal();
GUILayout.EndArea();
// Filter Mode
GUILayout.BeginArea(new Rect(0, 60, 300, 100));
GUILayout.BeginHorizontal();
filterModeInt = EditorGUILayout.IntPopup("Filter Mode(過濾模式)", filterModeInt, filterModeString, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
// Warp Mode
GUILayout.BeginArea(new Rect(0, 90, 300, 100));
GUILayout.BeginHorizontal();
wrapModeInt = EditorGUILayout.IntPopup("Wrap Mode(換行模式)", wrapModeInt, warpModeSering, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
//Max Size
GUILayout.BeginArea(new Rect(0, 120, 300, 100));
GUILayout.BeginHorizontal();
maxSizeInt = EditorGUILayout.IntPopup("Max Size(最大纹理大小)", maxSizeInt, maxSizeString, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
// Format
GUILayout.BeginArea(new Rect(0, 150, 300, 100));
GUILayout.BeginHorizontal();
formatInt = EditorGUILayout.IntPopup("Max Size(紋理格式)", formatInt, formatString, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
// AnisoLevel
GUILayout.BeginArea(new Rect(0, 180, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("AnisoLevel(紋理的過濾等級)");
anisoLevel = EditorGUILayout.IntSlider(anisoLevel, 0, 9);
GUILayout.EndHorizontal();
GUILayout.EndArea();
// 確定
GUILayout.BeginArea(new Rect(200, 300, 100, 100));
GUILayout.BeginHorizontal();
if (GUILayout.Button("確定"))
loopSetTexture();
GUILayout.EndHorizontal();
GUILayout.EndArea();
switch (textrueTypeInt) {
case 0:
GUILayout.BeginArea(new Rect(0, 210, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("Alpha From Graycal(阿爾法從灰度)");
alphaFromGraycal = EditorGUILayout.Toggle(alphaFromGraycal);
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 240, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("Alpha Is Transparen(阿爾法是透明的)");
alphaIsTransparen = EditorGUILayout.Toggle(alphaIsTransparen);
GUILayout.EndHorizontal();
GUILayout.EndArea();
break;
case 4:
GUILayout.BeginArea(new Rect(0, 210, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("Alpha From Graycal(阿爾法從灰度)");
alphaFromGraycal = EditorGUILayout.Toggle(alphaFromGraycal);
GUILayout.EndHorizontal();
GUILayout.EndArea();
break;
case 6:
GUILayout.BeginArea(new Rect(0, 210, 300, 100));
GUILayout.BeginHorizontal();
spriteModeInt = EditorGUILayout.IntPopup("Sprite Mode(Sprite 模式)", spriteModeInt, spriteModeString, intArray);
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 240, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("Sprite 標籤");
packingTag = EditorGUILayout.TextArea(packingTag);
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 270, 300, 100));
GUILayout.BeginHorizontal();
GUILayout.Label("Pixels Per Unit(每單位像素)");
pixelsPerUnit = EditorGUILayout.Slider((float)pixelsPerUnit, 0, 100);
GUILayout.EndHorizontal();
GUILayout.EndArea();
break;
}
}
TextureImporter getTextureSettings(string path) {
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.anisoLevel = anisoLevel;
// textureType (貼圖類型)
switch (textrueTypeInt){
case 0:
textureImporter.textureType = TextureImporterType.Image;
break;
case 1:
textureImporter.textureType = TextureImporterType.Bump;
break;
case 2:
textureImporter.textureType = TextureImporterType.GUI;
break;
case 3:
textureImporter.textureType = TextureImporterType.Reflection;
break;
case 4:
textureImporter.textureType = TextureImporterType.Cookie;
break;
case 5:
textureImporter.textureType = TextureImporterType.Lightmap;
break;
case 6:
textureImporter.textureType = TextureImporterType.Sprite;
break;
case 7:
textureImporter.textureType = TextureImporterType.Advanced;
break;
}
// Filter (過濾)
switch (filterModeInt) {
case 0:
textureImporter.filterMode = FilterMode.Point;
break;
case 1:
textureImporter.filterMode = FilterMode.Bilinear;
break;
case 2:
textureImporter.filterMode = FilterMode.Trilinear;
break;
}
// Warp (換行)
switch (wrapModeInt) {
case 0:
textureImporter.wrapMode = TextureWrapMode.Repeat;
break;
case 1:
textureImporter.wrapMode = TextureWrapMode.Clamp;
break;
}
// Max (紋理最大的大小)
switch (maxSizeInt) {
case 0:
textureImporter.maxTextureSize = 32;
break;
case 1:
textureImporter.maxTextureSize = 64;
break;
case 2:
textureImporter.maxTextureSize = 128;
break;
case 3:
textureImporter.maxTextureSize = 256;
break;
case 4:
textureImporter.maxTextureSize = 512;
break;
case 5:
textureImporter.maxTextureSize = 1024;
break;
case 6:
textureImporter.maxTextureSize = 2048;
break;
case 7:
textureImporter.maxTextureSize = 4096;
break;
}
// Format (格式)
switch (formatInt) {
case 0:
textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;
break;
case 1:
textureImporter.textureFormat = TextureImporterFormat.Automatic16bit;
break;
case 2:
textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
break;
}
// TextureType Setting
switch (textrueTypeInt) {
case 0:
textureImporter.alphaIsTransparency = alphaIsTransparen;
textureImporter.grayscaleToAlpha = alphaFromGraycal;
break;
case 4:
textureImporter.grayscaleToAlpha = alphaFromGraycal;
break;
case 6:
if (spriteModeInt.Equals(0))
textureImporter.spriteImportMode = SpriteImportMode.Single;
else if (spriteModeInt.Equals(1))
textureImporter.spriteImportMode = SpriteImportMode.Multiple;
textureImporter.spritePackingTag = packingTag;
textureImporter.spritePixelsPerUnit = pixelsPerUnit;
break;
}
return textureImporter;
}
/// <summary>
/// 設置選擇貼圖
/// </summary>
void loopSetTexture() {
Object[] textures = getSelectedTextures();
Selection.objects = new Object[0];
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);
}
}
/// <summary>
/// 選取貼圖
/// </summary>
/// <returns></returns>
Object[] getSelectedTextures() {
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
}
結果圖:
沒有留言:
張貼留言