Unity,APK资源优化

来源:互联网 发布:网络诈骗举报电话多少 编辑:程序博客网 时间:2024/05/16 02:05

Unity,APK大小优化
一个手机游戏到了开发的后期,一个最重要的步骤就是优化APK包的大小。主要优化贴图,模型,和声音。本文主要讲述如何批量优化这三种资源。
这里写图片描述

对于贴图资源可以修改贴图的最大Max Size 和Format来减少贴图资源的大小(也只能用这种方法)。如何项目中有好几十张贴图需要做这有的优化,或者需要在高清版和低配版之前切换,这样的修改就有些蛋疼了。这种情况可以使用Unity的编辑器代码来批量修改。
static void CompressTexture(string strPath)        {             int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            TextureImporter tImp= (TextureImporter)TextureImporter.GetAtPath(strPath);            tImp.textureFormat=TextureImporterFormat.ARGB16;            //tImp.maxTextureSize=tImp.maxTextureSize/2;            tImp.SetPlatformTextureSettings("Android",tImp.maxTextureSize/2,TextureImporterFormat.ARGB16);            AssetDatabase.ImportAsset(strPath);        }

通过以上代码修改一个贴图在Android平台的的大小和格式。如果是其他平台的,可以使用SetPlatformTextureSettings函数做具体的修改。

对于声音,可以修改Audio Format 和 Compression 来控制大小。

这里写图片描述

static void CompressAudio(string strPath)        {            int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            AudioImporter aImp=(AudioImporter)AudioImporter.GetAtPath(strPath);            aImp.threeD=false;            aImp.compressionBitrate=32;            AssetDatabase.ImportAsset(strPath);        }

对于3d 模型可以修改 模型压缩和动画帧来修改资源大小。

static void CompressFbx(string strPath)        {            int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            ModelImporter mImp=(ModelImporter)ModelImporter.GetAtPath(strPath);            mImp.meshCompression=ModelImporterMeshCompression.High;            mImp.animationCompression=ModelImporterAnimationCompression.KeyframeReductionAndCompression;            AssetDatabase.ImportAsset(strPath);        }

完整代码
修改函数 GetAllAsset() 中的 GetAssetFile()资源路径,确定要修改的资源的路径。

using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEditor;using System.IO;namespace CompressToolkit{public class CTWindow : EditorWindow {  static    CTWindow window;  [MenuItem("ComTool/Compression")]    static void AddCTWindow()    {        //创建窗口        Rect  wr = new Rect (0,0,450,500);        window = (CTWindow)EditorWindow.GetWindowWithRect (typeof (CTWindow),wr,true,"CompressionToolKit");         window.Show();    }    void OnGUI()    {        if(GUILayout.Button("Get All Assets(Compress)"))        {                //GetAllAsset();                ReadLogFile LogF=new ReadLogFile();                LogF.ReadLog();        }    }    static void GetAllAsset()    {            Debug.Log("GetAllAssets");            TextureAssetList.Clear();            AudioAssetsList.Clear();            FbxAssetsList.Clear();            //GetAssetFile("Assets/Textures");            //GetAssetFile("Assets/Image");            //GetAssetFile("Assets/Audio");            GetAssetFile("Assets/NewModle");            LogList();            DealTexture();            DealAudio();            DealFbx();            AssetDatabase.Refresh();    }        static  List<string> TextureAssetList=new List<string>();        static      List<string> AudioAssetsList=new List<string>();        static      List<string> FbxAssetsList=new List<string>();        static      string[] TextureFilter={".png",".jpg"};        static      string[] AudioFilter={".mp3",".ogg",".wav",".wma"};        static  string[] FbxFilter={".fbx",".FBX"};        static bool Contain(string ex,string[] collection)        {            //Debug.Log(ex);            for(int i=0;i<collection.Length;++i)            {                if(collection[i]==ex)                    return true;            }            return false;        }    public static void GetAssetFile(string dir)    {            //Debug.Log(dir);        foreach (string d in Directory.GetFileSystemEntries(dir))        {                //Debug.Log(d);            if (File.Exists(d))            {                FileInfo fi = new FileInfo(d);                    string exten=fi.Extension;                    if(Contain(exten,TextureFilter))                    {                        TextureAssetList.Add(fi.FullName);                        //Debug.Log(fi.FullName);                    }else if(Contain(exten,AudioFilter))                    {                        AudioAssetsList.Add(fi.FullName);                        //Debug.Log(fi.FullName);                    }else if(Contain (exten,FbxFilter))                    {                        FbxAssetsList.Add(fi.FullName);                        //Debug.Log(fi.FullName);                    }            }            else            {                DirectoryInfo d1 = new DirectoryInfo(d);                if (d1.GetFiles().Length != 0)                {                    GetAssetFile(d1.FullName);////递归                }            }        }    }  static  void LogList()        {            for(int i=0;i<TextureAssetList.Count;++i)            {                Debug.Log(TextureAssetList[i]);            }            for(int i=0;i<AudioAssetsList.Count;++i)            {                Debug.Log(AudioAssetsList[i]);            }            for(int i=0;i<FbxAssetsList.Count;++i)            {                Debug.Log(FbxAssetsList[i]);            }        }        #region  Compress Assets /         static void DealTexture()        {            for(int i=0;i<TextureAssetList.Count;++i)            {                string strPath=TextureAssetList[i];                CompressTexture(strPath);            }        }        static void CompressTexture(string strPath)        {             int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            TextureImporter tImp= (TextureImporter)TextureImporter.GetAtPath(strPath);            tImp.textureFormat=TextureImporterFormat.ARGB16;            //tImp.maxTextureSize=tImp.maxTextureSize/2;            tImp.SetPlatformTextureSettings("Android",tImp.maxTextureSize/2,TextureImporterFormat.ARGB16);            AssetDatabase.ImportAsset(strPath);        }        static void DealAudio()        {            for(int i=0;i<AudioAssetsList.Count;++i)            {                string strPath=AudioAssetsList[i];                CompressAudio(strPath);            }        }        static void CompressAudio(string strPath)        {            int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            AudioImporter aImp=(AudioImporter)AudioImporter.GetAtPath(strPath);            aImp.threeD=false;            aImp.compressionBitrate=32;            AssetDatabase.ImportAsset(strPath);        }        static void DealFbx()        {            for(int i=0;i<FbxAssetsList.Count;++i)            {                string strPath=FbxAssetsList[i];                CompressFbx(strPath);            }        }        static void CompressFbx(string strPath)        {            int  index= strPath.IndexOf("Assets");            strPath=strPath.Remove(0,index);            ModelImporter mImp=(ModelImporter)ModelImporter.GetAtPath(strPath);            mImp.meshCompression=ModelImporterMeshCompression.High;            mImp.animationCompression=ModelImporterAnimationCompression.KeyframeReductionAndCompression;            AssetDatabase.ImportAsset(strPath);        }        #endregion }}
0 0
原创粉丝点击