Iterators aren’t magic. We’ve seen the IL2CPP output for them and it’s not complex. It turns out we can just as easily implement our own iterators and gain some nice advantages along the way. Read on to learn how!
Posts Tagged coroutine
Coroutines are great for tasks that are easy to break up into little chunks, but we still need threads for long-running blocking calls. Today’s article shows how you can mix some threads into your coroutines to easily combine these two kinds of asynchronous processes.
Unity code frequently makes use of the coroutine feature of
MonoBehaviour. It can make asynchronous code a lot easier to write, but runs into problems when exceptions are thrown. There’s no avoiding exceptions since they’re built into C# (e.g.
NullReferenceException) but we can cope with them, even when they’re in coroutines. Today’s article introduces a helper function or two that you can drop into your projects to help you handle exceptions thrown from your coroutines. Read on to learn how!
In asynchronous programming we’re constantly dealing with callback functions. Maybe you have to call some function in a third party library that takes a callback function. Regardless, Unity programmers often want to use coroutines for their asynchronous tasks. Today’s article show you how you can use callback-based code from your coroutines, all while being simple and easy to use. Read on to learn how!
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!
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
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
MonoBehaviours. Read on for the source code and how to use it!
Unity’s coroutine support is great. So great that it’s easy to go overboard and end up with too many of them. That could be for any number of reasons. Perhaps the coroutines are using too much memory or have too many files open at once. In any case, you’ll need to find a way to limit how many are running at a single time. Today’s article introduces a solution to the problem that queues coroutines so you never have too many running. Read on to learn about the solution and for the class that implements it.
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!
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!