When you want to limit the rate in which your electronics receive messages, then instead of trying to solve this indirectly by limiting the framerate, I would recommend a more direct approach.
I assume that your code currently looks something like this:
class ElectronicsController: MonoBehaviour {
void Update() {
SendUpdateToDevice();
}
}
I would solve this problem by adding two variables to the class: The desired time between updates and the last time the device was updated. Then I would add a check to Update to see how much time elapsed since the last update and if it is less than the current time plus the update interval. I would only do an update to the device when that's the case, and then also update the time of the last update:
class ElectronicsController: MonoBehaviour {
public double updateInterval;
private double lastUpdate;
void Update() {
if (lastUpdate < Time.realtimeSinceStartupAsDouble + updateInterval) {
SendUpdateToDevice();
lastUpdate = Time.realtimeSinceStartupAsDouble;
}
}
}
Note that this solution will result in less updates being sent than the update interval would imply, because each update where the time since the last update does not precisely matches the update interval will result in a couple milliseconds being lost. So it can be more understood as a guaranteed minimum time between updates rather than a consistent update interval.
I know nothing about your particular use-case, so I don't know how important it is for you to have a consistent update rate for your electronics. But if you need more consistency, then I would recommend this slightly more complicated code instead.
class ElectronicsController: MonoBehaviour {
public double updateInterval;
private double lastUpdate;
void Start() {
lastUpdate = Time.realtimeSinceStartupAsDouble
}
void Update() {
bool needUpdate = false;
while (lastUpdate < Time.realtimeSinceStartupAsDouble + updateInterval) {
lastUpdate += updateInterval;
needUpdate = true;
}
if (needUpdate) {
SendUpdateToDevice();
}
}
}
You might notice that it does not set the last update to a new value but adds the interval to it instead. This results in more consistent time-keeping because excess time carries over to the next interval. The code will only skip updates if the game gets too slow to provide the desired update interval (this is the purpose of that while
loop).
But also note that it might happen occasionally with this code that there are two updates which are in very short succession. This would happen if the new lastUpdate
time is very close to the next update. You did not tell us if your electronics have some kind of command buffer which might overflow if it receives too many messages over time or if two fast messages are already a problem.
vSyncCount
is not 0,targetFramerate
is ignored: docs.unity3d.com/ScriptReference/… "If this setting is set to a value other than 'Don't Sync' (0), the value of Application.targetFrameRate will be ignored." \$\endgroup\$