Book Image

Unity 2D Game Development Cookbook

By : Claudio Scolastici
Book Image

Unity 2D Game Development Cookbook

By: Claudio Scolastici

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.</p> <p>Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.</p>
Table of Contents (15 chapters)
Unity 2D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Exporting animations


It is very likely that models are enriched with animations to improve the quality of their behavior as game objects. In the following recipe, we will see how to export animations from Maya.

Getting ready

When exporting animations, two approaches are possible. One, which we will adopt in this book, is to keep things on separate files. With this approach, we export one FBX file for the model, the materials, and the rigging (as we did before), and one FBX file for each individual animation, such as idle, run, jump, and the like.

The other approach is to export everything on a single file. In this case, the exported FBX file will consist of the model, materials, rigging, and a timeline containing the frames for the entire animation set of the character. We will discuss this second approach later.

As usual, we took care of providing the required assets (animated Maya scenes for the model) in case you don't have any.

How to do it...

  1. With the root node of the animated model selected in the outliner, navigate to File | Export Selection as usual.

  2. Select FBX File Format in the exporting panel and name your file. If you want Unity to automatically read the animation name when it is imported, follow the official naming convention that requires the animation file to be named modelName@animationName (with @ before the animation name). Assuming that we are about to export the idle animation for our character, name the file modelName@idle.

  3. Select the destination directory for the Animation file, which should be under Animations in your Unity project.

  4. In the Options... panel, we need to make some adjustments to import the animation correctly. Under the Animation tab, flag the Animation option.

  5. In the Bake Animation field, check that Bake Animation is flagged and that the starting and ending frame for the current animation are selected (this should be done automatically by Maya).

  6. In the Deformed Models field, check that all the flags are selected. These settings may change, depending on the specific requirements of each individual model and animation set.

  7. You don't need to change anything else with regard to the setting we defined to export the static model, so you can click on Export Selection.

How it works...

As we will see in the following recipe, where we will edit the settings of the imported animation in Unity, what we get is an FBX file containing an animation clip named idle, which represents the idle animation for our character.

As we said before, it is possible to trace the animation clips on a single file\timeline and export all character animations in one FBX file; in this case, however, additional operations are required when compared to the one-file-one animation technique.

With a single file containing all the clips, Unity is not capable, by default, to read the timeline imported from Maya and automatically detecting the individual clips, and you end up with a single, most likely long, timeline named track01 containing all the animations.

So you have to split the whole timeline by yourself, naming each clip individually and manually setting the start and end frames for each one of them in Unity Inspector.

If you think about a typical working pipeline with different people taking care of different operations, you may find your animators manually writing a text file of some sort with information regarding animation clip names and their reference frames. The animators then pass on this text file to someone responsible for taking care of importing animations in Unity. This last guy would also be responsible for typing the data found in the text file into Unity Inspector. A way of doing things that can easily lead to human errors, as you can imagine...

This is the reason why we prefer using separate FBX files for each animation clip.

It is also possible to automate the exporting process so it doesn't become a time-consuming activity, but you may need a programmer for this.

The idea is to script a piece of code in MEL to handle the job. MEL is the scripting scaffold of Maya: any operation you perform in the Maya editor has an equivalent instruction in MEL. Since performing hardcoding in the MEL scripting language would go beyond the scope of this book, we just provide a few references here for those interested. The list of MEL exporting commands is available at http://download.autodesk.com/us/fbx/20112/Maya/_index.html.

An example MEL script, courtesy of James Kyle, is available at http://www.jameskyle.net/2013/03/maya-to-fbx-batch-export/.

There's more...

For those of you who are interested in automating the exporting process, there are ways, pretty elegant too, that require advanced programming skills.

One way is to create a Maya Embedded Language (MEL) script that reads the scene in Maya and exports what you need, based on the settings you define for the exporting process. MEL is the programming language behind Maya; any operation performed in Maya can be converted into a scripting instruction that will achieve exactly the same result. By using MEL, you can thus create a script that automatically exports animation clips into Unity on one or more FBX files, helping you save time (and reduce the risk of errors).

Another option is to configure Maya to generate an XML file that describes the animation data stored in the timeline of a model (animation names, starting\ending frames, and the like), and then read this XML file from Unity to automatically create the required FBX files.

Both these approaches are very similar to using the post-processor to read custom attributes from Maya, as we discussed earlier.

If you'd like to go that way, you can refer to the following links: