At first glance an Updater
class seems unnecessary in Unity. All you have to do is inherit from MonoBehaviour
and add an Update
function. But what if you don’t want to inherit from MonoBehaviour
? Enter Updater
, an easy way to still get an update event and cut your dependency on MonoBehaviour
. This works great for code in DLLs, “pure code” projects, or just projects that don’t want to put everything into MonoBehaviour
s. Read on for the source code and how to use it!
Archive for category Unity
One typical piece of advice for dealing with the slowness of Unity’s garbage collector is to periodically force a garbage collection, such as every 30 frames. Even Unity advises this. The idea is that you’ll spread out the garbage collection work across many frames rather than having a big spike that causes your frame rate to stutter. But the question remains- what’s the best rate to force the GC? Today’s article tries out various intervals to see which is best. Read on for the results!
Having just concluded the series on handling errors without using exceptions, now’s a good time to check up on an assertion I made in the first part: exceptions are slow. A good question to ask is “compared to what?” So let’s put them up against good old error codes and the new Either
type I presented in the series. Which is fastest when there is no error? Which is fastest when there is an error? Read on to find out!
Last week’s article introduced the Either
class as an alternative to exceptions that makes it easy for functions to declare their error results in addition to their success results and for callers of those functions to handle both results. Today we’ll go further by linking together multiple functions to handle all the error cases almost transparently. Read on to learn how to make the most out of Either
!
Exceptions are the de facto way to handle errors in C#, but they have problems. Callers don’t know if the function they’re calling will throw an exception at all or which types of exceptions it’ll throw. Exceptions also introduce an alternative control flow that’s often hard for programmers to follow. They make our code slower too, even when never thrown! Today’s article introduces an alternative to exceptions to help solve all of these issues. Read on to learn a new way to handle errors!
Unity programmers have their choice of two kinds of events. We could use the built-in C# event
keyword or Unity’s UnityEvent
classes. Which is faster? Which one creates more garbage? Today’s article finds out!
In today’s article I’ll share a technique that can help you reason about your classes (and structs). The core idea is to move some methods out of the class into helper functions. Doing this can really simplify the class and simplify the functions so they’re much more easily understood by readers (including yourself!), more easily written, and more easily extended. Read on to learn more about this technique!
C and C++ have a great feature call the “union”. It’s like a struct
except it only has one of the fields at a time. C# lacks this feature, but with some trickery it can be added in. Today’s article shows how to do that!
At some point, every project ends up reading or writing to the file system. If you do anything more than storing a single blob of bytes (e.g. JSON text) then you’ll need to be very careful about performance. It’s easy to accidentally write code that takes way longer to read and write than it should and you won’t get any help from the compiler or from Unity. Today’s article reveals some of these traps so you won’t fall into them!
C# has a huge advantage over languages like Java and AS3: the struct
. It allows us to create a value type with multiple members and avoid creating garbage every time we create one with new
. At least that’s the simple version of the story. In reality, it’s really easy to accidentally create garbage with struct
. Today’s article shows how to use struct
so that you don’t create any garbage and reveals some of the traps so you won’t fall into them.