Hiding Unity Library Internals

Tags: , , ,

When writing code for a library, there is invariably some of it you want to hide from the users of the library. You want to keep the public API clean, but Unity makes this tough. Today’s article discusses a strategy for laying out your code so that users of the library aren’t burdened by classes, functions, and properties that they don’t need to know about. Read on to see how!

Read the rest of this article »

No Comments

Adding the const Keyword to C#

Tags: ,

Today’s article is not about the const keyword that C# already has. It’s about the const keyword that C++ has and how we can approximate it in C# to make our code safer. It’s a really powerful tool that’s often the default for C++ programmers, but we can take advantage of a similar strategy in C#. Read on to learn how!

Read the rest of this article »

3 Comments

The Global Variables Everybody Uses

Every programmer has heard that global variables are bad practice and should be avoided in favor of other techniques. Yet you’d be surprised how often global and pseudo-global variables are used. Today’s article reveals some of these usages and present some alternative ways to structure your code so it’s easier to read, write, and maintain. Read on to learn how!

Read the rest of this article »

4 Comments

Iterating Multiple Lists In Order: Part 2

Tags: , ,

Last week I presented a problem: how do you iterate over multiple lists of multiple types in the order of some common field? For example, how would you iterate over a list of Player and a list of Enemy by both of their Health fields? In that article I showed two solutions to iterate over two lists in this way. What I didn’t show were any solutions to handle more than two lists. What if you needed to also iterate over a list of NPC? Today’s article discusses how to tackle this problem and ends up with a handy utility class that you can use for your own types no matter how many lists you have. Read on to see how!

Read the rest of this article »

No Comments

Iterating Multiple Lists In Order

Suppose you have two sorted lists, one holding A elements and another holding B. Both types have a common field and you want to iterate over both lists according to that common field. For example, if you have [1, 3, 5] and [2, 4, 6] then you want to get the A with 1 then the B with 2 then the A with 3 and so on. How do you do this at all? How do you do it efficiently? Read on for my answers to the puzzle!

Read the rest of this article »

No Comments

An Interruptible YieldInstruction

Tags: , , ,

Coroutines are a fundamental building block of Unity scripting. In 5.3, we got a new class to make them more powerful: CustomYieldInstruction. Today we’ll look at it and see if we can make an arbitrarily-interruptible YieldInstruction so our coroutines can abort the things they yield. Read on to see how and to compare against the old 5.2 way!

Read the rest of this article »

No Comments

LINQ Extensions: Part 1

Tags: , ,

One of C#’s most unique features is its SQL-style LINQ syntax. It’s a powerful and expressive way to treat data structures like a database and perform all kinds of actions on them. LINQ can also be used without the SQL-style syntax via various extension methods of IEnumerable<T> defined in the System.Linq namespace. Due to C#’s extension method feature, we’re free to add on our own LINQ-style functions to extend its power. Today’s article introduces some extension methods to do just that. Read on for the source code and power up your LINQ!

Read the rest of this article »

No Comments

If-Else is Really Expensive

Tags: , , ,

Sometimes a tiny amount of code costs a huge amount of performance. This is especially true of built-in language features, which many programmers assume to be extremely cheap if not free. Today we’ll look at if and see just how much performance it can cost your app. Read on to see!

Read the rest of this article »

5 Comments

Better CPU Caching with Structs

Tags: , , , ,

While little utilized, C#’s struct type can come in really handy sometimes. Today’s article shows how to use it to get a lot more mileage out of modern CPUs’ caches to really boost your app’s performance. Read on for some quick tips!

Read the rest of this article »

2 Comments

FastList: A Solution to List’s GC Problems?

Tags: ,

As we know, foreach loops create garbage when used with a List<T>. This happens the first time you iterate over one and it happens every time thereafter. A comment on that article shared a link to a class called FastList that was written expressly to solve the GC issue. Does it? How does its performance compare to plain old List? Today’s article puts it to the test to find out!

Read the rest of this article »

5 Comments