Unity Texture优化和格式统一设置

using System;
using System.IO;
using UnityEditor;

namespace Assets.Optimize
{
    /// <summary>
    /// 图片格式优化和统一
    /// </summary>
    public class TextureOptimize
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dirPath">图片所在目录</param>
        public void FormatSetting(string dirPath)
        {
            if (string.IsNullOrEmpty(dirPath))
                return;
            if (Directory.Exists(dirPath))
            {
                //1. 遍历文件夹
                DirectoryInfo direction = new DirectoryInfo(dirPath);
                FileInfo[] files = direction.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                //2. 收集所有的图片
                var doCount = 0;
                for (int i = 0; i < files.Length; ++i)
                {
                    var path = files[i].FullName;
                    if (path.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase) || path.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))
                    {
                        path = path.Replace('\\', '/');
                        var name = files[i].Name;
                        //3. 设置图片的格式 
                        if (SetTextureFormat(path))
                        {
                            ++doCount;
                        }
                    }
                }
                UnityEngine.Debug.LogErrorFormat("优化完成,共优化{0}张图片", doCount);
            }
        }

        /// <summary>
        /// 改变Texture的格式属性
        /// Unity官方关于图片属性的说明:
        /// https://docs.unity3d.com/Manual/class-TextureImporter.html
        /// </summary>
        /// <param name="texPath">图片路径</param>
        private bool SetTextureFormat(string texPath)
        {
            //TODO  可以配置个不做格式更改和压缩的白名单目录(_whiteNames)进行过滤
            texPath = texPath.Replace('\\', '/');
            AssetImporter importer = AssetImporter.GetAtPath(texPath);
            bool isChanged = false;
            if (importer != null && importer is UnityEditor.TextureImporter)
            {
                //TODO if (!_whiteNames.Contains(fileName))
                TextureImporter textureImporter = (TextureImporter)importer;
                //禁用读写,在内部,Unity 使用纹理数据的副本进行脚本访问,这使纹理所需的内存量增加了一倍。
                if (textureImporter.isReadable != false)
                {
                    isChanged = true;
                    textureImporter.isReadable = false;
                }
                //如果是UI上用的Texture就可以设置成GUI模式
                if (textureImporter.textureType != TextureImporterType.GUI)
                {
                    isChanged = true;
                    textureImporter.textureType = TextureImporterType.GUI;
                }
                //关闭mipmap,耗内存
                if (textureImporter.mipmapEnabled != false)
                {
                    isChanged = true;
                    textureImporter.mipmapEnabled = false;
                }
                //指定如何生成纹理的Alpha,这里使用图片自带的Alpha通道
                if (textureImporter.alphaSource != TextureImporterAlphaSource.FromInput)
                {
                    isChanged = true;
                    textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;
                }
                //启用此属性以扩大颜色并避免过滤边缘上的伪影。
                if (textureImporter.alphaIsTransparency != true)
                {
                    isChanged = true;
                    textureImporter.alphaIsTransparency = true;
                }
                //设置图片二次幂(NPOT)的缩放属性,在导入时将纹理缩放到最接近的二维大小。
                //比如:一个 257x511 像素的纹理被缩放到 256x512 像素
                if (textureImporter.npotScale != TextureImporterNPOTScale.ToNearest)
                {
                    isChanged = true;
                    textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
                }
                //设置最大导入纹理尺寸。这里数值自己定义,比如UI以1280为基准设计就设置成1024,以1920为基准设计就设置成2048
                if (textureImporter.maxTextureSize != 1024)
                {
                    isChanged = true;
                    textureImporter.maxTextureSize = 1024;
                }
                //设置图片压缩模式,就使用正常压缩模式
                if (textureImporter.textureCompression != TextureImporterCompression.Compressed)
                {
                    isChanged = true;
                    textureImporter.textureCompression = TextureImporterCompression.Compressed;
                }
                //设置压缩格式,这个就各自选择了
                if (textureImporter.textureFormat != TextureImporterFormat.ASTC_RGBA_4x4)
                {
                    isChanged = true;
                    textureImporter.textureFormat = TextureImporterFormat.ASTC_RGBA_4x4;
                }
                if (isChanged)
                {
                    //刷新图片
                    AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceUpdate);
                }
            }
            return isChanged;
        }
    }
}

主要逻辑,按照项目所需,统一Texture格式和压缩方式,可以举一反三。

Unity - Manual: Texture Import Settings

《Unity性能优化》系列课程笔记——第叁节 - 哔哩哔哩

上门两篇文章可以多看看,了解下原理,了解下怎么降内存和适配移动平台。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值