こんな感じにPlayerPrefsを使うと簡単にスコア情報を保存出来ます。
var Score : int = 0; function Awake () { Score = PlayerPrefs.GetInt("Score"); } function OnGUI () { if (GUI.Button(Rect(5, 5, 200, 50), "+1")) { Score++; PlayerPrefs.SetInt("Score", Score); } if (GUI.Button(Rect(5, 60, 200, 50), "Data Clear")) { Score = 0; PlayerPrefs.DeleteKey("Score"); } GUI.Label(Rect(5, Screen.height-20, Screen.width-10, 50), "Scroe:"+Score); }
その他に SetFloat や SetString など float や String データを保存可能です。
環境によってファイルの保存場所が異なり、以下の様になります。
- .plistファイルに保存
~/Library/Preferences/unity.[company name].[product name].plist
- レジストリに保存
HKCU\Software\[company name]\[product name]
Web Player(Mac)の場合
- 独自形式?のファイル
~/Library/Preferences/Unity/WebPlayerPrefs
Web Player(Win)の場合
- 独自形式?のファイル
%APPDATA%\Unity\WebPlayerPrefs
Web Playerの場合はファイルサイズに1MBまでの制限有り。
Macスタンドアローンの.plistはこんな感じに出力されます。
ファイルが更新されるのはアプリが終了したタイミングみたいです。それまではオンメモリで処理されるのかな?
関連情報
Unity Script Reference – PlayerPrefs
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html