PlayerPrefs performance

I’m having some performance problems with my game across some phones. I develop on Nexus One but played my game on a buddy’s Galaxy S and the performance was surprisingly much worse than I get.

I have a sneaking suspicion that it’s related to me writing to PlayerPrefs quite frequently instead of storing values locally and writing them when the game is finished.

I’ve read a couple threads that said they are cached on iOS. I’m wondering if that could be different on Android?

Back on Unity 3.2 when I was trying to track down a problem I profiled our game with traceview. Traceview doesn’t show much as it is really a java profiler. However you could see the PlayerPrefs calls going straight out to the java layer, there was no caching done on read or write.

I imagine you’d have to do a LOT of calls before it became a noticeable problem though.

Based on that feedback I wrote a CachedPlayerPrefs to do a quick find/replace of all my PlayerPref Set and Gets. The hitching seems to have disappeared.

Probably not something too many people will run into as I knew I was being lazy as I did it.

Hope this helps someone, but don’t ever write to PlayerPrefs during gameplay on Android (iOS seems fine), but probably something you should avoid in any situation.

1 Like

Hi Josh,

We had similar problems with PlayerPrefs on Android (and even on iOS with a huge number of key/value pairs), so we wrote our own implementation. I shared it on our blog, so you can download it for free (it’s public domain).

With our implementation, you can easily save hundreds of PlayerPrefs key/values in a split second. You’ll only have to add two lines of code to make it work if you’re already using the built-in PlayerPrefs class.

This is the blog post I’m talking about: