- Home /
How to convert iOS game to unity and use the same load/save highscore files?
I'm converting iOS games from xcode c++ to unity c#. This is how I load and save my scores in xcode :-
void Load()
{
NSUserDefaults *m_pPrefs = [NSUserDefaults standardUserDefaults];
int nScore = 0;
char szName = "Score";
NSString *pName = [[NSString alloc] initWithUTF8String:szName];
nScore = [m_pPrefs integerForKey:pName];
[pName release];
}
How do I convert this code to unity c#, making sure all previous xcode versions of the games don't lose their scores when updating to the new unity versions?
Answer by SkaredCreations · Dec 12, 2014 at 11:41 AM
If I'm not wrong, PlayerPrefs on Unity iOS is a wrapper around NSUserDefaults, so you should be able to use this in Unity:
// The second parameter is the default value, if "Score" does not exist
int nScore = PlayerPrefs.GetInt("Score", 0);
Just try it on your device and see if it's true.
Hi. Thanks for the help, but I've got a similar problem with the Android versions. This time there is a filename parameter. Is it possible the convert the following code to Unity C# and still load the old java version settings?
public static void LoadData()
{
SharedPreferences settings = Context.getSharedPreferences("filename", 0);
m_bSounds = settings.getBoolean("Sounds", true);
m_nCoins = settings.getInt("Coins", 0);
}
Thanks SkaredCreations
Android Java has a filename as a parameter of their getSharedPreferences function. I cannot find a way to tell unity to get the SharedPreferences from the original file used in a previous Java version of the app?
I don't know much about Android, anyway the current question is about iOS so you should accept my answer if it was successful to you and eventually post another question for the Android part.
Answer by KNDL · Dec 14, 2014 at 03:02 PM
As SkaredCreations answered, for SharedPreferences use PlayerPrefs. This works for all platforms including iOS and android.
Your answer
![](https://koobas.hobune.stream/wayback/20220613173626im_/https://answers.unity.com/themes/thub/images/avi.jpg)