Unity3D的Mecanim动画系统是非常强大的,而且作为Unity推荐的动画系统,其未来会完全代替老的一套动画系统,即Legacy动画系统。目前的情况是Mecanim与Legacy两套动画系统同时共存,但是并不是说Legacy动画系统就没有任何价值了,作为Unity4.0以前使用的动画系统,我认为还是很有必要去了解和学习的,所以就有了这篇笔记。

Legacy动画系统

http://docs.unity3d.com/Manual/Animations.html

我们可以使用Unity自带的资源来学习老版本的动画系统,新建Unity3D项目,选择菜单“Assets”->“Import Package”->“Character Controller”,导入的资源里的那个小人就是使用Legacy动画系统的模型,我们的学习可以基于他来进行。

模型文件

在骨骼这一项中,我们发现动画类型的设置就是Legacy,说明这个模型使用的动画类型为老版本的动画系统。

我们再看看动画页:

动画页中,我们可以对动画剪辑进行编辑。

控制动画

我们直接将FBX文件拖入场景,Unity会自动帮我们添加Transform和Animation两个组件(注意Mecanim动画系统使用的是Animator组件,Legacy动画系统使用的是Animation组件)。

Animation组件的设置还是比较简单的:

  • Animation:当前播放的动画。
  • Animations:所有可以播放的动画。
  • Play Automatically:是否自动播放。
  • Animate Physics:动画是否和物理世界进行交互。
  • Culling Type:动画在不可见时是否还继续播放,优化选项默认即可。

点击播放按钮就可以看见动画正常播放了。

脚本控制

http://docs.unity3d.com/ScriptReference/Animation.html

下面我们来看看如何使用脚本控制动画的播放,我们将下面的脚本绑定到人物身上即可。

 using UnityEngine;
using System.Collections; public class AnimationScript : MonoBehaviour
{
private Animation _animation; void Start()
{
_animation = this.animation;
} void OnGUI()
{
//直接播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.Play("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.Play("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.Play("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.Play("jump_pose");
}
//使用融合来播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.CrossFade("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.CrossFade("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.CrossFade("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.CrossFade("jump_pose");
}
}
}

运行程序,会看见两排按钮,其中第一排按钮使用Play方法来切换动画,而第二排按钮则使用CrossFade来播放动画。

Play与CrossFade的区别

以从跑步切换到站立动画为例来看:

Play:直接切换动画,如果人物之前处于倾斜跑步状态,则会立即变成站立状态,表现上比较不真实,特别是当两个动画姿势差别较大时。

CrossFade:通过动画融合来切换动画,第二个参数可以指定融合的时间,如果人物之前处于倾斜跑步状态,则会在指定的融合时间内逐渐变成站立状态,表现上接近真实的人物动作切换效果。

但是当使用CrossFade播放跳跃动画时会出现问题,主要问题是跳跃动画不是循环播放且其持续时间小于动画融合的时间,我们修改为下面的脚本指定融合时间短一点就可以正常进行跳跃的融合播放了:

_animation.CrossFade("jump_pose", 0.1f);

PlayQueued

该方法可以指定当当前的动画播放完毕后接下来播放的动画,如下:

_animation.PlayQueued("run", QueueMode.CompleteOthers, PlayMode.StopSameLayer);

文件格式和资源加载

我们的模型使用通用的FBX格式,那么动画文件的储存一般有两种情况,一是所有的动画和模型都一起存放到一个文件中,还有一种情况是模型单独一个文件而动画单独一个文件。

模型动画都存放在一个文件中的情况

类似于Unity提供的Character Controller中的资源就是这样的结构,一个FBX文件保存了模型、骨骼和动画:

Resources加载

我们直接加载该资源就可以直接使用,将下面的脚本绑定到摄像机即可,脚本如下:

 using UnityEngine;
using System.Collections; public class AllInOneResourcesLoad : MonoBehaviour
{
private Animation _animation; void Start()
{
GameObject go = Resources.Load<GameObject>("Standard Assets/Character Controllers/Sources/PrototypeCharacter/Constructor"); GameObject man = Instantiate(go) as GameObject;
_animation = man.animation;
} void OnGUI()
{
//直接播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.Play("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.Play("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.Play("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.Play("jump_pose");
}
//使用融合来播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.CrossFade("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.CrossFade("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.CrossFade("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.CrossFade("jump_pose", 0.1f);
}
}
}

AssetBundle加载

打包

使用下面的脚本打包:

 using UnityEditor;
using UnityEngine; public class CreateAllInOneAB
{
[MenuItem("Tool/CreateAllInOneAB")]
private static void Create()
{
BuildPipeline.BuildAssetBundle(null, new[]
{
AssetDatabase.LoadAssetAtPath("Assets/Resources/Standard Assets/Character Controllers/Sources/PrototypeCharacter/Constructor.FBX", typeof(GameObject))
},
Application.streamingAssetsPath + "/AllInOne.assetbundle",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
BuildTarget.StandaloneWindows64);
}
}

加载

将下面的脚本绑定到摄像机即可:

 using UnityEngine;
using System.Collections; public class AllInOneAssetBundleLoad : MonoBehaviour
{
private Animation _animation; void Start()
{
AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AllInOne.assetbundle"); GameObject go = assetBundle.Load("Constructor", typeof(GameObject)) as GameObject; GameObject man = Instantiate(go) as GameObject;
_animation = man.animation;
} void OnGUI()
{
//直接播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.Play("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.Play("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.Play("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.Play("jump_pose");
}
//使用融合来播放动画
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.CrossFade("idle");
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animation.CrossFade("walk");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.CrossFade("run");
}
if(GUI.Button(new Rect(, , , ), "jump_pose"))
{
_animation.CrossFade("jump_pose", 0.1f);
}
}
}

模型动画分开存放的情况

还有一种情况是模型和动画是分为多个FBX文件存放的,比如下面是模型文件:

虽然有一个Take 001的动画,但是实际上我们并不使用该动画,而是使用下面仅保存了动画的FBX文件:

Resources加载

首先我们要清楚的是,无论是保存了模型还是保存了动画的FBX文件在Unity看来都是同样的一种类型,即添加了Animation组件的GameObject,下面我们看看如何在Resources中加载并显示这个角色,代码如下,挂载到主摄像机即可:

 using UnityEngine;
using System.Collections; public class ResourcesLoad : MonoBehaviour
{
private Animation _animation; void Start()
{
GameObject go = Resources.Load<GameObject>("ZombieNurse/Zombienurse_Rig"); GameObject man = Instantiate(go) as GameObject;
_animation = man.animation; //添加动画剪辑
_animation.AddClip(LoadAnimationClip("ZombieNurse/Animation/Zombienurse@attack"), "attack");
_animation.AddClip(LoadAnimationClip("ZombieNurse/Animation/Zombienurse@death"), "death");
_animation.AddClip(LoadAnimationClip("ZombieNurse/Animation/Zombienurse@idle"), "idle");
_animation.AddClip(LoadAnimationClip("ZombieNurse/Animation/Zombienurse@run"), "run"); _animation.Play("idle");
} private AnimationClip LoadAnimationClip(string path)
{
GameObject go = Resources.Load<GameObject>(path);
return go.animation.clip;
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.CrossFade("idle");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.CrossFade("run");
}
if(GUI.Button(new Rect(, , , ), "attack"))
{
_animation.CrossFade("attack");
}
if(GUI.Button(new Rect(, , , ), "death"))
{
_animation.CrossFade("death");
}
}
}

AssetBundle加载

打包

使用下面的脚本打包:

 using UnityEngine;
using UnityEditor; public class CreateAB : MonoBehaviour
{
[MenuItem("Tool/CreateAB")]
private static void Create()
{
BuildPipeline.BuildAssetBundle(null, new[]
{
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/Zombienurse_Rig.FBX", typeof(GameObject)),
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/Animation/Zombienurse@attack.FBX", typeof(GameObject)),
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/Animation/Zombienurse@death.FBX", typeof(GameObject)),
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/Animation/Zombienurse@idle.FBX", typeof(GameObject)),
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/Animation/Zombienurse@run.FBX", typeof(GameObject))
},
Application.streamingAssetsPath + "/AB.assetbundle",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
BuildTarget.StandaloneWindows64);
}
}

加载

将下面的脚本绑定到摄像机即可:

 using UnityEngine;
using System.Collections; public class AssetBundleLoad : MonoBehaviour
{
private Animation _animation; void Start()
{
AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AB.assetbundle"); GameObject go = assetBundle.Load("Zombienurse_Rig", typeof(GameObject)) as GameObject; GameObject man = Instantiate(go) as GameObject;
_animation = man.animation; //添加动画剪辑
_animation.AddClip(LoadAnimationClip(assetBundle, "Zombienurse@attack"), "attack");
_animation.AddClip(LoadAnimationClip(assetBundle, "Zombienurse@death"), "death");
_animation.AddClip(LoadAnimationClip(assetBundle, "Zombienurse@idle"), "idle");
_animation.AddClip(LoadAnimationClip(assetBundle, "Zombienurse@run"), "run"); _animation.Play("idle");
} private AnimationClip LoadAnimationClip(AssetBundle assetBundle, string path)
{
GameObject go = assetBundle.Load(path, typeof(GameObject)) as GameObject;
return go.animation.clip;
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animation.CrossFade("idle");
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animation.CrossFade("run");
}
if(GUI.Button(new Rect(, , , ), "attack"))
{
_animation.CrossFade("attack");
}
if(GUI.Button(new Rect(, , , ), "death"))
{
_animation.CrossFade("death");
}
}
}

Unity3D之Legacy动画系统学习笔记的更多相关文章

  1. Unity3D之Mecanim动画系统学习笔记(二):模型导入

    我们要在Unity3D中使用上模型和动画,需要经过下面几个阶段的制作,下面以一个人形的模型开发为准来介绍. 模型制作 模型建模(Modelling) 我们的美术在建模时一般会制作一个称为T-Pose( ...

  2. Unity3D之Mecanim动画系统学习笔记(一):认识Mecanim动画系统

    Mecanim简介 Mecanim动画系统是Unity3D4.0开始引入的一套全新的动画系统,主要提供了下面4个方面的功能: 针对人形角色提供一套特殊的工作流. 动画重定向的能力,可以非常方便的把动画 ...

  3. Unity3D之Mecanim动画系统学习笔记(九):Blend Tree(混合树)

    认识Blend Tree 我们在Animator Controller中除了可以创建一个State外还可以创建一个Blend Tree,如下: 那么我们看下新创建的Blend Tree和State有什 ...

  4. Unity3D之Mecanim动画系统学习笔记(七):IK(反向动力学)动画

    什么是IK? IK(Inverse Kinematics)即反向动力学,即可以使用场景中的各种物体来控制和影响角色身体部位的运动,一般来说骨骼动画都是传统的从父节点到子节点的带动方式(即正向动力学), ...

  5. Unity3D之Mecanim动画系统学习笔记(十一):高级功能应用

    动作游戏 还记得读书的时候熬夜打<波斯王子>的时光,我们的王子通过跳跃穿过墙壁的小洞.在高层建筑上进行攀爬和跳跃,还有在操作失误掉下高楼和触发必死机关后使用时之沙的时光倒流功能回归死亡之前 ...

  6. Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关

    资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载. 注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBu ...

  7. Unity3D之Mecanim动画系统学习笔记(五):Animator Controller

    简介 Animator Controller在Unity中是作为一种单独的配置文件存在的文件类型,其后缀为controller,Animator Controller包含了以下几种功能: 可以对多个动 ...

  8. Unity3D之Mecanim动画系统学习笔记(四):Animation State

    动画的设置 我们先看看Animation Clip的一些设置: Loop time:动画是否循环播放. 下面出现了3个大致一样的选项: Root Transform Rotation:表示为播放动画的 ...

  9. Unity3D之Mecanim动画系统学习笔记(三):Animation View

    动画组件之间的关系 我们先看一张图: 这里我们可以看到,我们在GameObject之上绑定的Animator组件是控制模型进行动画播放的. 而其属性Controller则对应一个Animator Co ...

随机推荐

  1. UVa 10375 (唯一分解定理) Choose and divide

    题意: 求组合数C(p, q) / C(r, s)结果保留5为小数. 分析: 先用筛法求出10000以内的质数,然后计算每个素数对应的指数,最后再根据指数计算答案. #include <cstd ...

  2. 将多个.a库合并为一个.a库的方法

    如果编译了多个架构的静态库,想将它们合并为一个静态库的时候,可以用如下方法合并: sudo lipo -create /libs/ffmpeg/2.6.3/arm64/lib/libavcodec.a ...

  3. CentOS SVN服务器安装配置小记

    SVN的安装 安装很简单,尤其对于CentOS这种,直接: # yum install subversion# yum install mod_dav_svn 不同发行版的Package安装方法参见h ...

  4. 开源Jabber(XMPP) IM服务器介绍

    一.摘要 这是我粗略读了一遍Jabber协议和相关技术文章后的产物,有些地方不一定准确.在文章中引用的一些代码来自www.jabber.org上的文章. 二. 什么是Jabber    Jabber就 ...

  5. centos软件环境

    1,保持能链接外网和yum的可用性. 注意:yum配置项中最好:keepcache=1 2,yum install gcc, gcc-c++, make, cmake, 3, ntfs-3g wget ...

  6. HDU 4267 A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. 仿酷狗音乐播放器开发日志二十五 duilib右键事件的不足的bug修复

    转载请说明原出处,谢谢~~ 虽然仿酷狗的各个菜单早就写好了,但是一直没有附加到程序里.今天把菜单和播放列表控件关联时发现了问题. 和播放列表相关的菜单有三个,分别是每个音乐项目控件相关的菜单.分组的菜 ...

  8. C++小游戏:扑克牌21点

    21点扑克牌游戏: 程序说明:该程序是模拟21点扑克牌游戏,玩家最多可以要5张牌,但是如果牌的点数之和超过21点,则自动出局,在不超过21点的情况下,玩家与庄家比牌的大小,大者为赢家 程序片段分析: ...

  9. ndk文件操作问题及小结

    最近在做文件传输,发现在android下用f系列的C库函数去读取文件文件大小会受到2G大小的约束,查阅了很久,最后只能去看google的libc源码,发现了以下几个问题: 1.bionic的libc是 ...

  10. Azure支持docker简介以及使用指南

    Docker 是一个开源的项目,主要的特点是能将应用程序包装在一个 LXC (Linux Container) 容器中,当这些应用被包装进容器后,部署.迁移都变得更为简单.与传统的虚拟化技术相比,虚拟 ...