• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jonttu · Oct 14, 2014 at 05:51 PM · playerprefshow to

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Oct 14, 2014 at 05:53 PM 0
Share

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

avatar image Jonttu · Oct 14, 2014 at 07:00 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jonttu · Oct 14, 2014 at 06:19 PM 0
Share

I use javascript and it doesnt support Events

avatar image Landern · Oct 16, 2014 at 02:43 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jonttu · Oct 14, 2014 at 06:27 PM 0
Share

This isn't related to my question in any way

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges