Unity AssetPostprocessor的Model的动画相关的函数修改实际应用

在Unity中,AssetPostprocessor是一个非常有用的工具,它可以在导入资源时自动执行一些操作。其中,Model的动画相关的函数修改可以帮助我们在导入模型时自动修改动画相关的函数,从而提高我们的工作效率。本文将介绍如何使用AssetPostprocessor的Model的动画相关的函数修改,并提供多个使用例子。

什么是AssetPostprocessor

AssetPostprocessor是Unity中的一个类,它可以在导入资源时自动执行一些操作。我们可以通过继承AssetPostprocessor并重写其中的方法来实现自己的需求。其中,Model的动画相关的函数修改是AssetPostprocessor中的一个功能,它可以帮助我们在导入模型时自动修改动画相关的函数。

Model的动画相关的函数修改

在Unity中,我们可以通过Animator组件来控制模型的动画。Animator组件中包含了一些动画相关的函数,例如SetBool、SetFloat、SetInteger等。这些函数可以帮助我们控制动画的播放。在导入模型时,我们可以通过AssetPostprocessor的Model的动画相关的函数修改来自动修改这些函数。

修改SetBool函数

我们可以通过修改SetBool函数来控制动画的播放。例如,我们可以在导入模型时自动将所有的SetBool函数中的参数名“isRunning”改为“isWalking”,从而控制动画的播放。具体实现如下:

using UnityEngine;
using UnityEditor; public class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Bool)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetBool" && e.stringParameter == "isRunning")
{
e.stringParameter = "isWalking";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

修改SetFloat函数

我们也可以通过修改SetFloat函数来控制动画的播放。例如,我们可以在导入模型时自动将所有的SetFloat函数中的参数名“speed”改为“velocity”,从而控制动画的播放。具体实现如下:

using UnityEngine;
using UnityEditor; public class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Float)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetFloat" && e.stringParameter == "speed")
{
e.stringParameter = "velocity";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

修改SetInteger函数

我们还可以通过修改SetInteger函数来控制动画的播放。例如,我们可以在导入模型时自动将所有的SetInteger函数中的参数名“state”改为“status”,从而控制动画的播放。具体实现如下

using UnityEngine;
using UnityEditor; public class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Int)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetInteger" && e.stringParameter == "state")
{
e.stringParameter = "status";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

使用例子

下面是几个使用AssetPostprocessor的Model的动画相关的函数修改的例子。

例子1:修改SetBool函数

我们可以在导入模型时自将所有的SetBool函数中的参数名“isRunning”改为“isWalking”,从而控制动画的播放。具体实现如下:

using UnityEngine;
using UnityEditor; public class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Bool)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetBool" && e.stringParameter == "isRunning")
{
e.stringParameter = "isWalking";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

例子2:修改SetFloat函数

我们可以在导入模型时自动将所有的SetFloat函数中的参数名“speed”改为“velocity”,从而控制动画的播放。具体现如下:

using UnityEngine;
using UnityEditor; public class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Float)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetFloat" && e.stringParameter == "speed")
{
e.stringParameter = "velocity";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

例子3:修改SetInteger函数

我们可以在导入模型时自动将所有的SetInteger函数中的参数名“state”改为“status”,从而控制动画的播放。具体实现如下:

using UnityEngine;
using UnityEditor; public class ModelPostprocessor :Postprocessor
{
void OnPostprocessModel(GameObject go)
{
Animator animator = go.GetComponent<Animator>();
if (animator != null)
{
AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
foreach (AnimatorControllerParameter parameter in controller.parameters)
{
if (parameter.type == AnimatorControllerParameterType.Int)
{
foreach (AnimationClip clip in controller.animationClips)
{
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clip);
foreach (AnimationEvent e in events)
{
if (e.functionName == "SetInteger" && e.stringParameter == "state")
{
e.stringParameter = "status";
AnimationUtility.SetAnimationEvents(clip, events);
}
}
}
}
}
}
}
}
}

总结

本文介绍了如何使用AssetPostprocessor的Model的动画相关的函数修改,并提供了多个使用例子。通过使用AssetPostprocessor的Model的动画相关的函数修改,我们可以在导入模型时自动修改画相关的函数,从而提高我们的工作效率。

Unity的AssetPostprocessor之Model之动画:深入解析与实用案例 3的更多相关文章

  1. Unity 4.0 中的新动画系统——MecAnim

    分享一个文档资料,关于动画系统的,版本应该很老了,但是有借鉴意义的: Unity 4.0 已于 2012 年 11 月 15 日正式发布,Unity 每一次版本的提升,都给游戏开发者带来惊喜,这一次也 ...

  2. 【Unity】10.1 类人动画的导入和设置

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.制作或购买类人模型 为了充分使用 Mecanim 类人动画系统和类人动画的动画重定位功能,需要先使用其他3D建模软件(例如3d ...

  3. Unity3D for VR 学习(9): Unity Shader 光照模型 (illumination model)

    关于光照模型 所谓模型,一般是由学术算法发起, 经过大量实际数据验证而成的可靠公式 现在还记得2009年做TD-SCDMA移动通信算法的时候,曾经看过自由空间传播模型(Free space propa ...

  4. 高速上手Unity中最好的补间动画插件DFTween

     出处:http://blog.csdn.net/u010019717 author:孙广东      时间:2015.3.17   23:00 DFTween 是一个在 Unity 游戏引擎中高 ...

  5. Android属性动画完全解析(下)

    转载:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中我们学习了 ...

  6. Android属性动画完全解析(上),初识属性动画的基本用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...

  7. 精选19款华丽的HTML5动画和实用案例

    下面是本人收集的19款超酷HTML5动画和实用案例,觉得不错,分享给大家. 1.HTML5 Canvas火焰喷射动画效果 还记得以前分享过的一款HTML5烟花动画HTML5 Canvas烟花特效,今天 ...

  8. Android属性动画完全解析(中)

    转载:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是 ...

  9. Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中 ...

  10. Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法 ...

随机推荐

  1. 2021-07-21:一张扑克有3个属性,每种属性有3种值(A、B、C),比如“AAA“,第一个属性值A,第二个属性值A,第三个属性值A,比如“BCA“,第一个属性值B,第二个属性值C,第三个属性值A

    2021-07-21:一张扑克有3个属性,每种属性有3种值(A.B.C),比如"AAA",第一个属性值A,第二个属性值A,第三个属性值A,比如"BCA",第一个 ...

  2. 2023.5.16 总结 AT_abc260_g

    atcoder AT_abc260_g 题意 一个点 O 可以影响到其它点,能影响到的点的坐标满足:(\((u, v)\) 为当前点的坐标,\((x, y) 为能影响到的点的坐标\)) \(u\le ...

  3. 理论+实操,带你了解多沙箱容器运行时Kuasar

    摘要:华为云DTSE技术布道师张天阳结合沙箱容器发展历程,介绍华为云多沙箱容器运行时 Kuasar 项目优势,开启多沙箱容器运行时上手实践体验. 本文分享自华为云社区<理论+实操,带你了解多沙箱 ...

  4. redis.conf 7.0 配置和原理全解,生产王者必备

    5.5 redis.conf 配置详解 我是 Redis, 当程序员用指令 ./redis-server /path/to/redis.conf 把我启动的时候,第一个参数必须是redis.conf ...

  5. ggplot2 调整绘图区域大小

    熟悉 R 绘图的朋友肯定知道,在普通绘图中,图片的大小可以直接在 png() 和 pdf() 中指定,而绘图区大小则可以用 par() 中的 mar 或 mai 来指定.但是在 ggplot2 中,图 ...

  6. 使用RSS打造你的科研资讯头条

    本文章为 "生信草堂" 首发,经生信草堂授权.原作者(Steven Shen)同意转载.由于微信不允许外部链接,你需要点击文章尾部左下角的 "阅读原文",才能访 ...

  7. 基于php的外卖订餐网站(php+mysql)

    介绍 一个基于php的外卖订餐网站,包括前端和后台. 效果演示 http://101.43.124.118:8001/admin 源码地址 https://github.com/geeeeeeeek/ ...

  8. xpoc漏洞使用与编写 浅尝

    下载地址 https://github.com/chaitin/xpoc/releases 目前最新版本是 0.0.4 可能是我还是不太习惯yaml这种结构的,感觉就很反人类,所以我以前一般都还是po ...

  9. Android 巧用ImageView属性实现选中和未选中效果

    原文地址: Android 巧用ImageView属性实现选中和未选中效果 - Stars-One的杂货小窝 选中和未选中状态变更是很常见需求,UI那边出的两个不同状态的图片,然后开发这边去实现 通常 ...

  10. PHP file_put_contents()写入配置文件

    php把提交的数据写入到配置文件中 在后台可以设置网站的基本信息,例如:title,keywords,copyright.等信息,这些信息只是一条数据,存入数据库耗费资源,直接写入到php文件中. 创 ...