Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UnityEngine.PlayerPrefs wrapper for WebGL LocalStorage
var FileIO = {
SaveToLocalStorage : function(key, data) {
localStorage.setItem(Pointer_stringify(key), Pointer_stringify(data));
},
LoadFromLocalStorage : function(key) {
var returnStr = localStorage.getItem(Pointer_stringify(key));
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
RemoveFromLocalStorage : function(key) {
localStorage.removeItem(Pointer_stringify(key));
},
HasKeyInLocalStorage : function(key) {
if (localStorage.getItem(Pointer_stringify(key))) {
return 1;
}
else {
return 0;
}
}
};
mergeInto(LibraryManager.library, FileIO);;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
namespace Jammer {
/// <summary>
/// UnityEngine.PlayerPrefs wrapper for WebGL LocalStorage
/// </summary>
public static class PlayerPrefs {
public static void DeleteKey(string key) {
Log.Debug(string.Format("Jammer.PlayerPrefs.DeleteKey(key: {0})", key));
#if UNITY_WEBGL
RemoveFromLocalStorage(key: key);
#else
UnityEngine.PlayerPrefs.DeleteKey(key: key);
#endif
}
public static bool HasKey(string key) {
Log.Debug(string.Format("Jammer.PlayerPrefs.HasKey(key: {0})", key));
#if UNITY_WEBGL
return (HasKeyInLocalStorage(key) == 1);
#else
return (UnityEngine.PlayerPrefs.HasKey(key: key));
#endif
}
public static string GetString(string key) {
Log.Debug(string.Format("Jammer.PlayerPrefs.GetString(key: {0})", key));
#if UNITY_WEBGL
return LoadFromLocalStorage(key: key);
#else
return (UnityEngine.PlayerPrefs.GetString(key: key));
#endif
}
public static void SetString(string key, string value) {
Log.Debug(string.Format("Jammer.PlayerPrefs.SetString(key: {0}, value: {1})", key, value));
#if UNITY_WEBGL
SaveToLocalStorage(key: key, value: value);
#else
UnityEngine.PlayerPrefs.SetString(key: key, value: value);
#endif
}
public static void Save() {
Log.Debug(string.Format("Jammer.PlayerPrefs.Save()"));
#if !UNITY_WEBGL
UnityEngine.PlayerPrefs.Save();
#endif
}
#if UNITY_WEBGL
[DllImport("__Internal")]
private static extern void SaveToLocalStorage(string key, string value);
[DllImport("__Internal")]
private static extern string LoadFromLocalStorage(string key);
[DllImport("__Internal")]
private static extern void RemoveFromLocalStorage(string key);
[DllImport("__Internal")]
private static extern int HasKeyInLocalStorage(string key);
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment