Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    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
     
    KarlN, berk_can, nahuel93 and 3 others like this.
  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:
    12
    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:
    11
    Thanks for sharing!
     
  11. digitalgreed

    digitalgreed

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