Last active
August 29, 2015 14:20
-
-
Save zentuit/cfd14f738ee9172af4ac to your computer and use it in GitHub Desktop.
GameStorage and GamePlayerPrefs implementations that use Soomla KeyValueStorage to store the key value pairs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Copyright (C) 2012-2014 After Insanity Studios | |
/// | |
/// Licensed under the Apache License, Version 2.0 (the "License"); | |
/// you may not use this file except in compliance with the License. | |
/// You may obtain a copy of the License at | |
/// | |
/// http://www.apache.org/licenses/LICENSE-2.0 | |
/// | |
/// Unless required by applicable law or agreed to in writing, software | |
/// distributed under the License is distributed on an "AS IS" BASIS, | |
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
/// See the License for the specific language governing permissions and | |
/// limitations under the License. | |
using System; | |
public static class GamePlayerPrefs { | |
public static void Save() { | |
//no-op as db saves automatically | |
} | |
public static string GetString(string key, string def = "") { | |
return GameStorage.GetValue(key, def); | |
} | |
public static int GetInt(string key, int def = 0) { | |
return GameStorage.GetValue(key, def); | |
} | |
public static float GetFloat(string key, float def = 0f) { | |
return GameStorage.GetValue(key, def); | |
} | |
public static bool GetBool(string key, bool def = false) { | |
return GameStorage.GetValue(key, def); | |
} | |
public static void SetString(string key, string val) { | |
GameStorage.SetValue(key, val); | |
} | |
public static void SetInt(string key, int val) { | |
GameStorage.SetValue(key, val); | |
} | |
public static void SetFloat(string key, float val) { | |
GameStorage.SetValue(key, val); | |
} | |
public static void SetBool(string key, bool val) { | |
GameStorage.SetValue(key, val); | |
} | |
public static bool HasKey(string key) { | |
string val = GameStorage.GetValue(key, null); | |
return val != null; | |
} | |
public static void DeleteKey(string key) { | |
GameStorage.DeleteKeyValue(key); | |
} | |
public static void DeleteAll() { | |
throw new Exception("DeleteAll is not possible with this version of GamePlayerPrefs"); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Copyright (C) 2012-2014 After Insanity Studios | |
/// | |
/// Licensed under the Apache License, Version 2.0 (the "License"); | |
/// you may not use this file except in compliance with the License. | |
/// You may obtain a copy of the License at | |
/// | |
/// http://www.apache.org/licenses/LICENSE-2.0 | |
/// | |
/// Unless required by applicable law or agreed to in writing, software | |
/// distributed under the License is distributed on an "AS IS" BASIS, | |
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
/// See the License for the specific language governing permissions and | |
/// limitations under the License. | |
using System; | |
using Database = Soomla.KeyValueStorage; | |
public static class GameStorage { | |
private const string GAME_KEY_PREFIX = "game."; | |
public static string GetValue(string key, string def = "") { | |
string result = Database.GetValue(GenerateDBKey(key)); | |
if (string.IsNullOrEmpty(result)) | |
result = def; | |
return result; | |
} | |
public static int GetValue(string key, int def = 0) { | |
return Convert.ToInt32(GetValue(key, Convert.ToString(def))); | |
} | |
public static float GetValue(string key, float def = 0f) { | |
return (float)Convert.ToDouble(GetValue(key, Convert.ToString(def))); | |
} | |
public static bool GetValue(string key, bool def = false) { | |
return Convert.ToBoolean(GetValue(key, Convert.ToString(def))); | |
} | |
public static void SetValue(string key, string val) { | |
Database.SetValue(GenerateDBKey(key), val); | |
} | |
public static void SetValue(string key, int val) { | |
SetValue(key, Convert.ToString(val)); | |
} | |
public static void SetValue(string key, float val) { | |
SetValue(key, Convert.ToString(val)); | |
} | |
public static void SetValue(string key, bool val) { | |
SetValue(key, Convert.ToString(val)); | |
} | |
public static void DeleteKeyValue(string key) { | |
Database.DeleteKeyValue(GenerateDBKey(key)); | |
} | |
private static string GenerateDBKey(string key) { | |
return string.Format("{0}{1}", GAME_KEY_PREFIX, key); | |
} | |
} | |
Forgot the Save() function - which is a no-op but should still be there for completeness
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my personal version of PlayerPrefs I also have methods to save lists/arrays and dictionaries. I haven't added those to GamePlayerPrefs ... yet