An opinion base question about implementing timer system.

Maybe not related to Unity.

I planed to implement server connecting with my game using C# System.Timer.

I need to use a lot of timers at the same time, And these timers use about 5 seconds.

Options 1 : Having single timer and insert or queue each tasks.
This case if insert nearer timer may need to cancel the old timer and queue it later.

Options 2 : Having many timer independent to each others.

What will you guys choose if performance are concerned.

I would avoid using System.Timer and instead write my own solution based on MonoBehavior.Update.

The reason for that is that state of System.Timer will not serialize in unity, which will make live rebuild of code more complicated. Implementing this a simple timer-like code is trivial. Also, system.Timer will not correctly function with unity’s time scaling.

Another option is coroutines.

1 Like

Thanks for reply :wink:

Yeah, System.Timer also add complexity for managing thread if it was in Unity.

In this case I’m on server, So there’s no UnityEngine.

In this case I would avoid introducing multithreading needlessly and would implement some sort of queue for timed tasks.So, one timer, or no timer at all.

Of course, it depends on design of your code, and I know nothing about it.

2 Likes