Fastest Callbacks: Delegate, Event, or Interface?

How fast are C# delegates and events? The best answer is “compared to what?”. You probably use callbacks all the time, but what’s the fastest kind of callback? C# delegates and events are nice, built-in features of C#, but you could also implement your own callback interface. Would that gain you any speed? Read on for the performance test and results!

Read the rest of this article »

Working Around Iterator Function Limitations

As you use iterator functions (and yield) more and more, you’ll start to run into some limitations in the C# language. For instance, you can’t yield inside a try block that has a catch block. And the foreach loop doesn’t provide a very good way to catch exceptions when looping over an iterator function, either. Today’s article goes into detail to find solutions to these issues and make iterator functions usable in even the trickiest scenarios!

Read the rest of this article »

Coroutine and Iterator Function Tricks

C# supports iterator functions with the yield keyword. Unity uses them to support coroutines. Unfortunately, both are poorly understood by many Unity programmers. Today’s article dives into iterator functions and coroutines to better understand how they work and what they can be used for. Read on to learn how to use iterator functions and coroutines for more effective asynchronous code!

Read the rest of this article »

SafeList: A Class To Eliminate Foreach Errors

Today’s article shows a class that helps clean up your foreach loops when you want to call Add() or Remove() on the List you’re looping over. Normally you’d get an exception, but today’s class works around that problem so your code is less error-prone and easier to read. It also discusses some workarounds you can use even if you don’t use SafeList. Read on to learn how to make your foreach loops less error-prone! UPDATE: SafeList 2.0 is out!

Read the rest of this article »

WebCall: A Class to Make Web Calls Cleaner and Easier

Today’s article is about WebCall, a class to make Unity’s WWW cleaner and easier to use. How could it be cleaner or easier than it already is? By adding C# events! Normally your web calls have lots of clutter around them, your logic gets split across functions, and handling the call is hard when the GameObject or MonoBehaviour get destroyed. WebCall solves all these problems so your web calls are clean, easy, and robust. Read on for the source code and examples!

Read the rest of this article »

String.Format() vs. Concatenation vs. String Builder

What’s the fastest way to build a string in C#? We have several options available to us. string.Format() is a function built right in to the string class., Concatenation ("a" + "b") is a feature of the language itself! The System.Text.StringBuilder class is a built in class with a name that makes it sound like it’s purpose-built for building strings. Today I pit these three against each other to find out just which one you should be using to build strings as quickly as possible.

Read the rest of this article »

IL2CPP Slowdown (Partially Solved)

The new IL2CPP scripting backend in Unity 4.6.2 and 5.0 is supposed to be much faster than the old Mono backend. I ran some benchmarks, but mostly found slowdowns compared to Mono. Today’s article shows the tests I ran, the results I got, and wonders why the IL2CPP version seems so slow. Perhaps one of you, dear readers, knows the reason why. Update: Part of the reason why has been discovered. Read on for updated results.

Read the rest of this article »

Unity Function Performance

Which is the fastest kind of C# function in Unity? There are several to choose from: regular old instance methods, static methods, delegates, and lambdas. Is there any performance reason to choose one over the other? Today’s article answers just these questions by putting each type of function to the test. Read on to see which is fastest and which is slowest!

Read the rest of this article »

Basic LINQ Performance

SQL-style LINQ queries are a concise, readable way of performing various tasks dealing with all kinds of collections. Surely all that convenience comes with a performance cost to it. How bad do you think it is? Today we’ll look at the cost of some basic LINQ queries (Where, Select) versus the equivalent non-LINQ code. We’ll also see how much slower both of them are compared to manually-written, traditional code that does away with all the flexibility. Read on to see the results!

Read the rest of this article »

Redirect Console.Write to Unity’s Console

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 »