Created
January 1, 2018 23:55
-
-
Save Kryeker/a70c4eaa66e8e2eb4c12cc3a69d4a238 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.Win32; | |
using Newtonsoft.Json; | |
using RejectedGames; | |
using UnityEditor; | |
using UnityEngine; | |
namespace MyEditor | |
{ | |
public class PlayerPrefsToJson : MonoBehaviour | |
{ | |
private static readonly string[] UnityHiddenSettings = | |
{ | |
"UnityGraphicsQuality", | |
"unity.cloud_userid", | |
"unity.player_session_background_time", | |
"unity.player_session_elapsed_time", | |
"unity.player_sessionid" | |
}; | |
[MenuItem("Tools/UpdateJsonPlayerPrefs")] | |
private static void DeletePlayerPrefs() | |
{ | |
File.WriteAllText(Application.dataPath + "/PlayerPrefs.json", | |
JsonConvert.SerializeObject(GetAllKeys().Select(s => new APPWindow.PlayerPrefsEntry(s)).ToDictionary( | |
entry => entry.Key, entry => | |
{ | |
if (entry.Value is string) | |
{ | |
return entry.Value.ToString() | |
.Split(new[] { "=|" }, StringSplitOptions.None).LastOrDefault(); | |
} | |
return entry.Value; | |
}), Formatting.Indented)); | |
} | |
private static string[] GetAllKeys() | |
{ | |
var result = new List<string>(); | |
if (Application.platform == RuntimePlatform.WindowsEditor) | |
result.AddRange(GetAllWindowsKeys()); | |
else if (Application.platform == RuntimePlatform.OSXEditor) | |
result.AddRange(GetAllMacKeys()); | |
#if UNITY_EDITOR_LINUX | |
else if (Application.platform == RuntimePlatform.LinuxEditor) | |
result.AddRange(GetAllLinuxKeys()); | |
#endif | |
else | |
Debug.LogError( | |
"Unsupported platform detected, please contact support@rejected-games.com and let us know."); | |
//Remove something Unity sometimes saves in your PlayerPrefs | |
for (var i = 0; i < UnityHiddenSettings.Length; i++) | |
if (result.Contains(UnityHiddenSettings[i])) | |
result.Remove(UnityHiddenSettings[i]); | |
return result.ToArray(); | |
} | |
/// <summary> | |
/// On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where | |
/// company and product names are the names set up in Project Settings. | |
/// </summary> | |
private static string[] GetAllWindowsKeys() | |
{ | |
var cuKey = Registry.CurrentUser; | |
RegistryKey unityKey; | |
//The default location of PlayerPrefs pre Unity 5_5 | |
#if UNITY_5_5_OR_NEWER | |
unityKey = cuKey.CreateSubKey("Software\\Unity\\UnityEditor\\" + PlayerSettings.companyName + "\\" + | |
PlayerSettings.productName); | |
#else | |
unityKey = cuKey.CreateSubKey("Software\\" + PlayerSettings.companyName + "\\" + PlayerSettings.productName); | |
if (unityKey.GetValueNames().Length == 0) | |
{ | |
//On some machines (Windows 7 & 8 64bit using Unity5.4) PlayersPrefs are saved in HKEY_CURRENT_USER\SOFTWARE\AppDataLow\Software\CompanyName\ProjectName weird enough... | |
unityKey = | |
cuKey.CreateSubKey("Software\\AppDataLow\\Software\\" + PlayerSettings.companyName + "\\" + PlayerSettings.productName); | |
} | |
#endif | |
var values = unityKey.GetValueNames(); | |
for (var i = 0; i < values.Length; i++) | |
values[i] = values[i].Substring(0, values[i].LastIndexOf("_")); | |
return values; | |
} | |
/// <summary> | |
/// On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product | |
/// name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used | |
/// for both Projects run in the Editor and standalone players. | |
/// </summary> | |
private static string[] GetAllMacKeys() | |
{ | |
var plistPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + | |
"/Library/Preferences/unity." + | |
PlayerSettings.companyName + "." + PlayerSettings.productName + ".plist"; | |
var keys = new string[0]; | |
if (File.Exists(plistPath)) | |
{ | |
var fi = new FileInfo(plistPath); | |
var plist = (Dictionary<string, object>)Plist.readPlist(fi.FullName); | |
keys = new string[plist.Count]; | |
plist.Keys.CopyTo(keys, 0); | |
} | |
return keys; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment