1. 废话描述
Unity中伴随着大量的图片资源,像模型贴图,UI图片等,有时设计那边导给程序的图片就很大,例如2048*2048,4096*4096等,在运行的时候内存直接爆棚,我的做法是将图片的分辨率大小降低,在能保证效果的程度上尽可能的低,但是有时图片的数量很多,操作不方便,因此写了个一个简单的快速设置分辨率的小工具,记录并分享一下~~
2. 上代码
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 纹理最大分辨率
/// </summary>
public enum TextureMaxSize
{
MaxSize_32 = 32,
MaxSize_64 = 64,
MaxSize_128 = 128,
MaxSize_256 = 256,
MaxSize_512 = 512,
MaxSize_1024 = 1024,
MaxSize_2048 = 2048,
MaxSize_4096 = 4096,
MaxSize_8192 = 8192
}
public enum EPlatform
{
iPhone,
Android,
}
public class SpritesEditorManager : EditorWindow
{
public TextureMaxSize curMaxSize = TextureMaxSize.MaxSize_1024;//最大分辨率
//保存当前设置的格式
public TextureFormat curTF = TextureFormat.ASTC_RGBA_6x6, setTF;
public GUIStyle enumStyle1;//枚举样式1
//保存当前设置的平台
public EPlatform curPl, setPl;
//是否强制设置成RGBA, false : 使用原有的RGB
public bool isConvertRGBA = true;
//是否是一个文件
public bool _isFile;
[MenuItem("Tools/ccv/设置图片压缩格式")]
static void SetTextureFormat()
{
EditorWindow.GetWindow<SpritesEditorManager>(false, "设置Texture", true).Show();
}
new void Show()
{
//设置绘制下拉框的格式
enumStyle1 = new GUIStyle(EditorStyles.popup);
enumStyle1.fontSize = 10;
enumStyle1.fixedHeight = 20;
enumStyle1.fixedWidth = 500;
}
void OnGUI()
{
GUILayout.Space(20);
if (Selection.objects.Length <= 0)
GUILayout.Label("请先选择一个文件夹!!! ");
else
GUILayout.Label("当前选中的文件夹: " + AssetDatabase.GetAssetPath(Selection.objects[0]));
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("图片最大分辨率: ");
curMaxSize = (TextureMaxSize)EditorGUILayout.EnumPopup(curMaxSize, enumStyle1);
GUILayout.EndHorizontal();
GUILayout.Space(10);
if (GUILayout.Button("开始设置"))
{
if (!CheckSelection())
return;
Object[] textures = Selection.GetFiltered<Texture2D>(SelectionMode.DeepAssets);
//设置最大分辨率
ParseTextureMaxSize(textures);
AssetDatabase.Refresh();
}
//GUILayout.Label("设置格式: ");
//setTF = (TextureFormat)EditorGUILayout.EnumPopup(curTF, enumStyle1);
//isConvertRGBA = EditorGUILayout.ToggleLeft("是否将RGB强制转成RGBA", isConvertRGBA);
//GUILayout.Label("");
}
/// <summary>
/// 解析设置Texture的最大分辨率
/// </summary>
/// <param name="textures"></param>
public void ParseTextureMaxSize(object[] textures)
{
int index = 0;
int total = textures.Length;
foreach (Texture2D texture in textures)
{
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
//不处理类型为“Lightmap”的Texture
if (texImporter.maxTextureSize != (int)curMaxSize && "Lightmap" != texImporter.textureType.ToString())
{
texImporter.maxTextureSize = (int)curMaxSize;
AssetDatabase.ImportAsset(path);
}
EditorUtility.DisplayCancelableProgressBar("正在设置Texture最大分辨率...", index + "/" + total, (float)index / total);
if (cansel)
{
EditorUtility.ClearProgressBar();
return;
}
index++;
}
EditorUtility.ClearProgressBar();
//AssetDatabase.Refresh();
}
/// <summary>
/// 检查是否选中的文件
/// </summary>
/// <returns></returns>
public bool CheckSelection()
{
if (Selection.objects.Length <= 0)
{
Debug.LogError("请先选择一个文件!!! ");
return false;
}
_isFile = false;
string selectPath = AssetDatabase.GetAssetPath(Selection.objects[0]);
if (File.GetAttributes(selectPath).CompareTo(FileAttributes.Directory) == 1)
{
Debug.LogError("选择了一个文件!!!");
_isFile = true;
}
return true;
}
}
3. 用法
把这个脚本直接放在Editor文件夹下即可。
然后在菜单栏就可以看到操作按钮了,点击后弹出操作界面。
在Project面板选中要改变的文件夹,操作面板的当前选中文件夹显示当前选中的文件夹,设置好要转变成的分辨率是多少后,点击开始设置即可转换。