2014-09-02 135 views
0

我正在构建针对Windows Store 8.1的Unity游戏,并且需要将游戏进度保存在独立存储中。但在访问本地存储使用“等待”经营者给我这个错误在独立存储中保存Unity游戏的进度

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

有没有一种方法,我可以用“等待”经营者不知何故?

我想如果我在将项目导出到Windows 8.1后生成的解决方案中实现我的Save方法,它将正常工作。麻烦是我不确定如何访问主解决方案中的统一脚本的变量和方法(在保存游戏时需要)。

任何人都可以帮助任何这些方法,或者如果有更好的方法吗?

编辑: 这里是我使用的代码:

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 
    } 
} 
+0

我不认为你会得到一个答案,除非你发布代码,再现问题 – Mick 2014-09-02 02:44:40

+0

@米克增加了代码 – Tehreem 2014-09-02 03:43:36

回答

1

是否需要使用隔离存储。统一保存游戏的最简单方法是使用playerprefs。它适用于任何平台。

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

+0

我已经读了它不安全,你能告诉我为什么吗? – Tehreem 2014-09-19 16:29:10

+1

'PlayerPrefs'没有遇到,因为它是安全的。它将数据保存在每个平台上的不同位置,但例如,在Windows上,它使用未加密的注册表值。从用户自己的设备上自然保留数据并不是真的可行,但是'PlayerPrefs'不会试图隐藏设备上的_anything_数据(包括其他应用程序)。对于喜欢声音和视觉效果的游戏设置来说,它更好。任何人都可以看到没有问题的东西。绝对不要将用户帐户信息存储在那里(例如用户密码)。 – 2014-09-20 21:01:05

+1

为什么不在游戏中实现简单的加密方法?这为您提供了一定程度的安全性。 – 2014-09-23 04:51:17

1

统一的版本,单声道/ .NET不支持异步/ AWAIT,这就是为什么你得到的第一个错误。

其他人已经想出了如何“欺骗”Unity让你间接调用异步函数。 UnityAnswers

0

你可以使用这样的事情。

// 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(); 

其中GameData类是[Serializable]。并且在反序列化之后,您可以将其成员加载到游戏变量中。

//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(); 

//这里将会像data.health = player.health file.Close();