Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by jackd5011, Nov 27, 2015.

  1. jackd5011

    jackd5011

    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. SixBeeps

    SixBeeps

    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. ismaelnascimentoash

    ismaelnascimentoash

    Joined:
    Apr 2, 2017
    Posts:
    30
    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:
    15
    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:
    17
    Thanks
     
  10. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Thanks for sharing!
     
  11. davidleefox

    davidleefox

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

    pjbaron

    Joined:
    Jan 12, 2017
    Posts:
    53
    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.  
     
    ferverence and vitaliy-ostrovsky like this.
  13. shreyanshanchlia

    shreyanshanchlia

    Joined:
    Jun 7, 2018
    Posts:
    21
    Thanks so much..
     
  14. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
  15. Untherow

    Untherow

    Joined:
    Jun 19, 2016
    Posts:
    8
    Great script! Thanks very much!
     
  16. RafaelKuhn

    RafaelKuhn

    Joined:
    Dec 5, 2019
    Posts:
    3
    Great tool!
     
  17. ElChileVengador

    ElChileVengador

    Joined:
    Feb 4, 2013
    Posts:
    27
    Amazing, this works great! You rock
     
  18. Blackflioww

    Blackflioww

    Joined:
    Aug 8, 2021
    Posts:
    1
    It is 2022, and still awesome tool, thank you a lot!