How Closures Work

Tags: , , ,

C#’s support for closures includes lambdas ((a, b) => a+b) and delegates (delegate(int a, int b){return a+b;}). They’re extremely handy tools that many developers use on a daily basis. For example, it’s really convenient when using List.Find to pass in a lambda like item => item.Id == IdToFind. They also make great callbacks for asynchronous operations. However you’re using them, understanding how they work behind the scenes will help you understand how they behave and give you insight when optimizing your code. Today’s article delves into the topic to gain just this understanding, so read on to learn some more about closures!

Read the rest of this article »

No Comments

DateTime Performance

Tags: , ,

Unity’s Time class is an easy way to get the relative time. You can find the time since the app started with Time.time or the time between frames with Time.deltaTime. But what if you want to know the absolute time? You may need to display a clock to the user, send a timestamp over a network, or record when a game was saved. This is where System.DateTime comes in. It’s powerful and offers so much functionality that it’s natural to worry about about how slow it’ll be. So today’s article puts it to the test to find out how much time is being spent in operations like DateTime.Now which gets the current date and time. Is it quick enough that you shouldn’t worry? Read on to find out.

Read the rest of this article »

No Comments

A Finite State Machine (FSM) for Unity

Tags: ,

Today’s article introduces a small but capable and extensible finite state machine (FSM) that you can use to organize your Unity applications’ code. FSMs are commonly used to represent the different modes the app can be in—main menu, world map, gameplay, game over—and how the app transitions from one state to another. The FSM in today’s article follows the pure code methodology by not tying the code to game objects, MonoBehaviours, or scenes. So read on to learn about and make use of this “pure code” FSM for Unity!

Read the rest of this article »

4 Comments

Getting the Result of an Iterator

Tags: , , ,

Iterators (functions that yield) are great for representing asynchronous processes such as web calls, updating state over time, and multi-step operations. One unfortunate downside is that it’s tough to return a result from them. Today’s article explores several workarounds to this problem before ultimately arriving at a clean, easy-to-use solution to the problem. Read on to learn how to make iterators more useful!

Read the rest of this article »

No Comments

A Model-View-Controller (MVC) Pattern for Unity

Tags: , , , , ,

What do you do if you want to use the Model-View-Controller (MVC) design pattern in your Unity app but you don’t want to use a framework like StrangeIoC? With a little thinking about the problem I think I’ve come up with a simple yet effective pattern to follow that doesn’t require you to use any framework. In today’s article I’ll talk about each part, how the parts fit together, and how you can use MVC to cleanly organize your “pure code” app. Whether you’re an MVC newbie or just want to see a new take on MVC in Unity, you’re sure to learn something today!

Read the rest of this article »

4 Comments

StrangeIoC Reflection Performance

Tags: , , ,

StrangeIoC is a library that can help you build your Unity app with a “pure code” approach. Today’s article addresses one common concern with using StrangeIoC- it uses a lot of reflection. As we know, that’s really slow in Unity. StrangeIoC tries to work around it by letting you control when the reflection takes place so you can put it on a loading screen or some other convenient place. Today’s article finds out just how slow the reflection is to determine if this is really a valid reason to not use StrangeIoC (or other dependency injection frameworks). Read on to find out!

Read the rest of this article »

No Comments

Linked Lists Are Slow

Tags: , , ,

Contrary to what you may have learned in a data structures class, linked lists are virtually always slower than just using arrays. The same goes for array wrapper classes like List. Today’s article discusses why this is the case and tests it out with a C# Unity app to make sure that the real world validates the theory.

Read the rest of this article »

2 Comments

Optimizing Arrays and Lists

Tags: , , , ,

Last week’s article compared the performance of arrays with List<T> and found List lacking. This week we’ll optimize both List and array to maximize performance regardless of which you choose to use.

Read the rest of this article »

1 Comment

Array vs. List Performance

Tags: , ,

System.Collections.List<T> is used everywhere in C# code. Except for very special cases, it’s the replacement for arrays, linked lists, queues, and most other one-dimensional data structures. This is because it has all kinds of extra functionality, including the ability to grow in size on-demand. Today’s article wonders about how much performance is lost to gain this convenience and tests the List<T> class against the lowly C# array: T[]. How much performance are you giving up with List and why is that happening? Read on to find out!

Read the rest of this article »

6 Comments

Guide to Using DLLs in Unity

Tags: , ,

Today’s article guides you through the necessary steps to create, use, and debug a .NET DLL in Unity. These can help you modularize your code into libraries that can be imported as a single file by the users of your library. They’re especially useful when utilizing the “pure code” approach to code design as you can easily break up a Unity app’s monolithic structure into reusable components. So read on to learn how to use DLLs in Unity!

Read the rest of this article »

4 Comments