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
- Unity,APK资源优化
- unity资源优化插件
- Unity 声音资源优化
- unity优化—资源优化
- [Unity优化]资源优化之纹理优化
- unity 图片资源导入优化
- Unity美术资源的优化
- Unity开发优化之资源优化
- Unity笔记 资源加载 内存优化
- 【unity优化七】美术资源要求
- Unity优化相关总结(资源)
- 【Unity】骨骼动画如何优化资源
- Unity优化杂谈3(资源)
- Disunity_V0.5.0 提取Unity生成的Apk的资源
- Android应用优化(3)APK资源图片优化
- 【UNITY填坑日记】unity图片资源优化神器dither4444
- Unity 资源优化方法及移动设备上优化
- Android资源图片的优化减小apk的体积
- 《30天自制操作系统》学习笔记(一)
- C++利用反射和简单工厂模式实现业务模块解耦
- Download模块 (二十)
- eclipse调试java程序的九个技巧
- 2015 HNU warm up 07
- Unity,APK资源优化
- 程序员生存定律-打造属于自己的稀缺性
- 数据结构-队列(循环)-2015
- Linux网络编程之简单总结
- 虚析构函数? vptr? 指针偏移?多态数组? delete 基类指针 内存泄漏?崩溃? .
- VirtualBox之vdi、vhd、vmdk虚拟硬盘格式相互转换
- platform虚拟总线
- 程序不死板【上】
- 查找网卡所用的驱动