Unity关于纹理图片格式带来的内存问题和对预制体批量格式和大小减半处理

文章讲述了在Android打包过程中遇到的内存问题,由于图片默认格式被改为RGBA32,导致内存增加。作者提供了一种方法,即检测并批量处理预设中的图片格式,将其转换为自动格式并调整纹理尺寸以减小内存占用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们经常会遇到内存问题,这次就是遇到很多图片的默认格式被改成了RGB32,导致Android打包后运行内存明显增加。

发生了什么

打包Android后,发现经常崩溃,明显内存可能除了问题,看了内存后发现了问题。
见下图:
在这里插入图片描述
实际被改成了RGBA 32,如下图
在这里插入图片描述
因为安卓端是没覆写的,所以导致格式就是rgb32
在这里插入图片描述

如何处理

那么如何处理能,最好一键处理。我是这样做的,我对我的需要打包的预设,例如场景,角色等等资源批量检测他们中间引用了哪些图片,然后对这些图片进行处理,因为很多图片是过度文件不打包,进行处理也没必要,白白浪费时间。

代码片段,这里的cfg.path是需要打包prefab的路径,这里只有代码片段是因为我的打包逻辑是自己写的,篇幅问题只能放上核心代码。
代码如下:

string[] file = AssetDatabase.GetDependencies(cfg.path, true);

foreach (var dep in file)
{
    string strExt = System.IO.Path.GetExtension(dep).ToLower();
    if (!exps.Contains(strExt))
        continue;
    TextureImporter textureImporter = AssetImporter.GetAtPath(dep) as TextureImporter;
    TextureImporterPlatformSettings set = textureImporter.GetDefaultPlatformTextureSettings();// .GetPlatformTextureSettings(path);
    if (set.format != TextureImporterFormat.Automatic)
    {
        if (changeFormatAuto)
        {
            set.format = TextureImporterFormat.Automatic;
            textureImporter.SetPlatformTextureSettings(set);
            //EditorUtility.SetDirty(textureImporter);

            // 重新导入资源,否则更改并未生效。
            // 如资源未执行重新导入,则会在项目保存时自动导入、生效
            AssetDatabase.ImportAsset(dep);

            Debug.Log("修改预设" + cfg.prefabname + "中的图片资源格式存在的隐患:" + dep, textureImporter);
            checkok = false;
            allcount++;
        }
        else
        {
            Debug.LogError("预设" + cfg.prefabname + "中的图片资源格式存在隐患:" + dep, textureImporter);
            allcount++;
            checkok = false;
        }
        continue;
    }
}

cfg.path就是perfab所在的位置。传入就可以了
脚本会自动改为Automatic格式。

最后再观察内存,图片找不到了,我放了其他的同尺寸图和一张4k的图,ASTC格式的2k大概是4.8M,4k只有19m内存。
在这里插入图片描述

在这里插入图片描述
最后希望大家有一个干净的资源包。

缩小纹理

可能PC转Android的时候,还希望能缩小纹理尺寸,可以利用这个对图片这样来处理。

//把图片的质量对半砍掉,0表示不砍掉,1:砍一半,2:1/4
public static bool CutHalfPicture(int sizeLevel = 1)
{
    List<string> exps = new List<string>()
    { ".bmp",".jpg",".jpeg",".png",".tif",".psd",".tga"};
    bool checkok = true;

    int[] textureSizes = new int[] {
    32,
    64,
    128,
    256,
    512,
    1024,
    2048,
    4096,
    8192,
    16384,
    };
    int allcount = 0;

    if (ResJsonObjectList == null || ResJsonObjectList.Count == 0)
        JResAssetJson.ReadResJson();


    for (int i = 0; i < ResJsonObjectList.Count; i++)
    {
        ResJson cfg = ResJsonObjectList[i];
        if (cfg.isab != 1)
            continue;
        
        string[] file = AssetDatabase.GetDependencies(cfg.path, true);

        foreach (var dep in file)
        {
            string strExt = System.IO.Path.GetExtension(dep).ToLower();
            if (!exps.Contains(strExt))
                continue;
            TextureImporter textureImporter = AssetImporter.GetAtPath(dep) as TextureImporter;
            TextureImporterPlatformSettings set = textureImporter.GetDefaultPlatformTextureSettings();// .GetPlatformTextureSettings(path);
                                                                                                      //Debug.Log(set.maxTextureSize+",");


            int width, height, max;
            //Texture2D tex = AssetDatabase.LoadAssetAtPath(dep, typeof(Texture2D)) as Texture2D;

            //width = tex.width; height = tex.height;
			//这里更正
			object[] args = new object[2] { 0, 0 };
			MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
			mi.Invoke(textureImporter, args);
			
			width = (int)args[0];
			height = (int)args[1];

            max = Mathf.Max(width, height);
            int size = 32; //Default size
            for (int j = 0; j < textureSizes.Length; j++)
            {
                if (textureSizes[j] >= max)
                {
                    size = textureSizes[j];
                    break;
                }
            }
            //set.maxTextureSize = 16384;
            //Debug.Log(size);
            size = (int)(size / Mathf.Pow(2, sizeLevel));
            if (size < 32)
                size = 32;
            if (set.maxTextureSize != size)
            {
                set.maxTextureSize = size;

                textureImporter.SetPlatformTextureSettings(set);

               
                AssetDatabase.ImportAsset(dep);

                Debug.Log("修改预设" + cfg.prefabname + "中的图片:" + dep + " ,大小:" + set.maxTextureSize, textureImporter);
                checkok = false;
                allcount++;
            }
            continue;

        }
    }
    Debug.Log("累计共修改" + allcount + "处图片。");
    return checkok;
}

代码中的ResJsonObjectList和JResAssetJson.ReadResJson()是预设列表和获取预设列表的方法,还是篇幅问题,这里无法放出。可以搜一下如何获取所有预设(prefab)的文章。

补充2024/1/23,后来发现这里的获取图片大小是当前的Unity的大小,所以有点问题,改为

object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(textureImporter, args);

width = (int)args[0];
height = (int)args[1];

如果对你有用,请点赞。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thinbug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值