- Home /
Saving and Loading Player Settings Editor Script - 2017.3.10f3
I'm trying to save player settings to be able to load different version settings...
This script seems fairly straight forward, but when I try to load a specific setting by name, it only seems to work if the current productName is the same as the one I want to load? Is this an editor bug of some sort?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class PlayerSettingsHelper : EditorWindow {
static string versionDescriptiveName;
public static Dictionary<string, int> dicPF = new Dictionary<string, int>();
static string pf_identifier_string = "aPlayerSettingsHelperString";
static void SaveSettings(){
Debug.Log("SaveSettings");
if (!dicPF.ContainsKey(versionDescriptiveName))
SaveToPF(DumpSettings());
else Debug.Log(versionDescriptiveName+ " needs to be unique");
}
public static void SaveToPF(string s){
string st = PlayerPrefs.GetString(pf_identifier_string);
st += s+";";
Debug.Log("Saved " + s);
PlayerPrefs.SetString(pf_identifier_string, st);
}
static string DumpSettings(){
string s = "";
s = versionDescriptiveName + "," + PlayerSettings.productName + "," + PlayerSettings.applicationIdentifier + "," + PlayerSettings.bundleVersion;
return s;
}
static void IndexSettings(){
dicPF = new Dictionary<string, int>();
string s = PlayerPrefs.GetString(pf_identifier_string);
Debug.Log(s);
if (s.Length > 3) {
string[] ssv = Parse.SSV(s);
for (int i = 0; i < ssv.Length; i++) {
string[] csv = Parse.CSV(ssv[i]);
if (!dicPF.ContainsKey(csv[0])) {
dicPF.Add(csv[0], i);
Debug.Log(csv[0]);
}
}
} else Debug.Log("Null Settings");
}
public static void ReadSettings(string s){
IndexSettings();
if (dicPF.ContainsKey(s))
ReadSettings(dicPF[s]);
else Debug.Log(s + " does not exist");
}
static void ReadSettings(int n){
string s = PlayerPrefs.GetString(pf_identifier_string);
if (s.Length > 3) {
string[] ssv = Parse.SSV(s);
if (n < ssv.Length) {
ReadStringToPS(ssv[n]);
} else
Debug.Log(n + " greater than " + ssv.Length);
}
}
static void ReadStringToPS(string s){
Debug.Log("Trying to read " + s);
if (s.Length > 3) {
string[] csv = Parse.CSV(s);
if (csv.Length >= 4) {
Debug.Log("Loaded " + csv[0]);
PlayerSettings.productName = csv[1];
PlayerSettings.applicationIdentifier = csv[2];
PlayerSettings.bundleVersion = csv[3];
Debug.Log("Assigned " + csv[1]);
}
}
}
[MenuItem("PlayerSettingsHelper/LoadSettings")]
public static void ShowWindow() {
//Show existing window instance. If one doesn't exist, make one.
IndexSettings();
EditorWindow.GetWindow(typeof(PlayerSettingsHelper));
}
void OnGUI() {
GUILayout.Label("PlayerSettingsHelper Load Settings");
EditorGUILayout.BeginHorizontal();
versionDescriptiveName = EditorGUILayout.TextField("Descriptive Name for this Version", versionDescriptiveName);
if (GUILayout.Button("Save Settings")) {
SaveSettings();
}
if (GUILayout.Button("Load Settings")) {
ReadSettings(versionDescriptiveName);
}
EditorGUILayout.EndHorizontal();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612153654im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Keeping state that cannot be serialized between play mode for EditorWindow 1 Answer
Access Monobehavior Instance from Static Function of Editor Script 1 Answer
Is it possible to have editor-only native plugins? 0 Answers
Editor scripting - How to show the Assets menu as a context menu 1 Answer
Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers