Redirect Console.Write to Unity’s Console

Tags: , ,

One of the great advantages of programming in Unity is that it uses a (mostly) standard .NET implementation. This means you can find lots of third party code that is written for .NET but not necessarily Unity and still incorporate it into your app. This kind of code typically uses System.Console.Write or System.Console.WriteLine to print to standard output, but Unity doesn’t display it in its Console panel or redirect it to platform-specific logging like Android’s logcat. This article provides a class you can easily integrate into your app to redirect System.Console writes to Unity’s standard Debug logging so it’ll show up like you’d expect.

Read the rest of this article »

No Comments

Unity Coroutine Performance

Tags: , ,

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!

Read the rest of this article »

2 Comments

Unity Reflection is Really Slow

Tags: , , , ,

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!

Read the rest of this article »

2 Comments

C# Performance: Properties vs. Fields vs. Locals

Tags: , , ,

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!

Read the rest of this article »

4 Comments

Unity Performance: Editor vs. Standalone

Tags: ,

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.

Read the rest of this article »

4 Comments

How To Use C# Events In Unity DLLs

Tags: ,

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!

Read the rest of this article »

No Comments

Unity Script Performance Testing

Tags: , , ,

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!

Read the rest of this article »

No Comments

Unity Editor Integration with Pure Code

Tags: , ,

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.

Read the rest of this article »

2 Comments

Capturing and Forwarding Unity Events

Tags: ,

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.

Read the rest of this article »

5 Comments

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…

Read the rest of this article »

11 Comments