Unity’s coroutine support allows you to easily create pseudo-threads and write synchronous-looking code that doesn’t block the rest of the app. They can be very handy for a variety of tasks. Before using them, we should understand the performance cost. Today’s article takes a look at the cost of starting a coroutine as well as the cost of running it. Just how expensive are they? Read on to find out!
Unity Reflection is Really Slow
Reflection allows you to introspect your code at runtime. You can do very dynamic things like call functions by their name as a string. As such, it’s a really powerful tool when you code needs to be more flexible. Unfortunately, it’s slow. Really slow. Today’s article puts it up against regular, non-reflection code to show the difference in speed. It’ll also walk you through reflection in C# in case you’ve never used it before. Read on to learn more about reflection in Unity!
C# Performance: Properties vs. Fields vs. Locals
C# has properties similar to AS3’s get
and set
functions. These functions can even be auto-generated for you, which is very convenient. However, the auto-generated versions don’t expose the so-called “backing field” that the property gets and sets. This brings up a question: is there a performance penalty to using an auto-generated property rather than manually implementing the property so we can directly access the backing field? The get
and set
blocks are like functions, so are we paying function call overhead for them every time we access our own private pseudo-variables? Finally, could we do even better by skipping fields altogether and working on local variables instead? Today’s article puts all three approaches to the test by analyzing the bytecode that’s generated and the performance within a Unity test environment. Read on to see which way is fastest!
Unity Performance: Editor vs. Standalone
Unity apps are usually developed within the Unity Editor and then published to standalone apps for iOS, Android, Windows, Mac, and so forth. We’d therefore like to think that what we’re seeing in the Editor is very close to what we’ll see once we publish the app. In many cases, this is true. However, when it comes to performance this is sometimes a bad assumption to make.
How To Use C# Events In Unity DLLs
DLLs—like SWCs in Flash—are an extremely handy way to build your code into reusable modules. Unfortunately, Unity has some quirks that can lead to crashes on iOS and other environments that don’t support JIT compilation. The biggest problem that crops up is when you try to use C# events in a DLL. Today’s article investigates why and where the problem occurs and presents a simple solution to work around the problem. Read on to learn how to safely use C# events in Unity DLLs!
Unity Script Performance Testing
Today’s article is the first to test Unity script performance speed. It establishes a way to set up and test C# scripts in Unity whether you have access to Pro or not. As a first example, I was reminded by the news this week that AddComponent(string)
is being removed in Unity 5.0. These alternative versions of AddComponent
and GetComponent
aren’t something I normally use, but the news got me thinking of their performance compared to the generic-typed versions: GetComponent<ComponentType>()
. The docs say to avoid the versions taking a string
, but how bad could the performance really be? Today’s article puts the two versions to the test to find out just that!
Unity Editor Integration with Pure Code
One apparent downside to the “pure code” approach to Unity app code design is that it makes less use of the Unity Editor. Because it only uses one main MonoBehaviour
, there aren’t a lot of MonoBehaviour
classes that can be modified by the Inspector panel- a mainstay in Unity design and debugging. Today’s article introduces another kind of auxiliary MonoBehaviour
to work around this issue enabling you to use the “pure code” approach without sacrificing the Inspector panel.
Capturing and Forwarding Unity Events
As mentioned in last week’s article on the “pure code” approach to Unity code design, capturing events can be problematic. I gave an example of how this could be overcome, but didn’t flesh it out to cover the sixty events that a MonoBehaviour
can receive. Today’s article includes the source code for a class that does just that. It should prove useful to anyone interested in exploring “pure code” design.
A Pure Code Approach to Unity App Code Design
There are many ways to design your Unity app from a coding perspective. Today’s article talks about just one path you might take. Like any path, it’ll have its own advantages and disadvantages. The choice of app code design will make a huge impact, so it’s good to think about it before you start down any path. Perhaps this is the right one for you…
Unity App Structure from a Flash Perspective
Unity apps are structured very strangely. They’re a lot different than Flash or standalone apps and definitely take some time to get used to. Today’s article is an introduction to how a Unity app’s code is structured from the perspective of a Flash developer. It should give a basic understanding of where your code goes and how the basic architecture of a Unity app looks.