Unity Texture optimization and unified format settings

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;
        }
    }
}

The main logic is to unify the Texture format and compression method according to the needs of the project, and you can draw inferences from one example.

Unity - Manual: Texture Import Settings

"Unity Performance Optimization" Series Course Notes - Section 3 - Bilibili

You can read more of the two articles here to understand the principles and how to reduce memory and adapt to mobile platforms.

Guess you like

Origin blog.csdn.net/u013476751/article/details/127686541