/// <summary> /// Get the layotu field associated with the provided PlayerPrefReference /// </summary> /// <param name="reference">The PlayerPrefReference to create a field for</param> private void GetLayoutField(PlayerPrefReference reference) { // If this is the field we have selected create an editable field if (editKey == reference.key) { string value = reference.GetPlayerPrefValue(); switch (reference.DefinePlayerPref()) { case PlayerPrefTypes.STRING: reference.SetPlayerPref(EditorGUILayout.TextField(value, GUILayout.Height(labelHeight))); break; case PlayerPrefTypes.INT: reference.SetPlayerPref(EditorGUILayout.IntField(int.Parse(value), GUILayout.Height(labelHeight)).ToString()); break; case PlayerPrefTypes.BOOL: reference.SetPlayerPref((string)EditorGUILayout.Toggle(bool.Parse(value), GUILayout.Height(labelHeight)).ToString()); break; default: reference.SetPlayerPref(EditorGUILayout.TextField(value, GUILayout.Height(labelHeight))); break; } } else { EditorGUILayout.SelectableLabel(reference.GetPlayerPrefValue(), GUILayout.Height(labelHeight)); } }
/// <summary> /// Add a new PlayerPrefKey manually /// </summary> /// <param name="keyName">The name of the key to add</param> private void AddPlayerPrefKey(string keyName) { PlayerPrefReference newPlayerPref = new PlayerPrefReference("<-MANUAL->", keyName); newPlayerPref.IsManual = true; if (toolbarSelection == 1) { newPlayerPref.IsWatched = true; } playerPrefs.Add(newPlayerPref); }
/// <summary> /// Create a PlayerPrefView in the scrollview /// </summary> /// <param name="pref">The PlayerPrefReference to create the view for</param> /// <param name="watchedButtonStyle">The style to use for the watch style</param> private void CreatePlayerPrefView(ref PlayerPrefReference pref, GUIStyle watchedButtonStyle) { EditorGUILayout.BeginHorizontal(); // Create the watch button based on IsWatched state Color current = GUI.color; if (pref.IsWatched) { GUI.color = Color.red; } else { GUI.color = Color.green; } if (GUILayout.Button(pref.IsWatched ? "+" : "-", watchedButtonStyle, GUILayout.Width(20))) { pref.IsWatched = !pref.IsWatched; } GUI.color = current; // Create visual elements for Key & Constant value EditorGUILayout.SelectableLabel(showConst ? pref.constant : string.Empty, GUILayout.Height(labelHeight)); EditorGUILayout.SelectableLabel(showKeys ? pref.key : string.Empty, GUILayout.Height(labelHeight)); // Create the field for the PlayerPrefReference.value pref.GetPlayerPrefValue(); GetLayoutField(pref); // Allow the changing of the type of variable the playerpref is PlayerPrefTypes selectedType = pref.overrideType != PlayerPrefTypes.AUTO ? pref.overrideType : pref.type; selectedType = (PlayerPrefReference.PlayerPrefTypes)EditorGUILayout.EnumPopup(selectedType, GUILayout.Width(55)); if ((pref.type != selectedType && pref.overrideType == PlayerPrefTypes.AUTO) || (pref.overrideType != selectedType && pref.overrideType != PlayerPrefTypes.AUTO)) { pref.overrideType = selectedType; } // If the key is manually added we add the option to remove it from the view if (pref.IsManual) { if (GUILayout.Button("X", GUILayout.Width(25))) { playerPrefs.Remove(pref); return; } } // Add the option to edit the key, making it an interactable field if (GUILayout.Button("Edit", GUILayout.Width(35))) { GUI.FocusControl(""); if (editKey == pref.key) { editKey = string.Empty; } else { editKey = pref.key; } } EditorGUILayout.EndHorizontal(); }
/// <summary> /// OnGUI method, draws the editor window field /// </summary> public void OnGUI() { // The Script input field EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("PlayerPref Keys Script"); playerPrefsScript = EditorGUILayout.ObjectField(playerPrefsScript, typeof(MonoScript), false) as MonoScript; EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); // The tab selection (view tabs) GUIContent[] content = new GUIContent[] { new GUIContent("ALL"), new GUIContent("WATCH") }; toolbarSelection = GUILayout.Toolbar(toolbarSelection, content); // The row names & options to enable/disable them EditorGUILayout.BeginHorizontal(); showConst = EditorGUILayout.Toggle(showConst, GUILayout.Width(20)); EditorGUILayout.LabelField("CONST", GUILayout.Height(labelHeight)); showKeys = EditorGUILayout.Toggle(showKeys, GUILayout.Width(20)); EditorGUILayout.LabelField("KEY", GUILayout.Height(labelHeight)); EditorGUILayout.LabelField("VALUE", GUILayout.Height(labelHeight)); EditorGUILayout.EndHorizontal(); if (toolbarSelection != 0) { GUI.enabled = false; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Filter Keys By:", GUILayout.Width(90)); filterTextInput = EditorGUILayout.TextField(filterTextInput); EditorGUILayout.EndHorizontal(); GUI.enabled = true; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Sort By:"); selectedSort = GUILayout.Toolbar(selectedSort, sortingTypes, "Button"); EditorGUILayout.EndHorizontal(); // Create the elements GUIStyle watchedButtonStyle = new GUIStyle(EditorStyles.miniButtonRight); scrollView = EditorGUILayout.BeginScrollView(scrollView, EditorStyles.helpBox, GUILayout.Height(position.height * 0.65f)); List <PlayerPrefReference> currentPrefs = toolbarSelection == 0 ? playerPrefs : playerPrefs.Where(x => x.IsWatched).ToList(); switch ((SortingTypes)selectedSort) { default: case SortingTypes.NONE: break; case SortingTypes.CONST_AZ: currentPrefs = currentPrefs.OrderBy(x => x.constant).ToList(); break; case SortingTypes.CONST_ZA: currentPrefs = currentPrefs.OrderByDescending(x => x.constant).ToList(); break; case SortingTypes.KEY_AZ: currentPrefs = currentPrefs.OrderBy(x => x.key).ToList(); break; case SortingTypes.KEY_ZA: currentPrefs = currentPrefs.OrderByDescending(x => x.key).ToList(); break; } for (int i = 0; i < currentPrefs.Count; i++) { PlayerPrefReference pref = currentPrefs[i]; if (!string.IsNullOrWhiteSpace(filterTextInput) && toolbarSelection == 0) { if (!pref.key.ToLower().Contains(filterTextInput.ToLower()) && !pref.constant.ToLower().Contains(filterTextInput.ToLower())) { continue; } } CreatePlayerPrefView(ref pref, watchedButtonStyle); currentPrefs[i] = pref; } EditorGUILayout.EndScrollView(); // THe menu for addign a new Manual Key EditorGUILayout.HelpBox(new GUIContent("Enter the 'key' name here to track it manually, this is case sensitive")); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Track key", GUILayout.Width(75)); addKeyField = EditorGUILayout.TextField(addKeyField); if (GUILayout.Button("Add", GUILayout.Width(75)) && !string.IsNullOrWhiteSpace(addKeyField)) { manualKeys.Add(addKeyField); AddPlayerPrefKey(addKeyField); addKeyField = string.Empty; } EditorGUILayout.EndHorizontal(); // The button to refresh the current PlayerPref list EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Refresh")) { GetPlayerPrefsFromFile(); } // The button to delete all added Manual Keys if (GUILayout.Button("Delete Manual Keys")) { foreach (string key in manualKeys) { PlayerPrefReference pref = playerPrefs.FirstOrDefault(x => x.key == key && x.IsManual); if (pref == null) { continue; } playerPrefs.Remove(pref); } manualKeys.Clear(); } EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { Repaint(); } }