Files

61 lines
1.8 KiB
C#
Raw Permalink Normal View History

2022-11-21 18:08:37 +02:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
public static class DemonicsSaver
{
[DllImport("__Internal")]
private static extern void FunctionImplementedInJavaScriptLibraryFile(string str);
[DllImport("__Internal")]
private static extern void SaveData(string strKey, string strData);
[DllImport("__Internal")]
private static extern string LoadData(string str);
2023-01-15 01:54:52 +02:00
//If the current build is Webgl then we save using our custom jsLibrary plugin, otherwise save to player Prefs
2022-11-23 15:03:45 +02:00
public static void Save(string key, string value, bool addOn = false)
2022-11-21 18:08:37 +02:00
{
string keyLower = key.ToLower();
#if UNITY_WEBGL && !UNITY_EDITOR
2022-11-23 15:03:45 +02:00
if (addOn)
{
SaveData(keyLower, LoadData(keyLower) + value);
}
else
{
SaveData(keyLower, value);
}
2022-11-21 18:08:37 +02:00
#endif
#if !UNITY_WEBGL || UNITY_EDITOR
2022-11-23 15:03:45 +02:00
if (addOn)
{
PlayerPrefs.SetString(keyLower, PlayerPrefs.GetString(keyLower) + value);
PlayerPrefs.Save();
}
else
{
PlayerPrefs.SetString(keyLower, value);
PlayerPrefs.Save();
}
2022-11-21 18:08:37 +02:00
#endif
}
2023-01-15 01:54:52 +02:00
//If the current build is Webgl then we load using our custom jsLibrary plugin, otherwise load to player Prefs
public static string Load(string key, string defaultValue = "")
2022-11-21 18:08:37 +02:00
{
2022-11-21 21:15:05 +02:00
string keyLower = key.ToLower();
2022-11-21 18:08:37 +02:00
#if UNITY_WEBGL && !UNITY_EDITOR
2022-11-21 21:15:05 +02:00
string value = LoadData(keyLower);
2022-11-21 18:08:37 +02:00
if (string.IsNullOrEmpty(value))
{
value = defaultValue;
}
return value;
#endif
#if !UNITY_WEBGL || UNITY_EDITOR
2022-11-21 21:15:05 +02:00
return PlayerPrefs.GetString(keyLower, defaultValue);
2022-11-21 18:08:37 +02:00
#endif
}
}