admin管理员组文章数量:1391987
For my game need a function that saves the game every x seconds. Problem is, while it works perfectly on windows in the timeframe i want it to be, when i try to play the game on my phone it's unbearably slow - the game itself runs fine without lag but the repeating code saves the game so sporadically ive only anecdotally witnessed it.
I've tried invoke repeating like this
Public void Start ()
{
InvokeRepeating("savegame", 5f, 5f);
InvokeRepeating("deactive", 6f, 5f);
//deactive hides a text field 1 second after saving
}
I've tried a coroutine like this
public void Start()
{
StartCoroutine(savegame());
}
IEnumerator savegame ()
{
yield return new WaitForSecondsRealtime(5);
saving();
yield return new WaitForSecondsRealtime(1);
deactive();
StartCoroutine(savegame());
}
All of these work exactly how i intended them to (ie, executing every 5 seconds) when im in the unity editor on my windows pc and click play, but on my phone, it appears to count the seconds much more slowly. It seems to be an issue of the framerate on my phone being lower, how can i make the seconds not depend on framerate? i want it to count seconds in real life time, the way the phone's internal clock counts them.
any ideas??
For my game need a function that saves the game every x seconds. Problem is, while it works perfectly on windows in the timeframe i want it to be, when i try to play the game on my phone it's unbearably slow - the game itself runs fine without lag but the repeating code saves the game so sporadically ive only anecdotally witnessed it.
I've tried invoke repeating like this
Public void Start ()
{
InvokeRepeating("savegame", 5f, 5f);
InvokeRepeating("deactive", 6f, 5f);
//deactive hides a text field 1 second after saving
}
I've tried a coroutine like this
public void Start()
{
StartCoroutine(savegame());
}
IEnumerator savegame ()
{
yield return new WaitForSecondsRealtime(5);
saving();
yield return new WaitForSecondsRealtime(1);
deactive();
StartCoroutine(savegame());
}
All of these work exactly how i intended them to (ie, executing every 5 seconds) when im in the unity editor on my windows pc and click play, but on my phone, it appears to count the seconds much more slowly. It seems to be an issue of the framerate on my phone being lower, how can i make the seconds not depend on framerate? i want it to count seconds in real life time, the way the phone's internal clock counts them.
any ideas??
Share Improve this question asked Mar 12 at 17:42 mrsjohnsonmrsjohnson 1 4 |1 Answer
Reset to default 1Never had an issue with time being inconsistent across the platforms. It's probably an issue within your save methods themselves.
If these methods perform a heavy operation, such as writing to a file, then they might take more time than the WaitForSeconds you're doing. You see, both InvokeRepeating and IEnumerator allow to do some "parallel" work, but it's not truly parallel. It's still happening on the main thread, so it might freeze the thread for, let's say, 200ms, and your next invocation will be 200ms later.
If you really need to do such heavy work in the background constantly, you might want to consider creating a new real thread - that will do the saving - without taking up your main thread upon which you run the game.
You might also want to attach a remote profiler session to your phone and investigate what it is that eats your framerate.
Additionally, running the same coroutine within the same coroutine like you do is a very ugly line of code :P. Just put the content of the coroutine inside the while(true) loop, and you'll have the same effect.
本文标签:
版权声明:本文标题:(Unity, C#) Getting function to execute every x seconds consistently across platforms - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744736136a2622339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
InvokeRepeating
depends on the time scale whileWaitForSecondsRealtime
doesn't .. no that even the coroutine will come to a hold afaik if the timescale is set to0
though ... but as mentioned they are not affected by frame rates (except of course your frame rate was less than 1 frame every 5 seconds ;) ) – derHugo Commented Mar 12 at 21:03InvokeRepeating
timers can drift apart? Your coroutine at least is more explicit about the timing between these tasks. But what ifsaving()
takes more than a second to execute? – Jeremy Lakeman Commented Mar 13 at 4:43