0

I am building a Unity game targetted for Windows Store 8.1 and I need to save game progress in isolated storage. But using 'await' operator in accessing the local storage gives me this error

'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Storage.StorageFile>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

Is there a way i can use 'await' operator somehow?

I figured if I implement my Save method in the solution generated after exporting the project to Windows 8.1 it would work fine. Trouble is I am not sure how to access the variables and methods(required in saving the game) of the unity script in the main solution.

Can anyone help with any of these approaches or if there is a better one?

EDIT: Here is the code I am using:

public async void save_game()
{
    StorageFile destiFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Merged.png", CreationCollisionOption.ReplaceExisting);
    using (IRandomAccessStream stream = await destiFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        //you can manipulate this stream object here
    }
}
Tehreem
  • 939
  • 2
  • 14
  • 31

3 Answers3

2

Is it necessary to use isolated storage. easiest way to save game in unity is using playerprefs. it works on any platform.

PlayerPrefs.SetString("Player Name", "Foobar");

  • 1
    `PlayerPrefs` doesn't come across as it was meant to be secure. It keeps it's data in different places on each platform, but for example, on Windows, it uses unencrypted registry values. Naturally keeping data from a user who is looking for it isn't really feasible on the user's own device, but `PlayerPrefs` doesn't attempt to hide data from _anything_ on the device (including other applications). It's better for things like in-game settings for like sound and visuals. Stuff anyone can look at without issue. And definitely don't store user account info in there (e.g. user passwords). – Selali Adobor Sep 20 '14 at 21:01
  • 1
    why don't you implement a simple encryption method inside the game. that gives you some level of security. – Thilina Premasiri Sep 23 '14 at 04:51
1

Unity's version of Mono/.NET doesn't support async/await, which is why you get the first error.

Others have figured out how to "trick" Unity into letting you indirectly call async functions. One such method is listed on UnityAnswers

Selali Adobor
  • 2,060
  • 18
  • 30
0

You could use something like this.

// this is for loading
    BinaryFormatter binFormatter = new BinaryFormatter();
                string filename = Application.persistentDataPath + "/savedgame.dat";
                FileStream file = File.Open(filename, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
                GameData data = (GameData)binFormatter.Deserialize(file) ;
                file.Close();

where the GameData class is [Serializable]. and after you Deserialize it you can load its members into ingame variables.

//And this is for saving
BinaryFormatter binFormatter = new BinaryFormatter();
        string filename = Application.persistentDataPath + "/savedgame.dat";
        FileStream file = new FileStream(filename, FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
        GameData data = new GameData();

// here it will be something like data.health = player.health file.Close();

Uri Popov
  • 2,127
  • 2
  • 25
  • 43