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 A game doesn't typically "autosave". It may be able to "record" (and playback) an entire game; but "saving" is typically a decision made by a user; not the app. (Unless you're talking a text processor like Word) – Gerry Schmitz Commented Mar 12 at 18:25
  • Framerate should not effect these functions unless you have physically messed with time. – BugFinder Commented Mar 12 at 19:36
  • InvokeRepeating depends on the time scale while WaitForSecondsRealtime doesn't .. no that even the coroutine will come to a hold afaik if the timescale is set to 0 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:03
  • I don't know if two InvokeRepeating timers can drift apart? Your coroutine at least is more explicit about the timing between these tasks. But what if saving() takes more than a second to execute? – Jeremy Lakeman Commented Mar 13 at 4:43
Add a comment  | 

1 Answer 1

Reset to default 1

Never 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.

本文标签: