- Home /
How to detect if something is added to PlayerPrefs?
So I have this code on Update()
if (PlayerPrefs.GetInt("Level") == 0) {
startTime = 14;
endTime = 131;
}
if (PlayerPrefs.GetInt("Level") == 1) {
startTime = 13;
endTime = 130;
}
if (PlayerPrefs.GetInt("Level") == 2) {
startTime = 12;
endTime = 60;
}
It keeps going till level 12 and this really affects performance. It droppes fps from 5000 to 15. How can I detect if 1 is added to PlayerPrefs?
a.) Why do you need to run that in every frame of Update()? It looks like it should be a one-shot level loading code or something.
b.) Call PlayerPrefs.GetInt() once and store the result in a local var. Then test the value of that var rather than retrieving it from PlayerPrefs every time.
c.) $$anonymous$$ake all your subsequent if
conditions into else if
conditions (or, if you'd rather a single switch statement).
On my game you get score for colliding object A to object B. it now runs everytime those objects collide, this didn't do anything. However, changing PlayerPrefs.GetInt() to local var increased the fps 30. It's now playable on my phone, but like my friend's phone it doesn't run even that smoothly
Answer by Landern · Oct 14, 2014 at 05:57 PM
You can't detect when something is added, however, you could create an event, subscribe to that event with an appropriate delegate that is invoked when setting the int for level.
Check out the Event Tutorial, it might have to do with mouse clicking, but you really just need to apply it to your structure, when the level int is set and saved for PlayerPrefs.
You can create the events in C# and subscribe to them in your javascript script.
Or you can give the NotificationCenter script on Unity's wiki a shot: http://wiki.unity3d.com/index.php/NotificationCenter
@alucardj posted about this on the forums and UA, i assume he went the NotificationCenter route.
Answer by dsada · Oct 14, 2014 at 06:00 PM
http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Here you can see where the playerprefs data is stored on the disk. Please note, that the datas will be on the disk if the application terminates successfully or you call PlayerPrefs.Save().
So if your program is running and you didnt call Save function the datas are stored in the memory not on the disk
Your answer
Follow this Question
Related Questions
PlayerPrefs not saving 1 Answer
Highscrore won't work 1 Answer
activating a Camera Using a Toggle Button 1 Answer
How to swap weapons easily? 1 Answer
How to get zombie AI working 3 Answers