Top Ad unit 728 × 90

5 Tips every Unity developer should know

5 Tips every Unity developer should know

5 tips every unity developer should know


Whether you are new to developing with Unity or you already have experience using the tool, it is always good to know new tips to improve our work. After searching many websites, asking in forums and from own experience, I have compiled 5 Tips every Unity developer should know (and use).

Unity error searching

If you are trying to find a way to fix an error message that you have received in Unity, you can easily find documentation about that particular error. Using Google, type "site: unity3d.com" followed by your error message. This will look for the error through the Unity site and its resources, including forums, tutorials, API documentation and so on.

Garbage collection

If you have ever encountered the problem of a steady drop in your frame rate (which is noticeable for mobile development), the culprit is usually related to garbage collection (GC). GC tries to free memory from objects that are not being used by the game. Here are some of the best common that can help you with this:

GameObject instantiate and destroy

If you make frequent calls to instantiate and destroy during the execution of your game or application, this can greatly affect GC.
Try to create a grouping of your objects at the beginning of the game and then simply activate or deactivate them according to the needs or not, instead of destroying them.

GetComponents

It is often used as a system for your game objects to interact with each other. A collection of components is generated and returned every time GetComponents is called, and once it is no longer used, GC is called. Make sure that you do not use this feature abusively, only when you really need it. You could even develop another type of architecture that supports the interaction of your game objects and that does not depend on GetComponents.

String concatenation

This is very common in C # developers. When you concatenate strings ("Hello" + "World") you are creating a new object of type string. If you do this in each frame, you are adding a good amount of (unnecessary) work to GC when you could be using cache or concatenating without +.


Profiler

In the past, only Unity Pro users could use this tool, but now even free users can use it, so it's good to talk a little about it. The profiles is a really valuable tool that allows you to detect memory leaks and garbage collection issues before players do. You can run the profiler remotely with Android, iOS, and browsers to test the game on the current platform instead of the editor. Also when testing remotely, the profiler does not affect the performance as it does run in the editor of the same machine, giving you more accurate information. Another good feature of profiles is the ability to create code blocks that profile your scripts. You can place the start and finish profiler calls anywhere in your script and really start to dig deep into your game's or individual algorithm's performance.

Interfacing behavior

The more experienced you become with Unity and design principles, you eventually come to an important conclusion: The more generic a script is, the more it can be reused, having less duplicate code. And less duplicate code and maintenance makes it faster to publish a new game. And that's a good thing!

If you develop in C #, once you learn about interfaces and abstract classes, your scripts become even more generic. I would go further and say that with interfaces, you can achieve the most generic design of each part of your game.

Imagine having a player and an enemy game object in Unity each with their own scripts: a player script, and an enemy script. When you have to program for example that the character dies, the chances of you using duplicate code are very high. At this point, you could create a separate death script with the duplicate code removed from both characters. This would be an improvement, but now you have created a triangular dependency between these three scripts. Instead, we could create an IKillable or IDeath interface. This would allow us to easily identify when the event occurs that the character dies. And after that, we would just create an implementation of PlayerDeath and EnemyDeatch.

But do not go crazy with this either. When some developers start using interfaces, they tend to use them for everything. This can cause the same maintenance problems that you were trying to fix, or simply a waste of time. There are certain rules and situations in which interfaces should be used and others not.


Test-driven development

Once you start using this (which may sound weird), the benefits it offers you are incredible. It all starts with a test that defines the expected behavior of a function. For example, let's say your character needs to spawn, in a specific position when they die. You write a test that spawns the player, kills him and checks if he has appeared where he should after dying. The key is that the code where it should appear is not included in the test. Just add enough code to make your game compile, which can perfectly well be creating an empty spawn class. Once the test fails (since the spawn class is empty), you can start working on the spawn function until you pass the test. Once you pass the test, you can continue to the next function.

TDD can be much more elaborate, but the benefits are clearly good. Imagine running dozens of tests all with green light, indicating that your game is working as expected. If you are implementing this integration, you can have it automatically synchronized with your source control, which automatically compiles, tests and publishes the update automatically. Your team can even receive emails when the game passes or fails tests.

Applying these tips when developing in Unity can save you a lot of time and frustration. Do you have any method to make your work in Unity easier? Do you have any tips or advice you would like to share?

5 Tips every Unity developer should know Reviewed by Ondade Dev Academy on 2:09:00 PM Rating: 5

No comments:

All Rights Reserved by Ondade © 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.