SoFunction
Updated on 2025-03-04

Unity's AssetPostprocessor Model animation function usage case study

Unity AssetPostprocessor's Model animation related functions modify actual application

In Unity, AssetPostprocessor is a very useful tool that automatically performs some operations when importing resources. Among them, the modification of the animation-related function of Model can help us automatically modify the animation-related functions when importing the model, thereby improving our work efficiency. This article will introduce how to modify the animation related function of the Model using AssetPostprocessor, and provide multiple usage examples.

What is AssetPostprocessor

AssetPostprocessor is a class in Unity that can perform some operations automatically when importing resources. We can implement our needs by inheriting the AssetPostprocessor and rewriting the methods therein. Among them, the modification of animation-related functions of Model is a function in AssetPostprocessor, which can help us automatically modify animation-related functions when importing models.

Modification of the animation-related functions of Model

In Unity, we can control the animation of the model through the Animator component. The Animator component contains some animation-related functions, such as SetBool, SetFloat, SetInteger, etc. These functions can help us control the playback of the animation. When importing the model, we can automatically modify these functions through the animation-related function modification of AssetPostprocessor's Model.

Modify SetBool function

We can control the playback of the animation by modifying the SetBool function. For example, we can automatically change the parameter name "isRunning" in all SetBool functions to "isWalking" when importing the model, thereby controlling the playback of the animation. The specific implementation is as follows:

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor : AssetPostprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetBool" &&  == "isRunning")
                                {
                                     = "isWalking";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Modify SetFloat function

We can also control the playback of the animation by modifying the SetFloat function. For example, we can automatically change the parameter name "speed" in all SetFloat functions to "velocity" when importing the model, thereby controlling the playback of the animation. The specific implementation is as follows:

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor : AssetPostprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetFloat" &&  == "speed")
                                {
                                     = "velocity";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Modify SetInteger function

We can also control the playback of the animation by modifying the SetInteger function. For example, we can automatically change the parameter name "state" in all SetInteger functions to "status" when importing the model, thereby controlling the playback of the animation. The specific implementation is as follows

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor : AssetPostprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetInteger" &&  == "state")
                                {
                                     = "status";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Use Example

Below are several examples of function modifications related to the animation of the Model using AssetPostprocessor.

Example 1: Modify SetBool function

When importing the model, we can change the parameter name "isRunning" in all SetBool functions to "isWalking" to control the playback of the animation. The specific implementation is as follows:

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor : AssetPostprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetBool" &&  == "isRunning")
                                {
                                     = "isWalking";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Example 2: Modify SetFloat function

When importing the model, we can automatically change the parameter name "speed" in all SetFloat functions to "velocity" to control the playback of the animation. The details are as follows:

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor : AssetPostprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetFloat" &&  == "speed")
                                {
                                     = "velocity";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Example 3: Modify SetInteger function

When importing the model, we can automatically change the parameter name "state" in all SetInteger functions to "status" to control the playback of the animation. The specific implementation is as follows:

using UnityEngine;
using UnityEditor;
public class ModelPostprocessor :Postprocessor
{
    void OnPostprocessModel(GameObject go)
    {
        Animator animator = <Animator>();
        if (animator != null)
        {
            AnimatorController controller =  as AnimatorController;
            if (controller != null)
            {
                foreach (AnimatorControllerParameter parameter in )
                {
                    if ( == )
                    {
                        foreach (AnimationClip clip in )
                        {
                            AnimationEvent[] events = (clip);
                            foreach (AnimationEvent e in events)
                            {
                                if ( == "SetInteger" &&  == "state")
                                {
                                     = "status";
                                    (clip, events);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Summarize

This article introduces how to use AssetPostprocessor's Model animation related function modifications, and provides multiple usage examples. By using the AssetPostprocessor's Model animation-related function modification, we can automatically modify the drawing-related functions when importing the model, thereby improving our work efficiency.

For more information about Unity AssetPostprocessor Model animation, please follow my other related articles!