作者:海螺里的秘密_471 | 来源:互联网 | 2023-05-18 19:15
Unity,APK大小优化一个手机游戏到了开发的后期,一个最重要的步骤就是优化APK包的大小。主要优化贴图,模型,和声音。本文主要讲述如何批量优化这三种资源。对于贴图资源可以修改贴
Unity,APK大小优化
一个手机游戏到了开发的后期,一个最重要的步骤就是优化APK包的大小。主要优化贴图,模型,和声音。本文主要讲述如何批量优化这三种资源。
,".jpg"};
static string[] AudioFilter={".mp3",".ogg",".wav",".wma"};
static string[] FbxFilter={".fbx",".FBX"};
static bool Contain(string ex,string[] collection)
{
for(int i=0;iif(collection[i]==ex)
return true;
}
return false;
}
public static void GetAssetFile(string dir)
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
string exten=fi.Extension;
if(Contain(exten,TextureFilter))
{
TextureAssetList.Add(fi.FullName);
}else if(Contain(exten,AudioFilter))
{
AudioAssetsList.Add(fi.FullName);
}else if(Contain (exten,FbxFilter))
{
FbxAssetsList.Add(fi.FullName);
}
}
else
{
DirectoryInfo d1 = new DirectoryInfo(d);
if (d1.GetFiles().Length != 0)
{
GetAssetFile(d1.FullName);
}
}
}
}
static void LogList()
{
for(int i=0;ifor(int i=0;ifor(int i=0;i#region Compress Assets /
static void DealTexture()
{
for(int i=0;istring 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.SetPlatformTextureSettings("Android",tImp.maxTextureSize/2,TextureImporterFormat.ARGB16);
AssetDatabase.ImportAsset(strPath);
}
static void DealAudio()
{
for(int i=0;istring 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;istring 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
}
}