📜 ⬆️ ⬇️

UE4 for Unity developers: creating 2D games



Continuing the comparison of the top 3D engines - Unreal Engine 4 and Unity, this time we will look at their advantages and disadvantages when developing 2D projects. We will find out how Unreal is good for 2D, how work is organized with the basic elements of a two-dimensional game, and by what means can 2D-characters with skeletal animation be implemented.

We have already compared Unity 5 and UE4 in relation to 3D games. In short, we came to the conclusion that Unreal is cool and it can be used to develop high-quality three-dimensional games. But what is the situation in the world of 2D games? Compare Unity 5 and Unreal Engine 4.16.

image
Unreal Engine 4 vs Hollow Knight (Unity)
')
If the popularity of Unreal for 3D is beyond doubt, then, for example, there are practically no commercial 2D projects made on it. Therefore, the question arises: is it worth it to use Unreal for 2D and is it too redundant for such projects?

Consider this question on the example of the game Hollow Knight (in the background of the screenshot). It is made on the Unity engine, the PC version takes 8.5 GB, a very powerful project. If you make a similar game on Unreal, the runtime libraries will take in the project build about 100–200 MB. For large-scale projects, this is not so scary, because even mobile games sometimes weigh a couple of gigabytes.

What can Unreal help us in creating a 2D game?


The advantage of Unreal is that it allows you to make a game on the Blueprints. Bluprint is a system of visual programming tools with which you can create a game entirely on visual schemes - without in-depth knowledge of any programming language.

Unreal Engine Blueprint

Often, not only programmers, but level designers, animators, game designers use blueprints. They are embedded in all subsystems of the engine, and they can be extended with C ++ code.

UE4 Blueprint logic
Implementing game object management through blueprint

Alternatively, if you are just starting to create games, try making the first game in 2D, because it is, by definition, simpler than a 3D game. 2D will require fewer components, the game mechanic itself is "flat." Of course, in such a project, you can still use 3D models as a background, if necessary. It’s easier for a novice developer to understand the bluprint system than to learn C # for Unity or C ++ for UE4.

If you have already tried Unreal, you may be familiar with the Unreal Gameplay Framework - this is a whole set of engine entities, from the player controller controls to the HUD, the game interface and animation. Despite the fact that the set of 2D components is slightly different from those used for 3D, the general principles of operation are undoubtedly present in them.

image

Also in Unreal there is a wonderful multiplayer. This is the implementation of a high-level client-server interaction built into the engine, with which you write both client code and server code right away. By the way, this problem is also solved by means of bluprint. At the same time you can build a dedicated server on the engine. In the Unity engine there is no such possibility. In addition, the editor is convenient to test multiplayer games, running the right number of copies of the game. This launch mode is not much different from the usual and does not require additional time.

2D to Unity


Unity 2D editing

What does Unity give us for 2D games? To begin with - the Sprite Renderer component, on which the 2D graphics game is based. We can transfer sprites from the content folder to the scene in the editor, build on the basis of them the elements of the level and game objects. Immediately, we note that in Unreal similar scene editing mechanics. We can drag the sprite component onto the scene, the usual drag-and-drop. Unity offers two ways to implement sprite (frame-by-frame) animation:



A remarkable feature of Unity is the ability to turn on automatic packaging of used sprites into atlases. At the stage of creating the game, you can not worry that you have a lot of sprites, that you need to somehow assemble them for optimization. However, this feature is disabled by default, most likely to speed up the project build during development.

2D to Unreal


image

In Unreal Engine 4, the creation of a project or prototype often begins with the choice of a template, so consider this step. To give developers a way to start, there is a 2D Side Scroller project template. This is an analogue of the 3D Side Scroller template, but the graphics in it are based on the standard Paper2D plugin. It gives us support for 2D sprites, i.e., the very basic graphic elements from which the game is assembled.

In addition to sprites, there are animated clips, they are called flipbook. It looks like a notebook, when we draw one frame on each page, and then quickly scroll to get a moving picture. Flipbooks are a simple 2D animation tool. They, like sprites, can be imported from the Texture Packer program, it allows you to customize the texture atlas layout. Having loaded the atlas into Unreal, we will get each sprite as a separate asset (Asset), which is then simply dragged onto the stage.

UE4 2D Side Scroller

Here we see a screenshot of the editor with the 2D Side Scroller project. It implements character control, character interaction with the world (physics), and animation switching. Despite 2D graphics, the physics in the project is three-dimensional. This is due to the fact that the experimental 2D physics, implemented on Box2D, is only on the Windows platform. Because of this, 2D sprites are attached to a three-dimensional physical representation. The approach is a bit strange, so for the character PaperCharacter there is a special flag that limits its movement in a given plane.

In Unity, unlike Unreal, elements for 2D physics are fully supported: there are colliders, Rigid Body (solids) and other components.

Creating 2D levels in Unreal


Usually the creation of levels is realized by transferring sprites to the game scene in the editor. But besides that, Unreal has an interesting tool for editing levels by creating tiles (Tile) and tile maps (TileMap).

UE4 Tilemap

Before starting to create a level, we pre-create a set of elements of a fixed size (tiles) and then build a level of them, and Unreal gives us the necessary tools for editing tiles and maps. Tiles combine both a graphic part and colliders. The resulting tile level is automatically optimized in terms of both physical presentation and rendering (drawcall optimization).

Unreal also allows you to link items at the level of bluprints. Without any special tools, you can implement such scripts as “When a door is opened, a monster appears” or “If a barrel explodes, an event will occur”.

UE4 Level Blueprint
Fragment of "bluprint level" in UE4 with the implementation of the reaction to the player's entry into the trigger zone

In Unity, to implement such connections, we would have to create a script for each game interaction mechanics or a control script for a specific level. For a large project, you would have to build a cut-scene or event system.

2D characters in Unreal


The Unreal Paper2D plugin provides us with the PaperCharacter component. This is a character based on a Flipbook, i.e., on a sprite time-lapse animation system. We will have the character itself and its physical model in the form of a 3D capsule.

Flipbook animation

To implement the old school movement there is a special flag of the flat base of the capsule. He allows, one might say, return to the past and make the character, standing on the very border of the step, not fall down. This point may be key for some game mechanics or level features.

UE4 old school physics
Old-school physics enabled!

What about the characters with the skeleton? Both in Unreal, and in Unity 5 there is no built-in component for two-dimensional skeletal animation, but we will consider this question further.

Spine for 2D skeletal animation


Spine is a comprehensive package and SDK for creating two-dimensional characters. With the help of it, the skeleton of the character is created, sprites are attached to it, and they are not necessarily rectangular: you can give them the desired shape. The animation turns out to be skeletal, completely analogous to that used for 3D characters. Unlike sprite, it will be smooth due to the fact that between the keyframes of the animation interpolation of the movement of bones is performed. Animation files at the same time weigh less, plus Spine allows you to bind objects to characters.

Spine
Spine main window displaying character bones

Thus, in Spine, you can completely create a character, including all its animations and object snapping settings. In addition to the animation itself, Spine makes it possible to fix events, for example, the sounds of steps, to any moment of the animation. You can customize them directly in the editor, see how the character and its events are combined with animation, and export to a format suitable for the engine. When exporting from the program, we get an atlas file with pictures - parts of the character and JSON with animation and additional settings. A character may have several skins, while the skin may have additional sprites attached to the bones.

It is also stated that Spine supports mesh deformation. When moving bones, sprites can drag on bones. This is not in all such packages, but the possibility is remarkable, as it can be useful for creating certain characters. Also, this system allows you to blend (mix) animations among themselves and manage them programmatically. In other words, by creating one run animation, for example, we can smoothly go into the walk animation, and vice versa.

Because of this specific set of features and the variety of platforms supported, the Spine system itself often goes beyond the main animation system in the engine. If in Unity, for example, you can create a hierarchy of bones, which was originally in the character (this is done with one button), then there is no such thing in the plugin for Unreal. There you can create special components for binding ector to the bones. We set, for example, the name of the bone, and the object is attached to it.

Spine for Unreal Engine 4


In recent versions of Unreal, a bug was fixed that prevented the Spine plugin from being built, and now it can be used. All examples of working with Spine are implemented on blueprints, and access to the API is both from the code and from the blueprints. For example, there are examples with the movements of the character, the blending of the animation and the reproduction of the sound on the animation events.

UE4 Spine

There is also a convenient event binding, you can attach a special event to any point in the animation. Events named, like the animations themselves, in Unreal, you can easily attach the delegate of an event by name and immediately implement the game's reaction to it.

Spine Play Animation

The main disadvantage of Spine is that the animation system is in fact not related to Anrialovsky. All that Unreal gives us (animation management via Animation Blueprint, various blending schemes, animation editors and their sequences) - all this we cannot use for Spine and 2D animations. You'll have to control the animation and switch it manually, in the Tick function, and most likely, this will require additional timers and states. Something like this approach resembles the old animation system in Unity 4, in which you had to use the Play, Blend and CrossFade functions in the Animation component.

Nevertheless, the plugin is good because it allows you to use 2D skeleton animation, which is not in Unreal. With it, you can create 2D-characters with dynamic and smooth animation, save the memory and work of artists by tying objects (such as weapons) to the bones of the character instead of creating an additional set of animation for each weapon.

2D optimization in Unreal Engine 4


A typical optimization method is packing sprites of a level into atlases. This saves us from switching textures. In the Unreal Engine, if you drag even a few identical sprites onto the scene, you get the same number of drawcalls. The built-in automatic optimization of rendering for 2D is absent, so it makes sense to select some parts of the levels, group them using the MergeSprites tool. The Merge Sprites command is similar to the Merge Actors for 3D objects, it combines several objects into one. Merge Sprites also combines collisions of objects.

UE4 sprite mesh editing
Editing the sprite geometry

Also in the engine there is a tool for editing the geometry of sprites. As shown in the picture, it allows you to set your own instead of a standard rectangular sprite. For example, by cutting out transparency, you can reduce overdraw and thus increase performance.

As in the three-dimensional graphics, in 2D the optimization through the material instance is relevant. It is possible out of habit, as in Unity, to copy materials, but in Unreal this is not worth doing. It is better to make one or several base materials that actually describe the rendering shader, and on its basis make material instances with a set of variable parameters (colors, textures and constants). Due to this optimization, the engine will not need to create a large set of compiled versions of shaders, which will save the size of the project after assembly, free up some amounts of RAM and video memory, and increase performance.

2D assets (Marketplace)


To accelerate the development of the prototype of the game often allow the assets, which can be purchased in the store. Similar to the Unity Asset Store, Epic Games has a Marketplace for UE4. There are few 2D assets in it, but they exist. Basically, these are sets of art: levels and characters. Sometimes there are templates for entire projects such as platformers, runners, etc. They can be used as a basis for a project or for creating prototypes. But, unfortunately, there are no special tools for 2D projects, as well as effects oriented on 2D. In this regard, everything is much better in the Unity Asset Store: more features, even systems for 2D animation, about five pieces: Puppet2D, Anima2D, etc.

Remarkable features UnrealEngine 4 in 2D


I managed to find in anriale and some remarkable features. For example, in the Paper2D flipbook sprite animation, you can set your own physical collider for each frame. That is, you can make an unusual object with one such asset, such as a magic gate, which opens and closes with animation. Probably, to change collision in each frame is not very optimal, but nevertheless there is such an opportunity.

Sprite shadows

In the screenshot of the three-dimensional character sprites surround, casting realistic shadows. In fact, they are two-dimensional, and you can, without creating special tools and crutches, turn on 2D shadows from sprites and even a reaction to lighting. This is done by replacing the material used for the sprite. For this there are materials Sprite Lit and Sprite Lit Shadows. This can be useful if your 2D game contains a 3D environment, that is, three-dimensional levels, and the characters themselves are flat. For example, in the game Ori And The Blind Forest there are such elements.

You can also enable special sorting of sprites in the project settings. If you have a regular 2D project with a side view, then you probably do not really care how the sorting is done. Any sprites you can just push it into the background. But if you have an isometric game and you need the order of the dynamic objects, then it may make sense to set the sort axis as an axis up, and then they will automatically be sorted.

I met the Unity-project, in which a similar sorting was done in the script for each object. Unity has a parameter for sorting sprites - sorting layer offset, and each script, each frame, calculated an offset value based on the current coordinates of the object. Of course, this method can not affect the performance. The sprite sorting axis solves a similar problem with one click.

UE 4 performance on mobile devices


There is a myth that UE4 has poor performance. But in fact, it is quite possible to create epic in its beauty projects, even in 3D. However, as in all engines, performance and optimization are interconnected. You just need to take this into account when developing and in time to do profiling and optimization. As for 2D, I have already listed some points. In addition, you may want to draw some 2D sprites through canvas. Much depends on the context and the project as a whole.

UE4 vs Unity 5: 2D


Summarize. Should I use Unreal at all and which features are critical for the development of 2D projects, and which ones are not? I have compiled a list in order of importance of features.

FeatureUnreal Engine 4Unity 5
2D physicsWindows onlyYes
SpritingManualAuto
Sprite effectsNotYes
2D Tools on the StoreFewYes
Shadows from spritesYesNot
Visual "level script"YesNot
Tile level editorYesNot
Collision model animationYesNot

Consider each item in a little more detail.

2D physics. From my point of view, the main problem is that in Unreal now there is no two-dimensional physics for most platforms, there is only an experimental implementation for Windows. While on most platforms there is no 2D physics, this will hurt and physics will have to be done through 3D components and artificial constraints.

2D sprite batching. Unity does not write the number of drawcalls for 2D sprites, if we add a lot of 2D sprites to the scene, it will not be displayed in the statistics window, but the performance speaks for itself - the engine still unites them into groups during rendering. And in Unreal you have to use the Merge Sprites tool, or perhaps wait for a better solution than the existing Paper2D system.

Effects of sprites. In Unity, you can find various assets for the effects on sprites, in the Unreal Engine this is not yet.

2D tools in the Store. For Unity, there are various tools in the Store, i.e., both plugins, and extensions, and effects. In the Unreal Marketplace, unfortunately, only 2D projects and their templates, the toolkit is almost absent.

Next come the features that will not need any project, such as shadows from sprites. The advantage of Unreal can be considered as visual scripting, including level blueprint logic. The tile level editor is useful if you want to make an old school game with pixel graphics in Unreal. Why not? And the animation of the collision model is also an interesting experimental feature.

Conclusion


To summarize: Unreal has the most basic tools for 2D, despite the presence of some interesting features. If you want to use skeletal animation, you will have to additionally explore third-party tools. Alas, there is no convenience similar to the one that Unreal offers in 3D. The main problem with Unreal is the lack of embedded 2D physics on all platforms except Windows.

Most likely, for most 2D-projects it is more reasonable to take some other engine instead of Unreal. But it always depends on the project itself, on your desires, goals, needs.

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


All Articles