Unity用代码设置图片的压缩格式(AssetImporter/TextureImporter)

前言

项目中,图集图片的压缩格式可能需要统一处理,如果挨个挨个设置实在太麻烦了,可以使用代码对图片压缩格式进行设置。

代码示例

以设置NGUI的图集的png大图的压缩格式在Android平台设置为ETC 4为例

using UnityEditor;
using System.IO;

// 获取目录中的所有预设
var fs = Directory.GetFiles(Application.dataPath + "\\GameRes\\Atlas", "*.prefab", SearchOption.AllDirectories);
foreach(var f in fs)
{
	var assetPath = f.Replace(Application.dataPath, "Assets");
	var atlas = AssetDatabase.LoadAssetAtPath<UIAtlas>(assetPath);
	if(null == atlas) continue;
	// 获取图集的png大图
	var	texture = atlas.spriteMaterial.mainTexture;
	
	var textureAssetPath = AssetDatabase.GetAssetPath(texture);
	// Android平台设置
	var imp = AssetImporter.GetAtPath(textureAssetPath) as TextureImporter;
	var setting = imp.GetPlatformTextureSettings("Android");
	// 设置成ETC 4
	setting.format = TextureImporterFormat.ETC_RGB4;
	imp.SetPlatformTextureSettings(setting);
	// 保存设置
	AssetDatabase.ImportAsset(textureAssetPath);
}

猜你喜欢

转载自blog.csdn.net/linxinfa/article/details/106288249
今日推荐