using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class TextureImportChanging : EditorWindow
{
enum MaxSize
{
Size_32 = 32,
Size_64 = 64,
Size_128 = 128,
Size_256 = 256,
Size_512 = 512,
Size_1024 = 1024,
Size_2048 = 2048,
Size_4096 = 4096,
Size_8192 = 8192,
}
// ----------------------------------------------------------------------------
TextureImporterType textureType = TextureImporterType.Sprite;
TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
MaxSize textureSize = MaxSize.Size_1024;
TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;
bool ifAllowsAlphaSplitting = true;
bool ifMipmapEnabled = false;
string textureTag = "默认为父文件夹名";
//平台
bool isAndroid = true;
bool isIOS = true;
static TextureImportChanging window;
[@MenuItem("Tools/Texture Settings")]
private static void Init()
{
Rect wr = new Rect(0, 0, 400, 400);
window = (TextureImportChanging)EditorWindow.GetWindowWithRect(typeof(TextureImportChanging), wr, false, "图片格式设置");
window.Show();
}
private void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.HelpBox("设置选中图片或选中路径下的图片属性", MessageType.Info);
EditorGUILayout.Space();
textureType = (TextureImporterType)EditorGUILayout.EnumPopup("Type:", textureType);
textureTag = EditorGUILayout.TextField("PackingTag", textureTag);
textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("Fromat:", textureFormat);
textureSize = (MaxSize)EditorGUILayout.EnumPopup("Size:", textureSize);
textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("Compression:", textureCompression);
ifAllowsAlphaSplitting = EditorGUILayout.Toggle("AlphaSplitting:", ifAllowsAlphaSplitting);
ifMipmapEnabled = EditorGUILayout.Toggle("MipmapEnabled:", ifMipmapEnabled);
EditorGUILayout.LabelField("设置平台 : ");
GUILayout.BeginHorizontal();
isAndroid = GUILayout.Toggle(isAndroid, "Android");
isIOS = GUILayout.Toggle(isIOS, "IOS");
GUILayout.EndHorizontal();
EditorGUILayout.Space();
if (GUILayout.Button("设置"))
{
TextureImporterPlatformSettings t = new TextureImporterPlatformSettings() {
allowsAlphaSplitting = ifAllowsAlphaSplitting,
format = textureFormat,
maxTextureSize = (int)textureSize,
textureCompression = textureCompression
};
SelectedChangeTextureFormatSettings(t, textureType, textureTag, isAndroid, isIOS);
}
}
static void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t, TextureImporterType _type, string _tag, bool _isAndroid, bool _isIOS)
{
Object[] textures = GetSelectedTextures();
if (window == null)
Init();
if (textures != null)
{
if (textures.Length < 1)
{
window.ShowNotification(new GUIContent("找不到图片!"));
return;
}
}
else
{
window.ShowNotification(new GUIContent("请选中图片或路径!"));
return;
}
Selection.objects = new Object[0];
int i = 0;
foreach (Texture2D texture in textures)
{
string path = AssetDatabase.GetAssetPath(texture);
//Debug.Log("path: " + path);
string[] pathArr = path.Split('/');
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.textureType = _type;
textureImporter.wrapMode = TextureWrapMode.Clamp;
textureImporter.filterMode = FilterMode.Bilinear;
if (_tag == "默认为父文件夹名" || _tag == "")
textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];
else
textureImporter.spritePackingTag = _tag;
textureImporter.SetPlatformTextureSettings(_t);
if (_isAndroid)
{
_t.name = "Android";
_t.overridden = true;
textureImporter.SetPlatformTextureSettings(_t);
}
else
{
_t.name = "Android";
_t.overridden = false;
textureImporter.SetPlatformTextureSettings(_t);
}
if (_isIOS)
{
_t.name = "iPhone";
_t.overridden = true;
textureImporter.SetPlatformTextureSettings(_t);
}
else
{
_t.name = "iPhone";
_t.overridden = false;
textureImporter.SetPlatformTextureSettings(_t);
}
ShowProgress((float)i / (float)textures.Length, textures.Length, i);
i++;
AssetDatabase.ImportAsset(path);
}
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
textures = null;
}
public static void ShowProgress(float val, int total, int cur)
{
EditorUtility.DisplayProgressBar("设置图片中...", string.Format("请稍等({0}/{1}) ", cur, total), val);
}
static Object[] GetSelectedTextures()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
void OnInspectorUpdate()
{
Repaint();
}
}
[小工具记录]批量修改png图片格式
最新推荐文章于 2024-07-08 16:03:49 发布