Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[Editor Utility] Player Prefs Editor - Edit Player Prefs inside the Unity editor!

Discussion in 'Extensions & OnGUI' started by Romejanic, Nov 27, 2015.

  1. Romejanic

    Romejanic

    Joined:
    Jul 29, 2013
    Posts:
    1
    Hello everyone! I wrote this quick editor for one of my personal projects by I decided to share it with you guys! This little utility is very useful for people who need to use the PlayerPrefs class to save information. This tool allows you to access, modify and delete elements of the save file without digging through your system and finding the file to edit! Here's how it works:

    To open the editor
    1. Install the file
    2. Click Edit>Player Prefs
    To get a value
    1. Change "Key to Set" to the key you want to retrieve
    2. Change the Field type to the type of key you are storing
    3. Press "Get Key". The value will be pasted into the value field
    To set a value
    PLEASE NOTE: This option will erase any previous value in the preferences file. There is no way to restore it!
    1. Change "Key to Set" to the key you want to set.
    2. Change the Field type to the type of key you want to store
    3. Enter a new value for the key
    4. Press "Set Key". The value will be saved into the Player Prefs file.
    To delete a value
    PLEASE NOTE: This option will erase the value in the preferences file. There is no way to restore it!
    1. Change "Key to Set" to the key you want to delete.
    2. Press "Delete Key". The value will be erased from the Player Prefs file.
    If you need to as well, there is an option to delete all keys stored in the Player Prefs file! Be careful, because this will also erase the information from the Unity setup dialog (the box that shows the screen resolution, graphics settings, controls, etc).

    Here's the full source code, just drop into a C# file in your /Assets/Editor folder, and call the file "PlayerPrefsEditor.cs"
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class PlayerPrefsEditor : EditorWindow {
    6.  
    7.     [MenuItem("Edit/Player Prefs")]
    8.     public static void openWindow() {
    9.  
    10.         PlayerPrefsEditor window = (PlayerPrefsEditor)EditorWindow.GetWindow(typeof(PlayerPrefsEditor));
    11.         window.titleContent = new GUIContent("Player Prefs");
    12.         window.Show();
    13.  
    14.     }
    15.  
    16.     public enum FieldType { String,Integer,Float }
    17.  
    18.     private FieldType fieldType = FieldType.String;
    19.     private string setKey = "";
    20.     private string setVal = "";
    21.     private string error = null;
    22.  
    23.     void OnGUI() {
    24.  
    25.         EditorGUILayout.LabelField("Player Prefs Editor", EditorStyles.boldLabel);
    26.         EditorGUILayout.LabelField("by RomejanicDev");
    27.         EditorGUILayout.Separator();
    28.  
    29.         fieldType = (FieldType)EditorGUILayout.EnumPopup("Key Type", fieldType);
    30.         setKey = EditorGUILayout.TextField("Key to Set", setKey);
    31.         setVal = EditorGUILayout.TextField("Value to Set", setVal);
    32.  
    33.         if(error != null) {
    34.  
    35.             EditorGUILayout.HelpBox(error, MessageType.Error);
    36.  
    37.         }
    38.  
    39.         if(GUILayout.Button("Set Key")) {
    40.  
    41.             if(fieldType == FieldType.Integer) {
    42.  
    43.                 int result;
    44.                 if(!int.TryParse(setVal, out result)) {
    45.                    
    46.                     error = "Invalid input \"" + setVal + "\"";
    47.                     return;
    48.                    
    49.                 }
    50.                
    51.                 PlayerPrefs.SetInt(setKey, result);
    52.  
    53.             } else if(fieldType == FieldType.Float) {
    54.  
    55.                 float result;
    56.                 if(!float.TryParse(setVal, out result)) {
    57.  
    58.                     error = "Invalid input \"" + setVal + "\"";
    59.                     return;
    60.  
    61.                 }
    62.  
    63.                 PlayerPrefs.SetFloat(setKey, result);
    64.  
    65.             } else {
    66.  
    67.                 PlayerPrefs.SetString(setKey, setVal);
    68.  
    69.             }
    70.  
    71.             PlayerPrefs.Save();
    72.             error = null;
    73.  
    74.         }
    75.  
    76.         if(GUILayout.Button("Get Key")) {
    77.        
    78.             if(fieldType == FieldType.Integer) {
    79.  
    80.                 setVal = PlayerPrefs.GetInt(setKey).ToString();
    81.  
    82.             } else if(fieldType == FieldType.Float) {
    83.  
    84.                 setVal = PlayerPrefs.GetFloat(setKey).ToString();
    85.  
    86.             } else {
    87.  
    88.                 setVal = PlayerPrefs.GetString(setKey);
    89.  
    90.             }
    91.        
    92.         }
    93.  
    94.         if(GUILayout.Button("Delete Key")) {
    95.  
    96.             PlayerPrefs.DeleteKey(setKey);
    97.             PlayerPrefs.Save();
    98.  
    99.         }
    100.  
    101.         if(GUILayout.Button("Delete All Keys")) {
    102.  
    103.             PlayerPrefs.DeleteAll();
    104.             PlayerPrefs.Save();
    105.  
    106.         }
    107.  
    108.     }
    109.  
    110. }
    111.  
    Hope you enjoy the editor! It is really handy if you need to make quick adjustments to the PlayerPrefs file without writing a new script or editing the file directly! If you make any modifications and improvements to this code, please leave credit to me, and leave the "by Romejanic" label in the GUI code!

    Thanks!
    Romejanic
     
  2. ozbilgic

    ozbilgic

    Joined:
    Mar 21, 2017
    Posts:
    1
    Teşekkürler. Çok işime yaradı.
     
  3. niorg2606

    niorg2606

    Joined:
    May 27, 2017
    Posts:
    2
    Thank you very much! This is going to help out a lot :).
     
  4. Braulio-Almeida

    Braulio-Almeida

    Joined:
    May 6, 2015
    Posts:
    1
    OMG, this code helped me so much. Thank you!
     
  5. ismaelnascimento01

    ismaelnascimento01

    Joined:
    Apr 2, 2017
    Posts:
    17
    Very good !


     
  6. nahuel93

    nahuel93

    Joined:
    Sep 18, 2017
    Posts:
    2
    Works like a charm. Thanks!
     
  7. berk_can

    berk_can

    Joined:
    Feb 8, 2016
    Posts:
    3
    OMG, thank you so much, It would be amazing if there is a way to get all keys xD
     
  8. kenwrotethis

    kenwrotethis

    Joined:
    Oct 21, 2016
    Posts:
    4
    Cheers to you, Romejanic! Quite useful.
     
  9. howler123

    howler123

    Joined:
    May 7, 2017
    Posts:
    2
    Thanks
     
  10. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    21
    Thanks for sharing!
     
  11. digitalgreed

    digitalgreed

    Joined:
    Nov 11, 2013
    Posts:
    2
    what a great editor script--thanks!
     
  12. pjbaron

    pjbaron

    Joined:
    Jan 12, 2017
    Posts:
    9
    That's a really useful editor script!
    With this small modification you don't need to tell it what to look for when using the Get Key option.

    Code (CSharp):
    1.  
    2.         if (GUILayout.Button("Get Key"))
    3.         {
    4.             error = null;
    5.             setVal = "-";
    6.             int i;
    7.             float f;
    8.             string s;
    9.  
    10.             if (PlayerPrefs.HasKey(setKey))
    11.             {
    12.                 i = PlayerPrefs.GetInt(setKey, int.MinValue);
    13.                 if (i == int.MinValue)
    14.                 {
    15.                     f = PlayerPrefs.GetFloat(setKey, float.NaN);
    16.                     if (float.IsNaN(f))
    17.                     {
    18.                         s = PlayerPrefs.GetString(setKey, string.Empty);
    19.                         if (string.IsNullOrEmpty(s))
    20.                         {
    21.                             error = "Unknown type: " + setKey;
    22.                         }
    23.                         else
    24.                         {
    25.                             setVal = s.ToString();
    26.                             fieldType = FieldType.String;
    27.                         }
    28.                     }
    29.                     else
    30.                     {
    31.                         setVal = f.ToString();
    32.                         fieldType = FieldType.Float;
    33.                     }
    34.                 }
    35.                 else
    36.                 {
    37.                     setVal = i.ToString();
    38.                     fieldType = FieldType.Integer;
    39.                 }
    40.             }
    41.             else
    42.             {
    43.                 error = "No matching Key " + setKey;
    44.             }
    45.         }
    46.  
     
  13. shreyanshanchlia

    shreyanshanchlia

    Joined:
    Jun 7, 2018
    Posts:
    2
    Thanks so much..
     
  14. EGA

    EGA

    Joined:
    Sep 12, 2014
    Posts:
    27
    Thank you <3.