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.


24 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

  4. Hey Rakesh. So you mean to say that we can control animations through unity’s Animation functions or dragonbones ones. The functions of db lack documentation. I don’t know how fade in works and and what parameters and all to pass.

    1. No it’s not through unity’s animation function. UnityArmatureComponent is a DragonBones class.
      And yes, there is very little documentations and whatever is there is in Chinese i think. Only the demo project that comes along with the plugin helps a little to learn about it. You can refer my test project too. its available in this page.

      1. Rakesh. Okay but not all makes sense since my coding skills are bit rusted. Can I reach out to you by social media say Twitter in case I have doubt?

  5. “The GotoAndStopByProgress function can be used to control an animation frame by frame”…… i would really like to get to know more on this cause i am making a game where the player shoots and would love to use this method …. one question is how do you set it up in dragon bones cause i am guessing there is sprite attachment so that when you import it in unity you could control the character

    1. There isn’t much about the function except what is mentioned up this page.
      GotoAndStopByProgress(animation, progress).

      The sprite sheet will be generated when you export an animation from dragonbones software. Importing the json file in Unity will import the sprite too.

      And for both your questions, you can take a look at my example project linked in this page. There is a very simple character animation. Let me know if it helped 🙂

  6. How can I play 2 animation on a sequence. because if i just put another animation below, only the last animation that will be executed.

    1. You need to play 2 animations in separate layers
      Example:
      armatureComponent.animation.FadeIn(“animation 1”, 0.05f, -1, 0 /*layer 0*/);
      armatureComponent.animation.FadeIn(“animation 2”, 0.05f, -1, 1 /*layer 1*/)

  7. Hello,
    Please explain what the parameter of the group affects and how to understand where to get the correct name of the group? – “aim”

    aimState = armatureComponent.animation.FadeIn (aimDownAnimation, 0.01f, 1, 0, “aim”);

    1. Sorry for late reply… i was away.

      You have to name the group yourself. Just have an unique name for a group and reuse it whenever needed.
      For example suppose you want a character that walks and aims at same time.
      Have a group called “aim”. Put aim animations in different layers of it.
      And have a group called “walk”. Put walking animations in different layers of it.

      Check the example project in the following URL. There I used default group for all walking/running/jumping animations. And “aim” group for aiming the arrow. The function prototype is FadeIn(animation, fade_in_duration, loop, layer, group) FYI. If group is not specified, it uses default one.

      https://bitbucket.org/rakesh_malik/dragonbones-example/src/9befc34f1f63a0eb6f80d2c6c22877629f4e69e2/Assets/Scripts/CharacterAnimation.cs?at=master&fileviewer=file-view-default

  8. Hi Rakesh,

    This has been a great post to introduce me to DragonBones and Unity. On your video, did you use the built-in Animator in Unity to control the states, or did you do this via C#. Could we use the Animator and DragonBone data together?

    Thanks for the great tutorial though!

Leave a reply to Rakesh Malik Cancel reply