|
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class ChangeTexSettingWindow :EditorWindow
{
enum TargetPlatform
{
Standalone,
IOS,
Android
}
[MenuItem("Tools/ChangeTexSettingsWindow")]
public static void OpenChangeTexSettingsWindow()
{
ChangeTexSettingWindow window = EditorWindow.CreateInstance<ChangeTexSettingWindow>();
window.Show();
}
string TexPath;
string TexSuffix = &#34;*.bmp|*.jpg|*.gif|*.png|*.tif|*.psd&#34;;
TargetPlatform SelectPlatform = TargetPlatform.Android;
TextureImporterFormat WithAlpha = TextureImporterFormat.ASTC_4x4;
TextureImporterFormat WithoutAlpha = TextureImporterFormat.PVRTC_RGB4;
private void OnGUI()
{
GUILayout.BeginHorizontal();
TexPath = EditorGUILayout.TextField(&#34;图片资源路径&#34;, TexPath);
if (GUILayout.Button(EditorGUIUtility.IconContent(&#34;Folder Icon&#34;), GUILayout.Width(18), GUILayout.Height(18)))
{
TexPath = EditorUtility.OpenFolderPanel(&#34;图片路径选择&#34;, &#34;&#34;, &#34;&#34;);
}
GUILayout.EndHorizontal();
GUI.enabled = false;
GUILayout.BeginHorizontal();
TexSuffix = EditorGUILayout.TextField(&#34;图片格式&#34;, TexSuffix);
GUILayout.EndHorizontal();
GUI.enabled = true;
GUILayout.BeginHorizontal();
SelectPlatform = (TargetPlatform)Enum.Parse(typeof(TargetPlatform), EditorGUILayout.EnumPopup(&#34;选择目标平台&#34;, SelectPlatform).ToString());
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
WithAlpha = (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), EditorGUILayout.EnumPopup(&#34;有Alpha通道&#34;, WithAlpha).ToString());
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
WithoutAlpha = (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), EditorGUILayout.EnumPopup(&#34;没有Alpha通道&#34;, WithoutAlpha).ToString());
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button(&#34;转换&#34;))
{
if (string.IsNullOrEmpty(TexPath) || !Directory.Exists(TexPath))
{
EditorUtility.DisplayDialog(&#34;错误&#34;, &#34;路径不能为空或路径不存在&#34;, &#34;确定&#34;);
return;
}
if (string.IsNullOrEmpty(TexSuffix))
{
EditorUtility.DisplayDialog(&#34;错误&#34;, &#34;路径不能为空或路径不存在&#34;, &#34;确定&#34;);
return;
}
List<string> lst = GetAllTexPaths(TexPath);
TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings();
settings.name = SelectPlatform.ToString();
settings.crunchedCompression = true;
settings.overridden = true;
int i = 0;
EditorUtility.DisplayProgressBar(&#34;修改&#34;, &#34;修改图片格式&#34;, 0);
for (; i < lst.Count; i++)
{
Change(lst, settings);
EditorUtility.DisplayProgressBar(&#34;转换&#34;, string.Format(&#34;修改图片格式 {0}/{1}&#34;, i, lst.Count), i / (float)lst.Count);
}
AssetDatabase.SaveAssets();
EditorUtility.ClearProgressBar();
}
GUILayout.EndHorizontal();
}
private void Change(string path, TextureImporterPlatformSettings platformSettings)
{
path = path.Substring(path.IndexOf(&#34;Assets&#34;));
try
{
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter.DoesSourceTextureHaveAlpha())
{
platformSettings.format = WithAlpha;
}
else
{
platformSettings.format = WithoutAlpha;
}
textureImporter.SetPlatformTextureSettings(platformSettings);
textureImporter.SaveAndReimport();
AssetDatabase.ImportAsset(path);
}
catch
{
AssetDatabase.SaveAssets();
EditorUtility.ClearProgressBar();
}
}
private List<string> GetAllTexPaths(string rootPath)
{
List<string> lst = new List<string>();
string[] types = TexSuffix.Split(&#39;|&#39;);
for (int i = 0; i < types.Length; i++)
{
lst.AddRange(Directory.GetFiles(rootPath, types, SearchOption.AllDirectories));
}
return lst;
}
}那个图片格式不知道怎么正则匹配,所以先这么写了 |
|