Tips and tricks: A handy Unity trick for detecting when scene changes in the Editor

We really needed to have some code run as soon as a new scene was loaded in the editor. We’re making a 3D tile-based game and we calculate most of the data associated with them automatically from the GameObjects layout (such as the navigation links between them and debug display information). This is great as changing anything in the editor recalculates it, so we always know our data is good. However, we need to have that step of “Reclaculate Dynamic Stuff”. We use custom editor components to handle a lot of this, but again they don’t have any nice “scene loaded entry point”. We needed a solution. So here’s the trick. Firstly you need to have an update function that is called all the time the editor is active. This is done by patching into EditorApplication.update. The easiest way to do this is using the very useful InitialiseOnLoad system that Unity provides. Here’s an example: [InitializeOnLoad] public class EditorCallBack { static EditorCallBack () { EditorApplication.update += Update; } static void Update () { } } When Unity starts up or you click play, Unity will hook into the update callback so you get a nice way to run things every frame. […]

Dev blog 0x01 – My favourite trick and random selections

Hello and welcome to what I hope will be the first in many blog posts from me with coding tips and tricks (or trips and ticks, as we often say). Our artist, Anya, has been bugging me to do this for ages, and I was recently told to get on it by the ace Dave Voyles – so here we are! I’ve been working in games for over 20 years now and it’s amazing how many little things you learn and then just assume everyone knows. I’ve shown a few of them, usually with the words “You know this, right?” only to be looked upon as some sort of sage with arcane knowledge brought from the code-gods of old. I am not a sage, I’ve just been around a bit. I think it also helps that I started programming at a very different time when performance was a big thing and so knowing a good, solid, and more importantly quick solution was key. So where to start? Well, how about with my favourite trick of all time. It’s not particularly useful now, but it gives you an idea of where I’m coming from. Back in the 80s, computers didn’t have […]