Unity图片压缩设置批量检查

检查点整理

合理的设置可以减少内存的占用,以我们游戏的安卓平台为例:

  • 美术输出的图片资源,分辨率最大不超过1024*1024
  • 在Unity中,Max Size设置为1024
  • Format设置为Compressed
  • Compression Quality设置为Best
  • 勾选Compress using ETC1

脚本实现

开发环境:Python2.7+Win10+Pycharm

美术输出的图片资源,分辨率最大不超过1024*1024

这一项可以使用Python的Pillow库,来获取图片的分辨率。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def get_files_by_suffix(path, suffixes=("txt", "xml")):
file_list = []
for root, dirs, files in os.walk(path):
for afile in files:
file_suffix = os.path.splitext(afile)[1][1:].lower()
if file_suffix in suffixes:
file_list.append(os.path.join(root, afile))
return file_list
pic = get_files_by_suffix("C:\Project\sg\sg\Assets\Res\zh-CN", ('jpg', 'png'))
for i in pic:
print i
p = Image.open(i)
x, y = p.size
p.close()
if x != y:
print 'size不相等{}*{}'.format(x, y)
if x > 1024 or y > 1024:
print 'size大于1024'

在Unity中,Max Size设置为1024
Format设置为Compressed
Compression Quality设置为Best
勾选Compress using ETC1

这里可以有两种实现方式:

Python方式

通过读取图片的meta文件,检查文件中的以下设置是否正确。

1
2
3
4
5
- buildTarget: Android
maxTextureSize: 2048
textureFormat: -1
compressionQuality: 100
allowsAlphaSplitting: 0

C#方式

通过Unity的api,读取相应的设置,进行检查。核心代码如下:

pic变量是图片的路径,如 @”Assets/Res/zh-CN/Battle/Texture/BG/BG_001.jpg”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
TextureImporter ti = AssetImporter.GetAtPath(pic) as TextureImporter;
int maxSize = 0;
TextureImporterFormat format;
int compressionQuality = 0;
ti.GetPlatformTextureSettings("Android", out maxSize, out format, out compressionQuality);
Debug.LogWarning(string.Format("strat to check {0}",pic));
if (maxSize != 1024) {
Debug.LogError(string.Format("{0},max size is not 1024,now is {1}", pic, maxSize));
}
if(format != TextureImporterFormat.AutomaticCompressed) {
Debug.LogError(string.Format("{0},format is not compressed,now is {1}", pic, format));
}
if (compressionQuality != 100) {
Debug.LogError(string.Format("{0},compression quality is not best,now is {1}", pic, compressionQuality.ToString()));
}
if(pic.ToLower().IndexOf("png") > -1) {
if (!ti.GetAllowsAlphaSplitting()) {
Debug.LogError(string.Format("{0},compress using ETC1 is not true", pic));
}
}