public static string GetAllKeyAndValues() { Pref[] prefs = keys.ToArray(); //ArrayList valuesList = new ArrayList(); string str = ""; for (int i = 0; i < prefs.Length; i++) { Pref p = prefs[i]; string key = p.key; PrefType type = p.type; object v = p.v; /* * if(type == PrefType.INT){ * valuesList.Add(PlayerPrefs.GetInt(key)); * }else if(type == PrefType.FLOAT){ * valuesList.Add(PlayerPrefs.GetFloat(key)); * }else if(type == PrefType.STRING){ * * }else{ * Debug.LogWarning("noKeyType: "+key); * } */ str += (int)type + "," + key + "," + v.ToString() + "\n"; } return(str); }
public PrefDesc(string value, PrefType type, bool hidden, string displayText) { Value = value; ValueEdited = value; Type = type; Hidden = hidden; DisplayText = displayText; }
public PrefDesc(string value, PrefType type, bool hidden, string displayText) : base(value, type, hidden, displayText) { Value = value; ValueEdited = value; base.Type = (CumPreferenceType)type; Hidden = hidden; DisplayText = displayText; }
public static IEnumerator LoadFromFile__(string path) { Debug.LogWarning("LoadFromFile: " + path); WWW wwwFile = new WWW(path); yield return(wwwFile); if (wwwFile.error != null) { Debug.LogWarning("www Error: " + path); yield return(null); } else { //Debug.Log(wwwFile.text); } string str = wwwFile.text; string[] splitStr = str.Split('\n'); keys = new List <Pref>(); for (int i = 0; i < splitStr.Length; i++) { string[] prefLine = splitStr[i].Split(','); if (prefLine.Length > 2) { int index = int.Parse(prefLine[0]); PrefType type = (PrefType)(Enum.GetValues(typeof(PrefType)).GetValue(index)); string key = prefLine[1]; object v = prefLine[2]; Pref p = new Pref(key, type, v); keys.Add(p); if (type == PrefType.INT) { PlayerPrefs.SetInt(key, System.Convert.ToInt32(v)); } else if (type == PrefType.FLOAT) { PlayerPrefs.SetFloat(key, System.Convert.ToSingle(v)); } else if (type == PrefType.STRING) { PlayerPrefs.SetString(key, (string)v); } else { Debug.LogWarning("noKeyType: " + key); } } } }
// generic static T Get <T>(PrefType prefType, int index) { if (typeof(T) == typeof(bool)) { return((T)(object)PlayerPrefs.HasKey(ParametersToString(prefType, index))); } if (typeof(T) == typeof(int)) { return((T)(object)PlayerPrefs.HasKey(ParametersToString(prefType, index))); } return((T)(object)null); }
// load from File to PlayerPrefs public static void LoadFromFile() { string path = Application.dataPath + fileName; Debug.LogWarning("LoadFromFile: " + path); try { using (System.IO.StreamReader sr = new StreamReader(path, Encoding.UTF8)) { string str = sr.ReadToEnd(); Debug.LogWarning(str); string[] splitStr = str.Split('\n'); // keys = new List<Pref> (); for (int i = 0; i < splitStr.Length; i++) { string[] prefLine = splitStr [i].Split(','); if (prefLine.Length > 2) { int index = int.Parse(prefLine [0]); PrefType type = (PrefType)(Enum.GetValues(typeof(PrefType)).GetValue(index)); string key = prefLine [1]; object v = prefLine [2]; Pref p = new Pref(key, type, v); // keys.Add (p); if (type == PrefType.INT) { PlayerPrefs.SetInt(key, System.Convert.ToInt32(v)); } else if (type == PrefType.FLOAT) { PlayerPrefs.SetFloat(key, System.Convert.ToSingle(v)); } else if (type == PrefType.STRING) { PlayerPrefs.SetString(key, (string)v); } else { Debug.LogWarning("noKeyType: " + key); } } } } } catch { Debug.LogWarning("No File"); } }
public PlayerPrefValue(string name, string prefType, string value) { this.m_Name = name; switch (prefType) { case "integer": this.m_IntValue = int.Parse(value); this.m_PrefType = PrefType.Int; break; case "real": this.m_FloatValue = float.Parse(value); this.m_PrefType = PrefType.Float; break; case "string": this.m_StringValue = value; this.m_PrefType = PrefType.String; break; } }
public void EnsurePref(string key, PrefType type) { if (!EditorPrefs.HasKey(key)) { if (type == PrefType.Bool) { EditorPrefs.SetBool(key, false); } if (type == PrefType.Float) { EditorPrefs.SetFloat(key, 0f); } if (type == PrefType.Int) { EditorPrefs.SetInt(key, 0); } if (type == PrefType.String) { EditorPrefs.SetString(key, string.Empty); } } }
protected virtual void DrawNewSettingsArea(GUILayoutOption[] options) { _showNewSetting = EditorGUILayout.Foldout(_showNewSetting, "New Setting"); if (_showNewSetting) { EditorGUI.indentLevel++; _newSettingKey = EditorGUILayout.TextField("Key: ", _newSettingKey, options); _newSettingType = (PrefType)EditorGUILayout.EnumPopup("Type: ", _newSettingType, options); if (_newSettingType == PrefType.Enum) { _newSettingTypeEnum = EditorGUILayout.TextField("Enum Type: ", _newSettingTypeEnum, options); } EditorGUI.indentLevel--; if (QuickBaseEditor.DrawButton("Create New Setting", options)) { object value = null; if (_newSettingKey.Length == 0) { EditorUtility.DisplayDialog("New Setting Creation Error", "The Key string cannot be empty", "Ok"); } else if (QuickPlayerPrefs.HasKey(_newSettingKey)) { EditorUtility.DisplayDialog("New Setting Creation Error", "The Key " + _newSettingKey + " already exists", "Ok"); } else if (CreateNewSetting(out value)) { QuickPlayerPrefs.SetValue(_newSettingKey, value); _newSettingKey = _newSettingTypeEnum = ""; _newSettingType = PrefType.String; } } } }
public static float GetFloat(this PrefType prop, float defaultValue = 0) { return(PlayerPrefs.GetFloat(_PREFIX + prop.ToString(), defaultValue)); }
public Pref(string key_, PrefType type_, object v_) { key = key_; type = type_; v = v_; }
private static void RegisterPref(string section, string name, string defaultValue, string displayText, PrefType type, bool hideFromList) { if (prefs.TryGetValue(section, out Dictionary <string, PrefDesc> prefsInSection)) { if (prefsInSection.TryGetValue(name, out PrefDesc pref)) { MelonModLogger.LogError("Trying to registered Pref " + section + ":" + name + " more than one time"); } else { string toStoreValue = defaultValue; if (ModPrefsController.HasKey(section, name)) { toStoreValue = ModPrefsController.GetString(section, name, defaultValue); } else { ModPrefsController.SetString(section, name, defaultValue); } prefsInSection.Add(name, new PrefDesc(toStoreValue, type, hideFromList, (displayText ?? "") == "" ? name : displayText)); } } else { Dictionary <string, PrefDesc> dic = new Dictionary <string, PrefDesc>(); string toStoreValue = defaultValue; if (ModPrefsController.HasKey(section, name)) { toStoreValue = ModPrefsController.GetString(section, name, defaultValue); } else { ModPrefsController.SetString(section, name, defaultValue); } dic.Add(name, new PrefDesc(toStoreValue, type, hideFromList, (displayText ?? "") == "" ? name : displayText)); prefs.Add(section, dic); } }
public bool GetPref(String prefName, out PrefType type, out String value) { type = PrefType.TypeString; value = ""; if (prefs == null) return false; // Xml is in the form. //<PREF Name='UIRoot' Persistent="true"><TYPE>string</TYPE><VALUE>/usr/local/dx</VALUE></PREF> //<PREF Name='wizard' Persistent="false"><TYPE>bool</TYPE><VALUE>false</VALUE></PREF> String lookup = "DXUIPREF[@Name='"; lookup += prefName; lookup += "']"; String val = null, typeS = null; try { XmlNode n = root.SelectSingleNode(lookup); if (n == null) return false; XmlNode n2 = n.SelectSingleNode("VALUE"); val = n2.InnerText; n2 = n.SelectSingleNode("TYPE"); typeS = n2.InnerText; } catch (XmlException) { Console.WriteLine("Corrupted prefs file--try deleting it"); } if (val.Length == 0) { value = null; return true; } value = val; if (typeS.CompareTo("bool") == 0) type = PrefType.TypeBool; else if (typeS.CompareTo("int") == 0) type = PrefType.TypeInt; else if (typeS.CompareTo("float") == 0) type = PrefType.TypeFloat; else type = PrefType.TypeString; return true; }
public static string SetString(this PrefType prop, string value) { PlayerPrefs.SetString(_PREFIX + prop.ToString(), value); return(value); }
protected virtual void OnGUI() { Rect r = this.position; r.x = 0F; r.y = 0F; if (this.prefManagers.Length > 0) { r.height = Constants.SingleLineHeight; GUILayout.BeginArea(r); { EditorGUILayout.BeginHorizontal(GeneralStyles.Toolbar); { EditorGUI.BeginChangeCheck(); this.currentManager = EditorGUILayout.Popup(this.currentManager, this.prefManagerNames, GeneralStyles.ToolbarDropDown); if (EditorGUI.EndChangeCheck() == true) { this.LoadCurrentPreferences(); return; } if (GUILayout.Button(this.showAdd == true ? "˄" : "˅", GeneralStyles.ToolbarDropDown, GUILayoutOptionPool.Width(24F)) == true) { this.showAdd = !this.showAdd; return; } EditorGUI.BeginChangeCheck(); this.searchKeywords = EditorGUILayout.TextField(this.searchKeywords, GeneralStyles.ToolbarSearchTextField); if (EditorGUI.EndChangeCheck() == true) { this.searchPatterns = Utility.SplitKeywords(this.searchKeywords, ' '); this.UpdateFilteredIndexes(); return; } if (GUILayout.Button("", GeneralStyles.ToolbarSearchCancelButton) == true) { this.searchKeywords = string.Empty; this.searchPatterns = Utility.SplitKeywords(this.searchKeywords, ' '); GUI.FocusControl(null); this.UpdateFilteredIndexes(); return; } if (GUILayout.Button(LC.G("NGPrefs_Refresh"), GeneralStyles.ToolbarButton, GUILayoutOptionPool.MaxWidth(100F))) { this.LoadCurrentPreferences(); GUI.FocusControl(null); return; } if (string.IsNullOrEmpty(this.searchKeywords) == true) { if (GUILayout.Button(LC.G("NGPrefs_ClearAll"), GeneralStyles.ToolbarButton, GUILayoutOptionPool.MaxWidth(100F)) && ((Event.current.modifiers & Constants.ByPassPromptModifier) != 0 || EditorUtility.DisplayDialog(LC.G("NGPrefs_ClearAll"), LC.G("NGPrefs_ClearAllConfirm"), LC.G("Yes"), LC.G("No")) == true)) { try { this.prefManagers[this.currentManager].DeleteAll(); } catch (Exception ex) { this.errorPopup.exception = ex; } return; } } else { if (GUILayout.Button(LC.G("NGPrefs_ClearList"), GeneralStyles.ToolbarButton, GUILayoutOptionPool.MaxWidth(100F)) && ((Event.current.modifiers & Constants.ByPassPromptModifier) != 0 || EditorUtility.DisplayDialog(LC.G("NGPrefs_ClearList"), LC.G("NGPrefs_ClearListConfirm"), LC.G("Yes"), LC.G("No")) == true)) { try { for (int k = 0; k < this.filteredIndexes.Count; k++) { int i = this.filteredIndexes[k]; this.prefManagers[this.currentManager].DeleteKey(this.prefManagers[this.currentManager].keys[i]); } for (int k = 0; k < this.filteredIndexes.Count; k++) { int i = this.filteredIndexes[k]; this.prefManagers[this.currentManager].DeleteIndex(i - k); } this.filteredIndexes.Clear(); } catch (Exception ex) { this.errorPopup.exception = ex; } return; } } } EditorGUILayout.EndHorizontal(); } GUILayout.EndArea(); if (this.showAdd == true) { r.y += r.height; GUILayout.BeginArea(r); { EditorGUILayout.BeginHorizontal(GeneralStyles.Toolbar); { using (LabelWidthRestorer.Get(60F)) { this.prefType = (PrefType)EditorGUILayout.EnumPopup(LC.G("NGPrefs_Type"), this.prefType, GeneralStyles.ToolbarDropDown, GUILayoutOptionPool.ExpandWidthFalse); this.newKey = EditorGUILayout.TextField(LC.G("NGPrefs_Key"), this.newKey, GeneralStyles.ToolbarTextField, GUILayoutOptionPool.ExpandWidthTrue); switch (this.prefType) { case PrefType.Int: this.newValueInt = EditorGUILayout.IntField(LC.G("NGPrefs_Value"), this.newValueInt, GeneralStyles.ToolbarTextField, GUILayoutOptionPool.ExpandWidthTrue); break; case PrefType.Float: this.newValueFloat = EditorGUILayout.FloatField(LC.G("NGPrefs_Value"), this.newValueFloat, GeneralStyles.ToolbarTextField, GUILayoutOptionPool.ExpandWidthTrue); break; case PrefType.String: this.newValueString = EditorGUILayout.TextField(LC.G("NGPrefs_Value"), this.newValueString, GeneralStyles.ToolbarTextField, GUILayoutOptionPool.ExpandWidthTrue); break; } EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(this.newKey)); { if (GUILayout.Button(LC.G("NGPrefs_Add"), GeneralStyles.ToolbarButton, GUILayoutOptionPool.MaxWidth(100F))) { switch (this.prefType) { case PrefType.Int: this.prefManagers[this.currentManager].SetInt(this.newKey, this.newValueInt); break; case PrefType.Float: this.prefManagers[this.currentManager].SetFloat(this.newKey, this.newValueFloat); break; case PrefType.String: this.prefManagers[this.currentManager].SetString(this.newKey, this.newValueString); break; } this.LoadCurrentPreferences(); return; } } EditorGUI.EndDisabledGroup(); } } EditorGUILayout.EndHorizontal(); } GUILayout.EndArea(); } } r.y += r.height; if (this.errorPopup.exception != null) { r.height = this.errorPopup.boxHeight; this.errorPopup.OnGUIRect(r); r.y += r.height; r.height = Constants.SingleLineHeight; } if (this.OSXPListMissing == true) { r.height = 32F; EditorGUI.HelpBox(r, "NG Tools seems to be initializing for the first time. " + NGPrefsWindow.Title + " can not work on first initialization, because it needs the preferences file generated by Unity, which does not exist at this moment.", MessageType.Warning); r.y += r.height; EditorGUI.HelpBox(r, "To force Unity to generate it, I suggest you to use EditorPrefs or PlayerPrefs from a custom script then restart Unity.", MessageType.Info); r.y += r.height; r.height = Constants.SingleLineHeight; } if (this.currentManager < 0) { return; } r.y += 1F; this.labelWidth = Mathf.Clamp(this.labelWidth, NGPrefsWindow.MinLabelWidth, this.position.width - NGPrefsWindow.MinValueWidth); r.width = this.labelWidth; EditorGUI.LabelField(r, LC.G("NGPrefs_Key"), GeneralStyles.CenterText); r.x += r.width + 20F; r.width = 2F; EditorGUI.DrawRect(r, Color.grey); r.x -= 1F; r.width += 2F; EditorGUIUtility.AddCursorRect(r, MouseCursor.ResizeHorizontal); if (this.draggingSplitterBar == true && Event.current.type == EventType.MouseDrag) { if (Mathf.Abs(r.y - Event.current.mousePosition.y) > NGPrefsWindow.AutoRestoreLabelWidthThreshold) { this.labelWidth = this.originLabelWidth; } else { this.labelWidth = Mathf.Clamp(this.originLabelWidth - this.originPositionX + Event.current.mousePosition.x, NGPrefsWindow.MinLabelWidth, this.position.width - NGPrefsWindow.MinValueWidth); } Event.current.Use(); } else if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition) == true) { this.originPositionX = Event.current.mousePosition.x; this.originLabelWidth = this.labelWidth; this.draggingSplitterBar = true; Event.current.Use(); } else if (this.draggingSplitterBar == true && Event.current.type == EventType.MouseUp) { // Auto adjust height on left click or double click. if (r.Contains(Event.current.mousePosition) == true && (Event.current.button == 1 || (this.lastClick + Constants.DoubleClickTime > EditorApplication.timeSinceStartup && Mathf.Abs(this.originPositionX - Event.current.mousePosition.x) < 5F))) { this.labelWidth = EditorGUIUtility.labelWidth; } this.lastClick = EditorApplication.timeSinceStartup; this.draggingSplitterBar = false; Event.current.Use(); } r.width = this.position.width - r.x; EditorGUI.LabelField(r, LC.G("NGPrefs_Value"), GeneralStyles.CenterText); r.x = 0F; r.y += r.height + 1F; r.width = this.position.width; r.height = 1F; EditorGUI.DrawRect(r, NGPrefsWindow.ColumnHeaderBackgroundColor); r.y += 3F; r.height = this.position.height - r.y; this.bodyRect = r; Rect viewRect = new Rect(); viewRect.height = this.filteredIndexes.Count * Constants.SingleLineHeight; viewRect.width = r.width - (viewRect.height >= this.bodyRect.height ? 15F : 0F); using (LabelWidthRestorer.Get(this.labelWidth)) { this.scrollPosition = GUI.BeginScrollView(r, this.scrollPosition, viewRect); { r.y = 0F; r.height = Constants.SingleLineHeight; r.width -= viewRect.height >= this.bodyRect.height ? 15F : 0F; this.OnGUIPreferences(r); } GUI.EndScrollView(); } }
public static int SetInt(this PrefType prop, int value) { PlayerPrefs.SetInt(_PREFIX + prop.ToString(), value); return(value); }
public static float SetFloat(this PrefType prop, float value) { PlayerPrefs.SetFloat(_PREFIX + prop.ToString(), value); return(value); }
public static string GetString(this PrefType prop, string defaultValue = null) { return(PlayerPrefs.GetString(_PREFIX + prop.ToString(), defaultValue)); }
private void DrawAddEntry() { var textFieldStyle = new GUIStyle(GUI.skin.textField); EditorGUILayout.Space(); GUILayout.Label(showEditorPrefs ? "Add EditorPref" : "Add PlayerPref", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); if (showEditorPrefs) { newEntryType = (PrefType)GUILayout.Toolbar((int)newEntryType, new string[] { "float", "int", "string", "bool" }); } else { if (newEntryType == PrefType.Bool) { newEntryType = PrefType.String; } newEntryType = (PrefType)GUILayout.Toolbar((int)newEntryType, new string[] { "float", "int", "string" }); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Key", EditorStyles.boldLabel); GUILayout.Label("Value", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); GUI.SetNextControlName("newEntryKey"); newEntryKey = EditorGUILayout.TextField(newEntryKey, textFieldStyle); GUI.SetNextControlName("newEntryValue"); switch (newEntryType) { case PrefType.Float: newEntryValueFloat = EditorGUILayout.FloatField(newEntryValueFloat, textFieldStyle); break; case PrefType.Int: newEntryValueInt = EditorGUILayout.IntField(newEntryValueInt, textFieldStyle); break; case PrefType.String: newEntryValueString = EditorGUILayout.TextField(newEntryValueString, textFieldStyle); break; case PrefType.Bool: newEntryValueBool = EditorGUILayout.Toggle(newEntryValueBool); break; } // If the user hit enter while either the key or value fields were being edited. var keyboardAddPressed = Event.current.isKey && Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyUp && (GUI.GetNameOfFocusedControl() == "newEntryKey" || GUI.GetNameOfFocusedControl() == "newEntryValue"); // If the user clicks the Add button or hits return (and there is a non-empty key), create the PlayerPref. if ((GUILayout.Button("Add", GUILayout.Width(40)) || keyboardAddPressed) && !string.IsNullOrEmpty(newEntryKey)) { if (newEntryType == PrefType.Float) { SetFloat(newEntryKey, newEntryValueFloat); CacheRecord(newEntryKey, newEntryValueFloat); } else if (newEntryType == PrefType.Int) { SetInt(newEntryKey, newEntryValueInt); CacheRecord(newEntryKey, newEntryValueInt); } else if (newEntryType == PrefType.Bool) { SetBool(newEntryKey, newEntryValueBool); CacheRecord(newEntryKey, newEntryValueBool); } else { SetString(newEntryKey, newEntryValueString); CacheRecord(newEntryKey, newEntryValueString); } Save(); Repaint(); newEntryKey = ""; newEntryValueFloat = 0; newEntryValueInt = 0; newEntryValueString = ""; // Deselect. GUI.FocusControl(""); } EditorGUILayout.EndHorizontal(); }
public static int GetInt(this PrefType prop, int defaultValue = 0) { return(PlayerPrefs.GetInt(_PREFIX + prop.ToString(), defaultValue)); }
public bool SetPref(String prefName, PrefType type, String value, bool persist) { if (prefs == null) return false; String lookup = "DXUIPREF[@Name='" + prefName + "']"; String typeS = null; switch (type) { case PrefType.TypeString: typeS = "string"; break; case PrefType.TypeInt: typeS = "int"; break; case PrefType.TypeBool: typeS = "bool"; break; case PrefType.TypeFloat: typeS = "float"; break; } try { XmlNode n = root.SelectSingleNode(lookup); if (n != null) { try { XmlElement elem = prefs.CreateElement("DXUIPREF"); elem.SetAttribute("Name", prefName); if (persist) elem.SetAttribute("Persistent", "true"); else elem.SetAttribute("Persistent", "false"); XmlElement val = prefs.CreateElement("VALUE"); val.InnerText = value; elem.AppendChild(val); XmlElement tp = prefs.CreateElement("TYPE"); tp.InnerText = typeS; elem.AppendChild(tp); root.ReplaceChild(elem, n); } catch (XmlException) { return false; } } else try { XmlElement elem = prefs.CreateElement("DXUIPREF"); elem.SetAttribute("Name", prefName); if (persist) elem.SetAttribute("Persistent", "true"); else elem.SetAttribute("Persistent", "false"); XmlElement val = prefs.CreateElement("VALUE"); val.InnerText = value; elem.AppendChild(val); XmlElement tp = prefs.CreateElement("TYPE"); tp.InnerText = typeS; elem.AppendChild(tp); root.AppendChild(elem); } catch (XmlException) { return false; } } catch (XmlException) { return false; } return true; }
/// <summary> /// Set a Default for a Pref. If it already exists, just return. /// In order to set a pref persistently (saves out to file) use /// SetPref. /// </summary> /// <param name="prefName"></param> /// <param name="type"></param> /// <param name="value"></param> /// <returns></returns> public bool SetDefault(String prefName, PrefType type, String value) { return SetDefault(prefName, type, value, false); }