文章目录
将 .fbx 放到 Assets
网格信息
face
375 顶点,638 面
body
6529 顶点,9381 面
hair
2234 顶点,3162 面
weapon
176 顶点,132 面
skin
1053 顶顶啊,1518 面
压缩选
- fbx 的 Animation->Anim.Compression : Off
- fbx 的 Model->Mesh Compression : Off
- fbx AnimationClip 的 keyframe 精度
Animation->Anim.Compression : Optimal
将 Anim.Compression 由 Off 调整为:Optimal
Rotation/Position/Scale Error 的各个容差都使用默认值:0.5
打包 AB 大小
单单一个:Anim.Compression : Off 到 Optimal, Errors : 0.5
然后压缩大小从:6.54 mb -> 1.64 mb
Model->Mesh.Compression : Optimal
将 Mesh.Compression 由 Off 调整为:High
打包 AB 大小
单单一个:Mesh Compression : Off 到 High
然后压缩大小从:1.64 mb -> 1.42 mb
AnimationClip 精度压缩
其实只保留了 2~3 位精度,原来的精度有:7位小数,我们将其压缩到 f2 : 2两个精度
可以看到调整变换:0.7071068 -> 071
AnimationClip 丢弃不必要的变化曲线
一般我们的动画中都是 位移、旋转比较多,缩放比较少,所以一般情况下,确定没使用到缩放的,可以直接删掉
public static void CompressAnimationClip(AnimationClip clip)
{
var editorCurves = AnimationUtility.GetCurveBindings(clip);
if (editorCurves != null)
{
foreach (EditorCurveBinding editorCurve in editorCurves)
{
string name = editorCurve.propertyName.ToLower();
if (name.Contains("scale"))
{
AnimationUtility.SetEditorCurve(clip, editorCurve, null);
}
}
}
...
}
CSharp 脚本 - ModelPreprocessor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class ModelPreprocessor : AssetPostprocessor
{
public void OnPreprocessModel()
{
//Debug.Log("ModelPreprocessor.OnPreprocessModel");
ModelImporter modelImporter = (ModelImporter)assetImporter;
modelImporter.animationCompression = ModelImporterAnimationCompression.Optimal;
modelImporter.meshCompression = ModelImporterMeshCompression.High;
}
public void OnPostprocessAnimation(GameObject go, AnimationClip clip)
{
try
{
CompressAnimationClip(clip);
}
catch (System.Exception e)
{
Debug.LogError("CompressAnimationClip Failed !!! animationPath :" + assetPath + "error: " + e);
}
}
public static void CompressAnimationClip(AnimationClip clip)
{
var editorCurves = AnimationUtility.GetCurveBindings(clip);
if (editorCurves != null)
{
foreach (EditorCurveBinding editorCurve in editorCurves)
{
string name = editorCurve.propertyName.ToLower();
if (name.Contains("scale"))
{
AnimationUtility.SetEditorCurve(clip, editorCurve, null);
}
}
}
string format = "f4";
AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(clip);
for (int ii = 0; ii < curves.Length; ++ii)
{
AnimationClipCurveData curveDate = curves[ii];
if (curveDate.curve == null || curveDate.curve.keys == null)
{
continue;
}
Keyframe[] keyFrames = curveDate.curve.keys;
for (int i = 0; i < keyFrames.Length; i++)
{
Keyframe key = keyFrames[i];
key.value = float.Parse(key.value.ToString(format));
key.inTangent = float.Parse(key.inTangent.ToString(format));
key.outTangent = float.Parse(key.outTangent.ToString(format));
keyFrames[i] = key;
}
curveDate.curve.keys = keyFrames;
clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
}
}
}
打包 AB 大小
从 f7 精度调整到 f2 精度
然后压缩大小从:1.42 mb -> 1.10 mb
三次优化
6.54 mb -> 1.64 mb-> 1.42 mb -> 1.10 mb
测试打包 AB 资源的 CSharp 脚本 : BuildFbxToABEditor.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public static class BuildFbxToABEditor
{
[MenuItem("Tools/BuildSelectionToAB")]
private static void BuildSelectionToAB()
{
Debug.Log("BuildSelectionToAB START.");
var objs = Selection.objects;
if (objs == null || objs.Length == 0)
{
Debug.Log($"Selection.objects.Length : {objs.Length}, u have no selections.");
return;
}
var ab_build_infos = new AssetBundleBuild[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
var asset_path = AssetDatabase.GetAssetPath(objs[i]);
Debug.Log($"asset path : {asset_path}");
var ext = Path.GetExtension(asset_path);
ab_build_infos[i] = new AssetBundleBuild
{
assetBundleName = $"{objs[i].name}{Path.GetExtension(asset_path)}",
assetNames = new string[1] { AssetDatabase.GetAssetPath(objs[i]) },
};
}
Debug.Log($"Selection.objects.Length : {objs.Length}");
var target_platform = BuildTarget.StandaloneWindows; // 这里可以试试 StandaloneWindows64
var ab_output_path = $"{Application.dataPath}/abs/{target_platform}/".Replace("Assets", "Bundles");
BuildAssetBundleOptions build_opts = BuildAssetBundleOptions.None;
build_opts |= BuildAssetBundleOptions.DisableWriteTypeTree;
build_opts |= BuildAssetBundleOptions.ChunkBasedCompression;
Debug.Log($"Build Bundle Export Path : {ab_output_path}");
if (Directory.Exists(ab_output_path))
{
// delete files
var files = Directory.GetFiles(ab_output_path, "*", SearchOption.AllDirectories);
foreach (var f in files)
{
try
{
File.Delete(f);
}
catch
{
}
}
// delete directories
var dirs = Directory.GetDirectories(ab_output_path, "*", SearchOption.TopDirectoryOnly);
foreach (var d in dirs)
{
try
{
Directory.Delete(d);
}
catch
{
}
}
}
if (!Directory.Exists(ab_output_path))
{
Directory.CreateDirectory(ab_output_path);
}
BuildPipeline.BuildAssetBundles(
ab_output_path,
ab_build_infos,
build_opts,
target_platform
);
Debug.Log("BuildSelectionToAB END.");
}
}
另,附送一个坑点
注意上面截图中的红色框中的内容,这是 FBX 模型的 Import Setting 设置。
在 Materials->Material Creation Mode 默认是 Standard
也就是使用了 Built-in Shader 中的 Standard 材质中的 Shader
如果您的项目中没有使用到 Standard 功能的话,建议写一个不要使用 Standard
可以选择如上图中的:None
也可以写个工具脚本,继承自 AssetPostprocessor
实现 OnPostprocessModel
函数类似下面的处理即可:
public class AllAssetPostprocessor : AssetPostprocessor
{
...
public void OnPostprocessModel(GameObject go)
{
ModelImporter import = (ModelImporter)assetImporter;
//Log.logError("OnPostprocessModel", import.assetPath);
if (import != null)
{
...
import.materialImportMode = ModelImporterMaterialImportMode.None;
...
}
}
...
}
除去了 Standard 有什么好处,如果你的项目用不着,但是因为 FBX 的 Material Creation Mode 用到了,那发布程序时,你会发现打出来的程序安装包会带有 standard shader,这会导致两个问题:
- 打包编译 shader 变体的时长增加了(特别是如果你的 shader 变体越多,时间越久)
- 运行时,你会发现内存占用很高,因为 shader 变体都是实时编译为内存 byte code 流,传入到 GPU 的,如果 shader 的变体越多,那么 byte code 的数据越多,都在内存中占用着。
另外,可以根据项目情况可以调整一下 其他的属性,如下:
private static void OnHandleModelImporterProperties(ModelImporter importer,
bool? readable = null,
ModelImporterMeshCompression? meshCompression = null,
ModelImporterAnimationCompression? animaCompression = null
)
{
var changed = false;
// jave.lin : model 属性
if (meshCompression.HasValue && importer.meshCompression != meshCompression.Value)
{
importer.meshCompression = meshCompression.Value;
changed = true;
}
if (readable.HasValue && importer.isReadable != readable.Value)
{
importer.isReadable = readable.Value;
changed = true;
}
if (importer.optimizeMesh != true)
{
importer.optimizeMesh = true;
changed = true;
}
if (importer.importBlendShapes != false)
{
importer.importBlendShapes = false;
changed = true;
}
if (importer.importVisibility != false)
{
importer.importVisibility = false;
changed = true;
}
if (importer.importCameras != false)
{
importer.importCameras = false;
changed = true;
}
if (importer.importLights != false)
{
importer.importLights = false;
changed = true;
}
if (animaCompression.HasValue && importer.animationCompression != animaCompression.Value)
{
importer.animationCompression = animaCompression.Value;
changed = true;
}
if (changed)
{
importer.SaveAndReimport();
}
}