unity动画入门
Have you been looking for an easy way to create skeletal animation for your 2D sprites? We’re introducing our own 2D Animation package, which allows you to rig 2D sprites, paint bone weights, and create skeletal animation, all in-editor! On top of that, there is support for Inverse Kinematics as well. You can check the feature out right now – it’s been available as a package in preview since 2018.1. Read on to learn how to use it effectively in your projects right now.
您是否一直在寻找一种简便的方法来为2D精灵创建骨骼动画? 我们将介绍我们自己的2D动画包,它允许您在所有编辑器中装配2D精灵,绘制骨骼权重并创建骨骼动画! 最重要的是,还支持逆运动学。 您可以立即检出此功能-自2018.1起已作为预览包提供。 请继续阅读以了解如何立即在您的项目中有效使用它。
2D Animation is currently shipped as a preview package, which means you can use Unity’s Package Manager to install it for your project. The Package Manager is accessible from Unity 2018.1 or higher. Make sure to also grab the 2D IK package if you are planning on using Inverse Kinematics too – they will be covered in a separate blog post, coming out later this month.
2D动画目前作为预览包提供,这意味着您可以使用Unity的包管理器为您的项目安装它。 可从Unity 2018.1或更高版本访问软件包管理器。 如果您也打算使用Inverse Kinematics,也请确保也要获取2D IK软件包-它们将在本月晚些时候发布的单独博客文章中介绍。
In order to start animating, we’re going to need a couple of things: the first is, of course, the sprite that we wish to animate. It can be anything you want – a character, a monster, an object… you name it!
为了开始制作动画,我们需要做两件事:首先,当然是我们希望制作动画的精灵。 它可以是任何您想要的东西-角色,怪物,物体……随便命名!
There are a few things to keep in mind when importing your sprites for 2D Animation. First of all, there is currently no layer support. This means that if you wish your rig to use multiple disjointed sprite meshes, e.g. for arms and legs, you will need to space them out directly on one layer. Full layer support is planned to be included in a later release of the package – where it will support layer import from PSD and other file formats. In addition, there is currently no integration with Unity’s new Vector Graphics package, which includes SVG file import. Support for this is also in the works.
导入2D动画的精灵时,需要牢记一些注意事项。 首先,当前没有图层支持。 这意味着,如果您希望装备使用多个不连续的Sprite网格物体(例如,用于手臂和腿部),则需要将它们直接在一层上隔开。 计划在该软件包的更高版本中包括对全层的支持–在此版本中,它将支持从PSD和其他文件格式的层导入。 此外,当前没有与Unity的新Vector Graphics软件包集成,该软件包包括SVG文件导入。 也正在为此提供支持。
With all of that in mind, let’s import your sprite. The example we use in this article is Ríkr the Viking. We’ll make this character available for you to experiment soon. It has been divided into multiple sprites and re-arranged on one layer:
考虑到所有这些,让我们导入您的精灵。 我们在本文中使用的示例是维京海盗。 我们将使您可以尝试使用此角色。 它已被分成多个精灵,并在一层上重新排列:
All of the 2D rigging happens in the Sprite Editor. To access it, select your character sprite asset, and then in the Inspector window you will find the Sprite Editor button.
所有2D装配都在Sprite Editor中进行。 要访问它,请选择您的角色精灵资源,然后在“检查器”窗口中找到“精灵编辑器”按钮。
In the top left-hand corner of the Sprite Editor, you will see a dropdown with the Sprite Editor selected. Change that to the Bone Editor, and from here, we can start creating the first bones of our rig! In the bottom right-hand corner, you will see an array of bone-editing tools.
在Sprite Editor的左上角,您会看到一个下拉菜单,其中选择了Sprite Editor。 将其更改为Bone Editor,然后从这里开始创建装备的第一个骨骼! 在右下角,您将看到一系列骨骼编辑工具。
To add your first bone, choose the Create Bone tool (shortcut ‘B’). If you now left-click anywhere in the Bone Editor, you will set the starting point of your Root bone. Left-clicking again will define the endpoint of the bone and the start of the next one. Right clicking will cancel the creation of the bone:
要添加第一根骨骼,请选择“创建骨骼”工具(快捷方式“ B”)。 如果现在在“骨骼编辑器”中的任意位置单击鼠标左键,则将设置“根”骨骼的起点。 再次单击鼠标左键将定义骨骼的端点和下一个骨骼的起点。 右键单击将取消骨骼的创建:
When you are creating the root bone of your skeleton, you may want to consider what type of root bone placement will be the most optimal for your use case. Generally, there are two approaches to this – the first is to put the root bone at an arbitrary point outside of the mesh, usually between the character’s feet – using it as a so-called origin point; the second is to place it at the hips of the character and make it also act as the hip bone. The main point to consider here is whether you plan to implement Root Motion. It is typically easier to do if you isolate the root bone from the rest of the skeleton so that when you do want to use Root Motion, you can simply move the origin to move the whole character. This workflow is also useful when you want to create animations that change the hip placement, but don’t want them to change the root position.
在创建骨骼的根部骨骼时,您可能需要考虑哪种类型的根部骨骼放置最适合您的用例。 通常,有两种方法可以使用:第一种方法是将根骨骼放置在网格外部的任意点,通常是角色的脚之间;将其用作所谓的起点。 第二种是将其放置在角色的臀部,并使其同时充当髋骨。 这里要考虑的重点是您是否计划实施Root Motion。 如果将根骨骼与骨架的其余部分隔离开,通常会更容易进行,这样,当您确实要使用Root Motion时,只需移动原点即可移动整个角色。 当您要创建可改变髋关节位置但不希望它们改变根部位置的动画时,此工作流程也很有用。
There is a selection of other bone editing tools accessible from the Bone Editor. One of them is the Free Bone tool (‘N’). It allows you to create a bone which still has a parent bone but is not directly connected to it. The Free Bone tool is useful for when you have a root bone at the origin which does not connect with the actual mesh of the sprite, as well as for when the elements of your sprite are divided into multiple parts – in this example, we can use free bones to connect our character’s limbs to the rest of the skeleton. Later, when we create our animations, we will be able to move each free bone to a desired position relative to the body of the character, so that they appear to form one complete sprite:
可以从“骨骼编辑器”中访问其他骨骼编辑工具。 其中之一是Free Bone工具('N')。 它允许您创建仍然具有父骨骼但未直接与其相连的骨骼。 “自由骨骼”工具对于以下情况很有用:在原点处具有不与子图形的实际网格连接的根骨骼,以及当子图形的元素分为多个部分时–在此示例中,我们可以用自由的骨头将角色的四肢连接到骨骼的其余部分。 稍后,当我们创建动画时,我们将能够将每个游离骨骼移动到相对于角色主体的所需位置,以便它们看起来形成一个完整的精灵:
The other tools allow you to Split (‘S’) an existing bone into two equal, smaller ones; Parent (‘P’) one bone to another; as well as Move (‘M’) any existing bones that are currently attached, turning them into free bones. Creating a bone by mistake is no big deal – you can get rid of a bone by selecting it and pressing Delete or using the equivalent tool from the tools panel.
其他工具使您可以将现有骨骼拆分为两个相等的较小骨骼; 将一个骨头与另一个骨头“ P”在一起; 并移动('M')当前已连接的任何现有骨骼,将它们转变为自由骨骼。 错误地创建骨骼并不重要-您可以通过选择骨骼并按Delete(删除)或使用工具面板中的等效工具来摆脱骨骼。
Once you’re done editing your skeleton, make sure you click the Apply button at the top right of the Sprite Editor UI! Otherwise, you will lose the changes you have made.
完成骨骼的编辑后,请确保单击Sprite Editor UI右上方的Apply按钮! 否则,您将丢失所做的更改。
Note: It’s a good practice to give descriptive names to your bones. If you have complex skeletons, navigating their hierarchies later will be much easier if you know what you are looking for!
注意:给骨骼起个描述性的名字是一个好习惯。 如果您有复杂的骨架,那么如果您知道要查找的内容,那么以后导航它们的层次将更加容易!
Once we have a complete rig, we can go ahead and generate the mesh for our sprite. Our mesh, together with bone weights which we will add later, will determine how our sprite gets deformed when we move and rotate the bones in the rig. In the top-left dropdown mentioned earlier, select the Skin Weights and Geometry Editor.
一旦有了完整的装备,就可以继续为精灵生成网格。 我们的网格以及稍后将添加的骨骼权重将确定在移动和旋转装备中的骨骼时,精灵如何变形。 在前面提到的左上方下拉菜单中,选择“外观权重和几何编辑器”。
From here, you can create your sprite mesh manually by adding vertices and edges. The tools for doing so are found in the top toolbar: Create Vertex, Create Edge, and Split Edge. However, this can be quite time consuming, especially if your sprites have detailed and complex outlines. For this purpose, Unity gives you the option to automatically generate your mesh, using a set of pre-defined parameters.
在这里,您可以通过添加顶点和边来手动创建精灵网格。 可以在顶部工具栏中找到用于执行此操作的工具:“创建顶点”,“创建边缘”和“分割边缘”。 但是,这可能会非常耗时,尤其是当您的子画面具有详细而复杂的轮廓时。 为此,Unity为您提供了使用一组预定义参数自动生成网格的选项。
In the top tool panel, click on the Generate dropdown.
在顶部工具面板中,单击“生成”下拉列表。
Here, you can set the Outline Detail of your mesh – which will determine how precisely it fits the graphic.
在这里,您可以设置网格的“轮廓细节”,这将决定其与图形的精确度。
Alpha Tolerance will determine how much transparency is taken into account when creating the mesh. Higher values will usually mean a mesh that more precisely fits the pixels (usually with quite a bit more vertices), however, if you set it too high, you might end up excluding semi-transparent segments of your sprite from the mesh. Also, keep in mind that extremely high-detail meshes might create a potential performance concern.
Alpha Tolerance将确定创建网格时要考虑多少透明度。 较高的值通常意味着网格更适合像素(通常具有更多的顶点),但是,如果将其设置得太高,最终可能会从网格中排除精灵的半透明段。 另外,请记住,极高细节的网格可能会引起潜在的性能问题。
The Subdivide level determines how many vertices (and therefore polygons) are created on the inside of the mesh, and might be useful for when you want to create facial animation or otherwise deform elements on the inside of your graphic. It is also useful for better mesh tessellation in case you wish to use custom lighting shaders.
“细分”级别确定了在网格内部创建多少个顶点(因此也创建了多边形),当您要创建面部动画或以其他方式使图形内部的元素变形时,这可能很有用。 如果您希望使用自定义照明着色器,它也可以用于更好的网格细分。
Generally, a good workflow here is to use the auto-generated mesh as a base, and add or remove details by hand where necessary using the editing tools.
通常,这里的一个很好的工作流程是使用自动生成的网格作为基础,并在必要时使用编辑工具手动添加或删除细节。
Once we’ve created our mesh, we need to add some bone weights to it. These will determine which vertices of the mesh are affected by each bone, and by how much. To start editing bone weights, in the top panel switch the editing mode from Geometry to Weights.
创建网格后,我们需要为其添加一些骨骼权重。 这些将确定网格的哪些顶点受每个骨骼以及受多少骨骼影响。 要开始编辑骨骼权重,请在顶部面板中将编辑模式从“几何体”切换到“权重”。
Right away, you will notice that there is a new tool panel at the bottom right of the Sprite Editor. Using it, you can paint bone weights by hand; but just like with the mesh, you can generate the weights automatically with the Auto tool.
马上,您会注意到Sprite Editor的右下方有一个新的工具面板。 使用它,您可以手工绘制骨骼重量; 但是就像使用网格一样,您可以使用“自动”工具自动生成权重。
While editing weights, you can also preview how the various parts of the mesh will be deformed when they are moved. To do so, you can select any bone in the skeleton by clicking on it, and try and move or rotate it to preview how the mesh is affected. You can also reduce the visibility of the painted weights and bones by adjusting the sliders in the top toolbar. This way you’ll get a better idea how the sprite will look in the scene.
编辑权重时,还可以预览移动网格的各个部分时将如何变形。 为此,您可以通过单击骨骼中的任何骨骼,然后尝试移动或旋转骨骼来预览网格的影响方式。 您还可以通过调整顶部工具栏中的滑块来减少绘制的权重和骨骼的可见性。 这样,您将更好地了解子画面在场景中的外观。
Bone weights will generally need more fine tuning than the mesh – you likely want precise control over which vertices get moved by each bone. The Brush and Slider tools in the bottom toolbar exist for this exact purpose!
骨骼权重通常需要比网格更精细的调整-您可能需要精确控制每个骨骼移动哪些顶点。 底部工具栏中的“画笔”和“滑块”工具正是用于此目的!
The Brush tool allows you to paint the weights directly onto the individual vertices of the mesh. You can change the Size of the brush, which determines how many vertices it affects; and the Hardness and Step settings allow you to determine how much the weight of one bone will overlay the weight of another if there is more than one bone influencing a vertex.
画笔工具允许您将权重直接绘制到网格的各个顶点上。 您可以更改笔刷的大小,该大小可以确定它影响多少个顶点。 以及“硬度”和“步长”设置使您可以确定如果有多根骨骼影响顶点,则一根骨骼的重量将覆盖另一根骨骼的重量。
You can start painting weights using the Add and Subtract mode. Simply choose a bone that you want to add weights for by either clicking on it in the Sprite Editor window or selecting it from the brush tool drop-down:
您可以使用加减法开始绘制权重。 只需在“精灵编辑器”窗口中单击要为其添加权重的骨骼,或从画笔工具下拉菜单中选择它即可:
The Slider tool allows you to more universally distribute bone weights, and much like the brush tool, it works differently based on the selected Mode.
滑块工具使您可以更普遍地分配骨骼重量,并且与笔刷工具非常相似,它根据所选模式的不同而工作。
The Add and Subtract mode, in this case, allows you to add a selected bone’s influence to the whole mesh; while the Grow and Shrink mode increases or reduces the bone’s already existing area of influence, and therefore is more confined to the vertices directly around the bone.
在这种情况下,“添加和减去”模式允许您将选定骨骼的影响力添加到整个网格。 而“生长和收缩”模式则增加或减少了骨骼已经存在的影响区域,因此更局限在骨骼周围的顶点。
The Smooth mode is used to even out transitions between all bone weights in the skeleton. Using this mode with the brush tool allows you to do the same, but only for localized areas.
平滑模式用于使骨骼中所有骨骼权重之间的过渡均匀。 将此模式与画笔工具一起使用可让您执行相同操作,但仅适用于局部区域。
When rigging your sprite, you will most likely want to have control over how its parts will overlay each other. For example, you might want the right arm of your character to appear in front of the body, and the left arm to be behind the body, and so on. As such, 2D Animation allows you to set the render order of your bones, called Bone Depth.
装配精灵时,您很可能希望控制其各个部分如何相互重叠。 例如,您可能希望角色的右臂出现在身体的前面,而左臂出现在身体的后面,依此类推。 因此,“ 2D动画”允许您设置骨骼的渲染顺序,称为“骨骼深度”。
Bone Depth is configured in the Weights editor and is set per-bone. To change the render order of a bone, select it in the editor window. At the bottom left of the window, you will see an Inspector pop-up. Here, you can set your rendering priority. Higher values mean that parts of the mesh associated with that particular bone will overlay those with lower values. By default, all bones have a depth of 0.
骨骼深度在“权重”编辑器中配置,并按骨骼设置。 要更改骨骼的渲染顺序,请在编辑器窗口中选择它。 在窗口的左下方,您会看到一个检查器弹出窗口。 在这里,您可以设置渲染优先级。 较高的值意味着与该特定骨骼关联的网格部分将覆盖具有较低值的那些部分。 默认情况下,所有骨骼的深度均为0。
Once you have set up a rigged mesh and painted bone weights, you will be able to use it in the scene. To do so, first drag the rigged Sprite asset into the scene, and add a Sprite Skin component to it. If a sprite contains a skeleton, you will have the option to Create Bones, which will instantiate that skeleton in the scene.
一旦设置了绑定的网格并绘制了骨骼权重,就可以在场景中使用它。 为此,首先将绑定的Sprite资产拖动到场景中,然后向其中添加Sprite Skin组件。 如果精灵包含骨骼,则可以选择创建骨骼,它将在场景中实例化该骨骼。
From here, you are free to move and rotate the bones however you wish to achieve the desired pose for your character. To rotate a bone, either select it and use the Rotate tool (‘E‘), or grab the end of the bone and drag it in the desired direction. To move a bone or detach it from its parent, use the Move tool (’W’) or grab it at the origin and drag. To reset the bones to their default position in the rig, you can use Reset Bind Pose in the Inspector. You can also select multiple bones to rotate them uniformly as a group – this might be useful when trying to create rounded shapes, such as those for hair or roots.
从这里开始,您可以自由移动和旋转骨骼,但是您希望为角色实现所需的姿势。 要旋转骨骼,请选择它并使用“旋转”工具('E'),或抓住骨骼的末端并将其沿所需方向拖动。 要移动骨骼或将其与父骨骼分离,请使用“移动”工具('W')或在原点处抓住它并拖动。 要将骨骼重置为装备中的默认位置,可以使用“检查器”中的“重置绑定姿势”。 您还可以选择多个骨骼以使它们均匀地成组旋转-这在尝试创建圆形形状(例如用于头发或根部的形状)时可能很有用。
Let’s do a quick walkthrough of how you can use your newly built skeleton to create a complete 2D animation!
让我们快速演练如何使用新建的骨架来创建完整的2D动画!
With the 2D Animation package, you can create skeletal animations just like you would in 3D projects before. The simplest way to do so is to select the rigged sprite and open the Animation window (Window > Animation > Animation). From here, you will be prompted to create an Animator component, and the first Animation Clip for this game object. Once you click Create, you can choose the folder where you wish to save these components, and name your first Animation Clip. An Animator asset will be created automatically, but you can rename or move it wherever you wish. Alternatively, you could create the Animator first (Assets > Create > Animator Controller) and attach it as a component onto the game object. After that, you only need to create the Animation Clip.
使用2D动画包,您可以像以前在3D项目中一样创建骨骼动画。 最简单的方法是选择绑定的精灵,然后打开“动画”窗口(“窗口”>“动画”>“动画”)。 从这里,将提示您创建一个Animator组件,以及该游戏对象的第一个Animation Clip。 单击“创建”后,可以选择要保存这些组件的文件夹,并命名第一个动画剪辑。 将自动创建一个Animator资产,但您可以将其重命名或移动到所需位置。 或者,您可以先创建Animator(资产>创建> Animator控制器),然后将其作为组件附加到游戏对象上。 之后,您只需要创建动画剪辑。
All the animating work happens in the Animation window – you can have multiple Animation Clips per Animator. To create new clips, at the top left of the Animation window you will find a dropdown with the name of the currently selected animation clip. If you click on it, you will see the option to Create New Clip.
所有动画工作都在“动画”窗口中进行-每个Animator可以有多个动画剪辑。 要创建新的剪辑,请在“动画”窗口的左上方找到一个下拉列表,其中包含当前所选动画剪辑的名称。 如果单击它,您将看到创建新剪辑的选项。
At the start of your animation, it is a good practice to keyframe all of your bone positions and rotations – even if you aren’t going to change all of them. Otherwise, if you later move or rotate the bones in the scene that haven’t been affected by the animation, they will keep their transforms from the scene, which often changes the animation in an undesirable way.
在动画开始时,最好对所有骨骼位置和旋转进行关键帧设置,即使您不打算更改所有骨骼位置和旋转。 否则,如果您以后在场景中移动或旋转不受动画影响的骨骼,它们将保留其变换,从而经常以不希望的方式更改动画。
In order to keyframe all of your bones and start recording the changes in the animation, you must first enable recording mode in the Animation window by pressing the red button at the top toolbar. To create your initial keyframe, select all the bones of the rig in the Hierarchy window, and in the Inspector you will be able to right-click the Position and Rotation transforms and choose to ‘Add Key’. This will create keys for all selected bone positions and rotations, respectively. To make sure you’ve selected all the bones and their children, you need to expand your bone hierarchy before you select everything. A quick shortcut to doing this and making sure you don’t miss any bones is to hold Alt (or Option for Mac) and Left Click on the root bone. This will either collapse or expand all of the root’s nested children.
为了对所有骨骼进行关键帧设置并开始记录动画中的更改,必须首先通过单击顶部工具栏上的红色按钮在“动画”窗口中启用记录模式。 要创建初始关键帧,请在“层次结构”窗口中选择装备的所有骨骼,然后在“检查器”中右键单击“位置和旋转”变换,然后选择“添加关键点”。 这将分别为所有选定的骨骼位置和旋转创建关键点。 为确保已选择所有骨骼及其子级,需要在选择所有内容之前扩展骨骼层次。 确保您不会遗漏任何骨骼的快速捷径是按住Alt(或Mac的Option)并在根骨骼上单击鼠标左键。 这将折叠或扩展根的所有嵌套子代。
From here, while in recording mode, you can move the animation playhead to a new position, and start rotating or moving the bones in the scene to automatically create new keyframes.
从此处开始,在录制模式下,您可以将动画播放头移动到新位置,并开始旋转或移动场景中的骨骼以自动创建新的关键帧。
You can move keyframes along the animation timeline by selecting and dragging them to a desired point; or copy and paste selected keys with familiar shortcuts – Ctrl (Cmd) + C and Ctrl + V respectively. Once you’re done editing the animation, you can exit recording mode and use the Play button to preview the animation.
您可以通过选择关键帧并将其拖动到所需的点来沿动画时间轴移动。 或使用熟悉的快捷键复制和粘贴选定的键-分别为Ctrl(Cmd)+ C和Ctrl +V。 编辑完动画后,您可以退出录制模式并使用“播放”按钮预览动画。
And now you’ve got everything to get you started with creating your own skeletal animation! Here’s an example clip of a walk cycle for our viking that can be put together really quickly:
现在,您已经拥有了一切,可以开始创建自己的骨架动画! 这是我们的北欧海盗步行周期的示例片段,可以很快地将其放在一起:
There is additional information on working with 2D Animation in the preview docs, currently available on the feature’s GitHub repository.
预览文档中提供了有关使用2D动画的其他信息,当前可从该功能的GitHub存储库中获得 。
On the same repository, you will find several sample projects that you can play around with which demonstrate the basics of the workflow described in this blog post.
在同一个存储库上,您将找到几个示例项目 ,这些示例项目可用于演示此博客文章中描述的工作流的基础。
Stay tuned to this blog and learn how to get Started with 2D Inverse Kinematics in the second part of this series.
请继续关注此博客,并在本系列的第二部分中学习如何开始使用2D逆运动学。
We would love to see what you are creating with our new 2D packages, and to hear about your experience using them!
我们很乐意看到您使用我们的新2D软件包创建的内容,并了解您使用它们的经验!
That’s one of the reasons we launched the Unity 2D Challenge. Create a small piece of content using some of our new 2D tools and you can win cash prizes and tickets to Unite! It can be anything from some pixel-perfect art to a thin vertical slice of a 2D game. The challenge is open for submissions until December 17, so now is a great time to get started.
这就是我们发起Unity 2D Challenge的原因之一。 使用我们的一些新2D工具创建一小部分内容,您将获得现金奖励和Unite门票! 它可以是任何像素完美的艺术品,也可以是2D游戏的垂直薄片。 挑战赛截止到12月17日截止,因此现在是开始的绝佳时机。
You can also share your thoughts and projects with us on the 2D Animation Forum!
您也可以在2D动画论坛上与我们分享您的想法和项目!
翻译自: https://blogs.unity3d.com/2018/11/09/getting-started-with-unitys-2d-animation-package/
unity动画入门