📜 ⬆️ ⬇️

Optimization of animations in Unity3D

Use frame-by-frame animations in Unity3D


Surely, you have noticed that creating a new animation in Unity 5 immediately places a link to the SpriteRenderer component's sprite . Unity calls you to select the prepared frames and drag them to the Animation window.



A couple of mouse movements and new animation decorates your game. But for 1 second video you need 12 - 30 frames. And if a character performs a dozen different movements: runs, reads, plants, waters, plays, sunbathes, etc., then the duration of all the animations passes in a minute, and frames per thousand. And the character is not the only animated object on the level. Here we also received that not especially difficult scene is loaded longer than a minute, and Unity starts to fly because of an overrun of random access memory. Yes, of course, you can use the built in Unity SpritePacker to pack sprites into atlases, but this gives only a minor result and an improvement in productivity by 10 - 20%.


')

Replacing frame-by-frame animations with skeletal


To achieve more tangible results, we decided to replace frame-by-frame animations with skeletal ones. Instead of a rectangle on which the sprite is drawn, a more complex model is created, on which parts of the character are drawn from the atlas.



Animation is obtained by the movements of this model. Do not worry, in fact, everything is simple! There are several tools for creating skeletal 2d animations for units: Spine , DragonBones , Anima2D , Adobe Flash , etc.
Since Our artist did all the animations in Flash for us best suited GAF .

Importing animations from Flash to Unity3D


  1. Import GAF from Unity AssetStore ;
  2. Drag the * .swf file into the converter window;
  3. Create an object and customize it. All settings are intuitive:



Animations are created automatically, based on the markup made in Flash .

To run the necessary animations, we use scripting:

using UnityEngine; using GAF.Core;//   GAF public class Mouse : MonoBehaviour { //      [SerializeField] private GAFMovieClip GAFMovieClip; void Update () { if (Input.GetKeyDown(KeyCode.RightArrow))//   "" GAFMovieClip.setSequence("run", true);//  run  } } 

Setting transitions in Unity Animator


In the Pro-version of GAF , it is possible to use a native Unity animator, which will make development much easier. However, we must remember that our type of animation does not support mixing. Unity defaults to mixing clips. So our animations can break at the transitions between clips. To make the animation work smoothly turn off the mixing of clips:



Editing Animated Clips


If it turns out that you did not create the necessary clips in Flash, you can assemble them directly in Unity , by calling the updateToFrameAnimator () function and specifying the required frame as a parameter.



Results:


So replacing frame-by-frame animations with animations created in Flash , the loading time of scenes has decreased by 10 times! Accordingly, the consumption of RAM was significantly reduced.

Useful links:


Video Tutorial "Using Flash Animations in Unity3D"
GAF: import of Flash animations in Unity3d

Source: https://habr.com/ru/post/322280/


All Articles