简介

原来以为4.0会有新的UI系统,没想到Unity选择了在模型动画上增加新内容,

就是据说很强大的Mecanim动画系统了,官方的介绍链接:

http://china.unity3d.com/unity/mecanim/

http://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html

 官方教学视频:

http://v.youku.com/v_show/id_XNDc5OTc4NTEy.html

一、基本概念

原来看那个宣传视频时真心觉得不错,这两天花了点时间大概了解了下:

现了解一些名词术语之类的东西吧:

http://docs.unity3d.com/Documentation/Manual/AnimationGlossary.html

A Glossary of Animation and Mecanim terms

图标名字描述类型用途/备注
Animation Clip related terms
Animation Clip

Animation data that can be used for animated characters or simple animations. It is a simple "unit" piece of motion, such as (one specific instance of) "Idle", "Walk" or "Run"

(用来保存使模型运动的动画信息,是motion的一个基本单元,例如空闲状态,走路状态,跑步状态等基本动作)

sub-Asset  
Body Mask

A specification for which body parts to include or exclude for a skeleton

(使身体的某些部分不受骨骼的影响)比如我一个动作只想影响到上半身,那我就屏蔽掉下半身的运动。

Asset (.mask)

Used in Animation Layers and in the importer

(在动画分层中或模型导入中使用)

  Animation
Curves

Curves can be attached to animation clips and controlled by various parameters from the game

(对动画绑定动画曲线,在游戏时可以通过参数来改变动画)

   
Avatar related terms
Avatar

An interface for retargeting one skeleton to another

(骨骼重定向的一个接口)

sub-Asset  
  Retargeting

Applying animations created for one model to another

(将一个模型的动画施加到另一个模型上)

Process  
  Rigging

The prcoess of building a skeleton hierarchy of bone joints for your mesh

(组模型的骨骼层次结构的过程)

Process

done in an external tool, such as Max or Maya

(在Max或Maya中已经做好了)

  Skinning

The process of binding bone joints to the character's mesh or 'skin'

(为骨骼蒙皮的过程)

Process

done in an external tool, such as Max or Maya

(在Max或Maya中已经做好了)

  Muscle Definition

A Mecanim concept, which allows you to have a more intuitive control over the character's skeleton. When an Avatar is in place, Mecanim works in muscle space, which is more intuitive than bone space

(Mecanim系统中的一个概念,让人可以更直观的控制骨骼系统,当使用Avatar时,Mecanim系统是在muscle空间下工作,比骨骼空间更直观)

就我理解,这个应该是就是一个或多个骨骼组合成一个 Muscle,可以更方便控制。

   
  T-pose

The pose in which the character has his arms straight out to the sides, forming a "T". The required pose for the character to be in, in order to make an Avatar

(两手向外平伸张开形成一个T字,为了制作一个Avatar,角色需要满足这种造型)

   
  Bind-pose

The pose at which the character was modelled

(角色模型的造型)

   
Human template

A pre-defined bone-mapping

(定义好的骨骼映射模板)

Asset (.ht)

Used for matching bones from FBX files to the Avatar.

(用于使导入的FBX文件的骨骼和Avatar匹配)

Animator and Animator Controller related terms
  Animator Component

Component on a model that animates that model using the Mecanim animation system. The component has a reference to an Animator Controller asset that controls the animation.

(Animator组件,指向Asset里定义的Animator Controller)

Component  
  Root Motion

Motion of character's root, whether it's controlled by the animation itself or externally.

(角色的根Motion,被自己的动画控件或者被外部的动画控制)

   
Animator Controller (Asset)

The Animator Controller controls animation through Animation Layers with Animation State Machines and Animation Blend Trees, controlled by Animation Parameters. The same Animator Controller can be referenced by multiple models with Animator components.

(通过带有动画状态机的动画层和动画混合树来控制动画,同时又可以通过动画参数来控制它。多个含有Animator组件的模型可以引用同一个Animator Controller)

Asset (.controller)  
  Animator Controller (Window)

The window where the Animator Controller Asset is visualized and edited.

(Animator Controller的可视化编辑窗口)

Window  
  Animation Layer

An Animation Layer contains an Animation State Machine that controls animations of a model or part of it. An example of this is if you have a full-body layer for walking / jumping and a higher layer for upper-body motions such as throwing object / shooting. The higher layers take precedence for the body parts they control.

(一个动画层包括了一个动画状态机,这个动画状态机控制整个或部分模型的一系列动画。例如,你有一个全身的动画层,用于跑步或跳跃动作,和一个更高层次的上半身动作,如扔东西和射击动作。高层次的就享有所控制身体的那部分的使用权。)

   
  Animation State Machine

A graph controlling the interaction of Animation States. Each state references an Animation Blend Tree or a single Animation Clip.

(动画状态的控制图,每个动画状态都是一个动画混合树或者是一个基本动画)

   
  Animation Blend Tree

Used for continuous blending between similar Animation Clips based on float Animation Parameters.

(通过对float型的动画参数的修改来做到连续的混合类似的动作)比如左转和右转。

   
  Animation Parameters

Used to communicate between scripting and the Animator Controller. Some parameters can be set in scripting and used by the controller, while other parameters are based on Custom Curves in Animation Clips and can be sampled using the scripting API.

(用于脚本和Animator Controller之间的通信。一些参数可以在脚本中设置并且被controller使用,同时其它一些参数是基于动画里的Custom Curves,可以通过脚本API来采样)

   
  Inverse Kinematics (IK)

The ability to control the character's body parts based on various objects in the world.

(可以通过世界里的物体来控制角色的身体)IK的概念已经挺老的,可以百度“反向动力学”,最通俗的理解就是,你看到前面有个苹果,你的手臂就能知道应该怎么运动能到苹果那个位置,拿到苹果。

   
Non-Mecanim animation terms
  Animation Component

The component needed for non-Mecanim animations

(不使用Mecanim动画系统的动画组件)就是4.0以前用的那一套动画系统

Component  

 

二、使用

基本的操作,网上已经有人写过一点教程,我就不再花时间去写了。

http://www.narkii.com/club/bbs_271433.shtml

http://www.narkii.com/club/bbs_271638.shtml

看了上面两篇,再加上官方文档,应该就能入门了,至少知道模型应该如何导入,如何制作Avatar出来。

下面我要说一下状态机的使用方式,其实最好是能把官方的DEMO载下来看,收获会很多。

 

有了动画之后,我们就想着要怎么把他们串起来,比如我如何从走变成跑,又如何变成跳。

Mecanim为我们提供了状态机机制,并提供了可视化编辑窗口。

注意图中的三个红框,

①是设置状态转换的参数,通过脚本来改变设置的值与②中设置的条件对比来判断是否转换状态。

②是状态转换的箭头,在里边设置转换的参考值。

③是一个动作状态,里边设置Motion。

 这里可以设置Animation Clip或者是Blend Tree。

选中②的界面:

 

选中上面的红框,进入下面红框来设置状态转换的条件,如:这里设置的是当Speed参数的值大于0.1的时候进入奔跑模式。

同理可以在回来的箭头上设置当Speed参数小于0.1里进入idle模式。

 

在代码里控制Speed值:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
    protected Animator m_Animator;
    GameObject m_PlayerObj;
    
    // Use this for initialization
    void Start () {
        m_Animator = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () 
    {
        if (m_Animator)
        {
            // attack
//            if (Input.GetButtonDown("Fire1"))
//            {
//                m_Animator.SetBool("Attack1", true);
//            }
//            else if (Input.GetButtonUp("Fire1"))
//            {
//                m_Animator.SetBool("Attack1", false);
//            }
            
            
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            
            m_Animator.SetFloat("Speed", h*h+v*v);
            m_Animator.SetFloat("Direction", h, 0.25f, Time.deltaTime);
        }
    }
}

 

 

最后结果:

其实这里边的跑动,是把官方Demo里的动画重定向到我自己的模型上了,让一个女士跑成这样。。真是丑。。

 

加一个国外的教程:

http://www.gamasutra.com/blogs/HeikkiTormala/20121214/183567/