private void DrawExistingPrefs() { Event evt = Event.current; KeyValuePair <string, object>[] array = localPrefs.ToArray(); Array.Sort(array, (x, y) => String.Compare(x.Key, y.Key)); for (int i = 0; i < array.Length; i++) { float width = position.width; float deltaItem = 6f; KeyValuePair <string, object> item = array[i]; Rect lastRect = GUILayoutUtility.GetLastRect(); lastRect.x = 2f; lastRect.y += EditorGUIUtility.singleLineHeight + 3f; lastRect.width = position.width - MinusButtonWidth - 6f; if ((evt.type == EventType.MouseDown || evt.type == EventType.mouseDown || evt.type == EventType.mouseUp || evt.type == EventType.MouseUp) && lastRect.Contains(evt.mousePosition)) { SetPrefKey = item.Key; SetPrefType = ConvertTypeToEnum(item.Value.GetType()); SetPrefValue = item.Value.ToString(); DebugLog(" change values"); Repaint(); } GUILayout.BeginHorizontal(); GUILayout.TextField(item.Key, GUILayout.Width(position.width * 0.3f)); width -= position.width * 0.3f; width -= deltaItem; GUILayout.Label(ConvertTypeToEnum(item.Value.GetType()).ToString().ToLower(), GUILayout.Width(PrefTypeWidth - 10f)); width -= (PrefTypeWidth - 10f); width -= deltaItem; GUILayout.TextField(item.Value.ToString(), GUILayout.Width(width - MinusButtonWidth - deltaItem)); if (GUILayout.Button("X", GUILayout.Width(MinusButtonWidth))) { DebugLog("Delete pref with key: " + item.Key); PlayerPrefs.DeleteKey(item.Key); localPrefs.Remove(item.Key); PlayerPrefs.Save(); return; } GUILayout.EndHorizontal(); } GUILayout.Space(DefaultRegionsSpace); }
private void SetPlayerPref(PrefTypes type, string key, string value) { if (string.IsNullOrEmpty(key)) { DebugErroLog("Empty key for Pref"); return; } if (type == PrefTypes.STRING) { DebugLog("PlayerPrefs.SetString " + key + " " + value); PlayerPrefs.SetString(key, value); UpdateValue(key, value); } else if (type == PrefTypes.INT) { int val = 0; if (int.TryParse(value, out val)) { DebugLog("PlayerPrefs.SetInt " + key + " " + val); PlayerPrefs.SetInt(key, val); UpdateValue(key, val); } else { errorText = "Cannot cast " + value + " to int"; DebugErroLog(errorText); } } else if (type == PrefTypes.FLOAT) { float val = 0; if (float.TryParse(value, out val)) { DebugLog("PlayerPrefs.SetFloat " + key + " " + val); PlayerPrefs.SetFloat(key, val); UpdateValue(key, val); } else { errorText = "Cannot cast " + value + " to float"; DebugErroLog(errorText); } } else if (type == PrefTypes.UNDEFINED) { DebugErroLog("Type is undefined. Change it"); } PlayerPrefs.Save(); }
private void DrawSetPlayerPref() { string tempStr = string.Empty; PrefTypes tempType = PrefTypes.UNDEFINED; GUILayout.Space(5f); GUI.backgroundColor = new Color(0.85f, 0.85f, 0.85f); float boxHeight = EditorGUIUtility.singleLineHeight * 4 + 2f * 4 + (showErroBox ? 40f : 0f); GUI.Box(new Rect(1, 1, position.width - 2, boxHeight), ""); GUI.skin.label.fontStyle = FontStyle.Bold; GUILayout.Label("Set PlayerPref"); GUI.skin.label.fontStyle = FontStyle.Normal; GUILayout.BeginHorizontal(); GUILayout.Label("PlayerPref key:", GUILayout.Width(85f)); tempStr = GUILayout.TextField(SetPrefKey, GUILayout.Width(position.width - 85f - 15f)); if (tempStr != SetPrefKey) { OnSetPrefValuesChanged(); Debug.Log("Key wa changed"); } SetPrefKey = tempStr; GUILayout.EndHorizontal(); GUILayout.Space(5f); GUILayout.BeginHorizontal(); GUILayout.Label("Value:", GUILayout.Width(35f)); tempStr = GUILayout.TextField(SetPrefValue, GUILayout.Width(position.width - 50f - PrefTypeWidth - 35f - 20f)); if (tempStr != SetPrefValue) { OnSetPrefValuesChanged(); } SetPrefValue = tempStr; tempType = (PrefTypes)(EditorGUILayout.Popup((int)SetPrefType - 1, prefTypesList.ToArray(), GUILayout.Width(PrefTypeWidth)) + 1); if (tempType != SetPrefType) { OnSetPrefValuesChanged(); } SetPrefType = tempType; GUI.color = new Color(0.6f, 1f, 0.6f); if (GUILayout.Button("Set", GUILayout.Width(50f))) { SetPlayerPref(SetPrefType, SetPrefKey, SetPrefValue); } GUILayout.EndHorizontal(); GUI.backgroundColor = Color.white; GUI.color = Color.white; if (showErroBox) { EditorGUILayout.HelpBox(errorText, MessageType.Error, true); } GUILayout.Space(DefaultRegionsSpace); }