- unity 可以通过修改图片格式来减少包体大小,不同平台对不用格式图片的加载速度,也有差别.在开发WebGl是因为没有开发好的工具,要对图片进行一个处理,自己写了一个脚本工具类,来进行图片适配WebGl端.
- 最后的效果,用过选择文件夹来处理文件夹下的图片,不用人工一张一张设置了.
代码:
先创建一个TextureImportChanging类,继承EditorWindow类.
主要方法:先设置一个设置图片格式的界面,
using UnityEditor;
using UnityEngine;
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,
}
private static TextureImporterFormat textureImporterFormat
{
get
{
#if UNITY_WEBGL
return TextureImporterFormat.DXT5Crunched;
#else
return TextureImporterFormat.Automatic;
#endif
}
}
// ----------------------------------------------------------------------------
static TextureImporterType textureType = TextureImporterType.Sprite;
/// <summary>
/// 格式
/// </summary>
static TextureImporterFormat textureFormat = textureImporterFormat;
MaxSize textureSize = MaxSize.Size_1024;
/// <summary>
/// 压缩
/// </summary>
TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;
static TextureWrapMode wrapMode = TextureWrapMode.Repeat;
static FilterMode filterMode = FilterMode.Bilinear;
bool ifAllowsAlphaSplitting = true;
/// <summary>
/// 是否允许Mipmap
/// </summary>
static bool ifMipmapEnabled = false;
/// <summary>
/// 是否重置图片属性
/// </summary>
static bool textureModifier = true;
/// <summary>
/// 是否改变图片类型
/// </summary>
bool isChangeTextureType = false;
/// <summary>
/// 是否固定的改变图片格式
/// </summary>
static bool isChangeTexturFormat = false;
/// <summary>
/// 是否统一图片最压缩大尺寸,true 所选的图片的override的maxSize都会设置为所选尺寸,false都设置为图片原生尺寸的宽高取最大值
/// </summary>
static bool isUnificationSize = false;
static TextureImportChanging window;
[@MenuItem("资源管理/设置图片格式")]
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();
isChangeTextureType = EditorGUILayout.Toggle("是否改变图片类型:" , isChangeTextureType);
if (isChangeTextureType)
textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:" , textureType);
EditorGUILayout.Space();
isChangeTexturFormat = EditorGUILayout.Toggle("是否固定改变图片格式:" , isChangeTexturFormat);
if (isChangeTexturFormat)
textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:" , textureFormat);
EditorGUILayout.Space();
textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:" , textureCompression);
ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:" , ifAllowsAlphaSplitting);
ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:" , ifMipmapEnabled);
textureModifier = EditorGUILayout.Toggle("是否重置图片属性:" , textureModifier);
if (!textureModifier)
{
EditorGUILayout.Space();
EditorGUILayout.HelpBox("如果有导入自动设置图片脚本,下面方法未必执行\n 统一OverrideSize值,则所选图片的该值都设置为所选大小,否则为图片的宽高最大值" , MessageType.Info);
EditorGUILayout.Space();
isUnificationSize = EditorGUILayout.Toggle("是否统一Override MaxSize:" , isUnificationSize);
if (isUnificationSize)
textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:" , textureSize);
wrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup("wrapMode:" , wrapMode);
filterMode = (FilterMode)EditorGUILayout.EnumPopup("filterMode:" , filterMode);
}
EditorGUILayout.Space();
if (GUILayout.Button("设置"))
{
TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();
t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
t.format = textureFormat;
t.maxTextureSize = (int)textureSize;
t.textureCompression = textureCompression;
SelectedChangeTextureFormatSettings(t , textureType , GetSelectedTextures());
}
//if (GUILayout.Button("一键设置(Textures目录)")) {
// TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();
// t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
// t.format = textureFormat;
// t.maxTextureSize = (int)textureSize;
// t.textureCompression = textureCompression;
// SelectedChangeTextureFormatSettings(t, textureType,GetTexture());
//}
}
void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t , TextureImporterType _type , Object[] arr)
{
Object[] textures = arr;
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);
if (path.Substring(path.Length - 3 , 3) == "dds")
continue;
string[] pathArr = path.Split('/');
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (!isUnificationSize)
_t.maxTextureSize = GetMaxSize(texture.height , texture.width);
if (isChangeTextureType)
textureImporter.textureType = _type;
textureImporter.allowAlphaSplitting = _t.allowsAlphaSplitting;
textureImporter.mipmapEnabled = ifMipmapEnabled;
textureImporter.borderMipmap = ifMipmapEnabled;
textureImporter.textureCompression = _t.textureCompression;
if (!textureModifier)
{
textureImporter.maxTextureSize = _t.maxTextureSize;
textureImporter.wrapMode = wrapMode;
textureImporter.filterMode = filterMode;
textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];
TextureImporterPlatformSettings webglPlatFormSetting = textureImporter.GetPlatformTextureSettings("WebGl");
webglPlatFormSetting.overridden = true;
webglPlatFormSetting.format = GetFormat(textureImporter , "WebGl");
webglPlatFormSetting.maxTextureSize = _t.maxTextureSize;
textureImporter.SetPlatformTextureSettings(webglPlatFormSetting);
TextureImporterPlatformSettings pcPlatFormSetting = textureImporter.GetPlatformTextureSettings("PC");
pcPlatFormSetting.overridden = true;
pcPlatFormSetting.format = GetFormat(textureImporter , "PC");
pcPlatFormSetting.maxTextureSize = _t.maxTextureSize;
textureImporter.SetPlatformTextureSettings(pcPlatFormSetting);
}
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);
}
static TextureImporterFormat GetFormat(TextureImporter item , string platformName)
{
if (isChangeTexturFormat)
return textureFormat;
TextureImporterFormat format = textureFormat;
bool isNormalMap = item.textureType == TextureImporterType.NormalMap;
switch (platformName)
{
case "WebGl":
format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5Crunched : TextureImporterFormat.DXT1Crunched;
break;
case "PC":
format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5 : TextureImporterFormat.DXT1;
break;
default:
break;
}
return format;
}
static int GetMaxSize(int height , int width)
{
return height >= width ? height : width;
}
void OnInspectorUpdate()
{
Repaint();
}
}
第一次写这种工具,也是在别人的基础上修改的,有大佬可以指出里面逻辑不对的地方
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/109257.html