2014-09-02 134 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();