您当前的位置: 首页 >  Jave.Lin unity

Unity - Mesh、Animation Compress of the Model - 模型的网格、动画压缩,减少包体大小

Jave.Lin 发布时间:2021-01-18 21:47:26 ,浏览量:4

文章目录
  • 将 .fbx 放到 Assets
  • 网格信息
    • face
    • body
    • hair
    • weapon
    • skin
  • 压缩选
    • Animation->Anim.Compression : Optimal
    • 打包 AB 大小
    • Model->Mesh.Compression : Optimal
    • 打包 AB 大小
    • AnimationClip 精度压缩
    • AnimationClip 丢弃不必要的变化曲线
    • CSharp 脚本 - ModelPreprocessor.cs
    • 打包 AB 大小
  • 三次优化
  • 测试打包 AB 资源的 CSharp 脚本 : BuildFbxToABEditor.cs
  • 另,附送一个坑点
  • References

将 .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  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             
关注
打赏
1688896170
查看更多评论
0.0541s