background preloader

Unity tips

Facebook Twitter

50 Tips for Working with Unity (Best Practices) About these tips (Edit: August 2016.

50 Tips for Working with Unity (Best Practices)

I have revised these tips. You can find the new list here.) These tips are not all applicable to every project. They are based on my experience with projects with small teams from 3 to 20 people.There’s is a price for structure, re-usability, clarity, and so on — team size and project size determine whether that price should be paid.Many tips are a matter of taste (there may be rivalling but equally good techniques for any tip listed here).Some tips may fly in the face of conventional Unity development. Process 1. 2. 3. 4. It makes it unnecessary to re-setup each scene.It makes loading much faster (if most objects are shared between scenes).It makes it easier to merge scenes (even with Unity’s new text-based scenes there is so much data in there that merging is often impractical in any case).It makes it easier to keep track of data across levels. You can still use Unity as a level editor (although you need not).

Packing Lightmaps. Pop Quiz: You have 765,618 lightmaps for a scene and very few of them have power of 2 dimensions, what do you do?

Packing Lightmaps

If your answer was to rescale them and call CreateTexture 765,618 times please slap yourself. If your answer had anything to do with glTexImage2D, you might want to leave now. What you really want to do is smush them all into a couple larger textures, and this text will show you one way of doing it. What we'll do is recursively divide the larger texture into empty and filled regions. We start off with an empty texture and after inserting one lightmap we get this: Here we've split the texture in half by line A then split the upper half by B and inserted the first lightmap to the left of B. Pretty basic. The insert function traverses the tree looking for a place to insert the lightmap. Once the lightmap is in the larger texture, you'll want to go through any meshes that use it and adjust their texture coordinates based on the lightmap's rectangle in the larger texture.

Unity-Technologies / UI / source / UnityEngine.UI / UI / Core / ToggleGroup.cs — Bitbucket. Understanding Automatic Memory Management. Execution Order of Event Functions Platform Dependent Compilation When an object, string or array is created, the memory required to store it is allocated from a central pool called the heap.

Understanding Automatic Memory Management

When the item is no longer in use, the memory it once occupied can be reclaimed and used for something else. In the past, it was typically up to the programmer to allocate and release these blocks of heap memory explicitly with the appropriate function calls. Nowadays, runtime systems like Unity’s Mono engine manage memory for you automatically. Value and Reference Types When a function is called, the values of its parameters are copied to an area of memory reserved for that specific call. Types that are stored directly and copied during parameter passing are called value types.

Allocation and Garbage Collection The memory manager keeps track of areas in the heap that it knows to be unused. Optimization The key detail here is that the new pieces don’t get added to the string in place, one by one. Optimizing Scripts. This section demonstrates how you would go about optimizing the actual scripts and methods your game uses, and it also goes into detail about the reasons why the optimizations work, and why applying them will benefit you in certain situations.

Optimizing Scripts

Profiler is King There is no such thing as a list of boxes to check that will ensure your project runs smoothly. To optimize a slow project, you have to profile to find specific offenders that take up a disproportionate amount of time. Trying to optimize without profiling or without thoroughly understanding the results that the profiler gives is like trying to optimize with a blindfold on. Internal mobile profiler You can use the internal profiler to figure out what kind of process is slowing your game down, be it physics, scripts, or rendering, but you can’t drill down into specific scripts and methods to find the actual offenders.

For more about profiling on mobile devices, see the profiling section. Optimized by Design Object Pooling Implementation.