I’ve been dabbling on and off (more off than on) with some Unity development since Guildford Game Jam.

I wanted to do a simple thing and as is seemingly the way with modern development platforms, there was more than one way to tackle it.

I’m working on little game that connects a desktop/VR experience to multiple web users. As part of this I wanted to send a periodic update to the web users from the main game via a SignalR websockets connection.

Every Unity script derives from MonoBehaviour which implements a Start() and an Update() method. Start is called once when (surprisingly) the script first starts and Update is a function that is called every frame and is generally where you would implement any ongoing game behaviour. By being called every frame, the script will be called an unknown amount of times each second (60+ being the ideal target by modern standards).

I want to send a regular update informing my web clients of the state of the game but calling WebSockets this many times per second isn’t ideal and would generate a lot of network traffic that isn’t really necessary. A couple of times per second or so would suffice.

So began the following Unity cat-skinning journey:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ExampleClass : MonoBehaviour {
 
   float sendEvery = 0.5f;
   float sendTimer = 0.5f;
 
    void Update() {
        sendTimer -= Time.deltaTime;
        if (sendEvery <= 0)
        {
            DoNetworkCall();
            sendTimer = sendEvery;
        }
    }
 
    void DoNetworkCall();
    {
        // do the networking stuff
        ...
    }
}

This did the job but does involve work on every frame, plus is quite verbose. I later discovered a more Unity-native approach as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ExampleClass : MonoBehaviour {
 
    void Start() {
        StartCoroutine(DoNetworkCall());
    }
 
    IEnumerator DoNetworkCall();
    {
        while (true)
        {
            // do the networking stuff
            ...
            yield return new WaitForSeconds(0.5f);   
        }
    }
}

A Coroutine is a funtion that offers the ability to partially execute and then return, continuing from where it left off on its next invocation. Particularly useful in the context of Unity and as we can see, a much tidier approach. It’s potentially doing something very similar under the hood but is more succinct than the first approach.

Lastly, I discovered another function of the MonoBehaviour that simplifies things even further called InvokeRepeating :

1
2
3
4
5
6
7
8
9
10
11
12
public class ExampleClass : MonoBehaviour {
 
    void Start() {
        InvokeRepeating("DoNetworkCall", 0.5f, 0.5f);
    }
 
    void DoNetworkCall();
    {
            // do the networking stuff
            ...
    }
}

This last version calls the method repeatedly at the specified interval. The fact the method name is passed as a string would suggest this is using reflection and so I would imagine has a higher overhead. But it’s probably the most readable and is the least code (less code = less bugs) so until performance is an issue (avoid premature optimisation), this seems like the best approach to me.

I’m still very new to Unity so if I’ve missed something please let me know.

#adventblogging post 18 of 24 see the rest.