Saving and loading an int variable on Android
Hi !
I am currently developing some sort of a CALCULATOR app for 3rd prep study in Egypt.
I will not publish it on any app store. I'll just have it for myself and my friends. Also, I am developing it to feel like I'm really a Programmer (I haven't worked on any game or app yet).
So, I decided to use Unity Ads in the app to generate some revenue (I know it will not be $XXXXXXX but I am not integrating it for the value of money itself, just to have a reason to continue learning programming).
Now, I've programmed the functionality of the CALCULATOR itself and I am ready to integrate Unity Ads (I have Unity 5.2 and I know it's integrated in Unity itself now).
I want to have Energy with each calculation process taking 1 Energy and each completed video ad adding 5 Energy (easy).
I want when the user opens the app for the first time, an int variable gets created with the name of Energy and value of 0. After each calculation process, Energy gets saved. When the user later opens the app Energy gets loaded. (Hard)
What I can't do is when the user first opens the app Energy gets created and when he later opens it Energy gets loaded.
I know that saving and loading ints can be done using UserPrefs but how can I apply it to my case ?
Thanks in Advance
Answer by hexagonius · Sep 12, 2015 at 08:16 PM
PlayerPrefs.GetInt has a two parameter version, where the second is the default if the value hasn't been created. So just running GetInt with a default of 0 will get you the initial 0 and as soon as you write the first value it will return it.