PlayerPrefs LAG issue after showing keyboard based controls

Strangest behaviour. It seems that if at any point in the game, if a keyboard or dialog prompt is displayed to the user, the writes to PlayerPrefs will lag for the rest of the duration of the game.

We noticed it in our previous game Stickman Skater during development and recently have seen a youtube user where the problem is a HUGE issue. Basically when the skater passes a checkpoint, we save about 8 values to PlayerPrefs… If no keyboard or popup dialog had been shown prior to that point in the game, the checkpoint save would fly past without any pause, however if at any point the keyboard / popup had been shown in any other scene in the game, the save to the PlayerPrefs takes forever causing a 1+ second pause at each checkpoint not to mention when the level is finished and we persist a dozen more values to PlayerPrefs the user just sits there waiting for several seconds.

See 00:02:56 In the following video for a user experiencing this problem… In this case the lag is being caused by a gamcenter login prompt that was displayed on the main menu scene near the start of the video.

http://www.youtube.com/watch?v=hvxMsRxLN30

We were hopeful that our new game would no longer suffer this issue due to faster devices / more recent updates to Unity etc but after just purchasing an iPod touch 4G running os 4.2 the PlayerPrefs lag is back with a vengence. Not quite as long as it is in this video but still in the context of the new game, a very noticeable lag.

It is worth noting that our iPhone 4G running os 4.1 does not suffer the issue however our iPhone 4G running os 4.2 does. I can confirm it also happens on iPhone 3GS running os 3.1 and iPhone 3G running any OS.

Anyone have any idea what Unity might be doing when it shows a keyboard in a previous scene that would cause PlayerPref writes to slow down so much?

maybe its constantly writing to it? instead of just when they click enter.

Not quite sure what you mean by ‘constantly writing to it’ but the negative behaviour seems to go deeper than just slowing down the PlayerPrefs write speed… We have now noticed that when we display a popup message dialog in-game that the FPS drops from a constant 60fps to 30fps as soon as the dialog is closed and remains running at that framerate until a new scene is loaded / reloaded. This is slightly different to the nature of the PlayerPrefs write speed lag as that is not scene dependant and cannot be rectified by reloading a scene.

FYI we got around the PlayerPrefs lag issue by means of a bandaid solution using a 3rd party Prefs saving utility and completely bypassing the Unity one. We still do not know why the slow-down is occurring but at least we can get around it for now.

My gut feeling says that Unity is doing something very strange / unorthodox with its keyboard View that is not being destroyed for the remainder of the app life, and whatever this persistant state is doing is causing all kinds of App slowdowns including PlayerPrefs writes and overall performance / fps decreases. For some reason this ‘state’ is affecting every other kind of UI based popup including dialog boxes / prompts etc.

Thank you for posting this—we’ve been experiencing the same bug in our current title and I’ve spent days trying to track it down. The most baffling thing was that the ‘buggy state’ is completely resolved upon suspension of and resuming the application.

Did you roll your own PlayerPrefs solution, and if not, what third party utility did you use to solve the issue?

Just a quick update, found an excellent and simple plugin at Writing PlayerPrefs, Fast - PreviewLabs which hands off PlayerPrefs writing to a hashTable - much, much quicker!

Make sure to substitute OnApplicationQuit with OnApplicationPause for their PlayerPrefs.Flush() call to save the preferences to device.

Hi edsin,
Nice to see you’re using our PlayerPrefs class :slight_smile: I’m glad that some people are making good use of it.

Only calling PlayerPrefs.Flush() in OnApplicationPause may not be enough, as it isn’t always called.

In the following forum post, Dreamora gave a nice summary of when OnApplicationPause is called exactly:
http://forum.unity3d.com/threads/7820-OnApplicationPause-()?p=624076&viewfull=1#post624076