Using DragonBones in Unity

logo512

I recently came across this Animation tool called DragonBones. Which looks very light and easy and compatible to unity using this Unity C# Plugin. Though the plugin is really lacking documentation, it comes up with a demo project which can give you an idea about how to use it. So here I am posting a easy guide here for get started with it fast.

Creating animation

First I created parts of my character as images. And then put them together in DragonBones with a bone structure. Then created few animations, such as Walk, Jump, Run etc.

Exporting for Unity

Dragonbones saves the project as a *.dbproj file. It can be exported to a compatible for for unity plugin.

Screen Shot 2017-07-08 at 5.34.47 PM

It creates 3 files. A *_tex.png file which contains texture atlas, a *_tex.json file which contains texture information and a *_ske.json file which contains the skeleton and animation information.

Setting up DragonBones Plugin for Unity

For setting up only the DragonBones & MiniJSON has to be imported into your unity project. i.e. Just copy the DragonBonesCSharp/Unity/Demos/Assets/Scripts/DragonBones folder into your unity project Asset folder.

Importing in Unity

Put the exported json and png files under Asset/Resources in your project. Open a scene in your project. Create a new empty object. Add new component to that object as “Unity Armature Component”.

Screen Shot 2017-07-08 at 5.52.29 PM.png

Select a your *_ske.json file as DragonBones Data. A Create button would appear. Pressing that would import the model and animations.

Screen Shot 2017-07-08 at 5.56.46 PM.png

Using Plugin for Animations

Animations can be easily handled from code by accessing the UnityArmatureComponent component. Attach your C# code to the same GameObject and try adding a code similar to the following code.

 UnityArmatureComponent armatureComponent = GetComponent<UnityArmatureComponent> ();
 armatureComponent.animation.FadeIn ("Walk", 0.25f, -1);

The default animation for my “Rama” character was “Idle”. This would make the a transition to “Walk” animation in 0.25 seconds, and loop in “Walk” animation infinitely (-1).

The GotoAndStopByProgress function can be used to control an animation frame by frame. Such as moving hand of the character according to mouse movement.

public void LoadArrow(float arrowLoadProgress) {
    armatureComponent.animation.GotoAndStopByProgress (loadArrowAnimation, arrowLoadProgress);
}

And the is a few more similar functions in the library. These can be mixed to create awesome animations. The following video shows result of a small implementation.

Just for information, I have used Unity 5.3 and DragonBones Pro 3.9.

I have uploaded the example project in BitBucket. Please find it in the following link if you require help. Animation related operations are available in Assets/Scripts/CharacterAnimation.cs.

Repository Link: bitbucket.org/rakesh_malik/dragonbones-example external_link-512

Please leave your comments if you liked or disliked my post.

Advertisements

7 thoughts on “Using DragonBones in Unity

  1. Hello, great tutorial. Can you please upload this examples code on github or somewhere else? Can’t handle using FadeIn or Run in Update() game cycle. But in this video animation changes in a runtime.

  2. hi Rakesh

    Wow this is so inspiring, Thank You! 🙂

    I have been trying out DragonBones animations in my Unity Projects lately and I have been making lots of progress UNTIL I tried to build my game with Unity’s compiler. No matter what I tried I kept getting this same compile error:

    Error building Player because scripts had compiler errors
    UnityArmatureComponent.cs

    The scripts work well and the game plays well in Unity until compilation then this UnityArmatureComponent.cs error appears. My search on the web revealed nothing yet. I would just like to know if you had a similar serror compiling DragonBones scripts in UNity? Any feedback would indeed be greatly appreciated as I am Loving working with DragonBones so far and it would be a shame to ditch it now because of this failure to compile the game. Thanking you in advance 🙂

    1. Hi, Glad that you found it helpful. 🙂

      But I didn’t get any such errors. Could you mention what were the compilation errors and what platform you were trying to build for. And you can try getting help from Unity Forum or Unity Answers. I mean post your issue there. It’s quite helpful.

  3. Hello, your tutorial really help much! 🙂

    But I’ve encountered a problem where dragonBones armature cannot show up in unity canvas.

    Is it possible to use armature in unity canvas?
    Can you make a tutorial on using armature in unity canvas?
    Or the only way is to export an image sequence then use it in Canvas Image?

    1. The plugin does not show the armatures on Unity canvas. It only shows the images and animation. If you need to do any changes in armatures, you need to do that in DragonBones application and reimport it in Unity

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s