Search Unity

How to delete PlayerPrefs Saves using the editor?

Discussion in 'Scripting' started by milos_nov, Mar 9, 2019.

  1. milos_nov

    milos_nov

    Joined:
    Jun 2, 2017
    Posts:
    3
    A friend of mine was tired of commenting in and out to delete all of his saves so he can do testing.
    So I decided to make his life a little easier and share this:

    Script 1:
    Drag this in any game object


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class Jumping : MonoBehaviour
    7. {
    8.     public void DeleteSaves()
    9.     {
    10.         PlayerPrefs.DeleteAll();
    11.         PlayerPrefs.Save();
    12.     }
    13. }
    Script 2:
    Warrning: Do not drag this in, Just save it!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(Jumping))]
    7. public class PlayerPrefsEditor : Editor
    8. {
    9.     public override void OnInspectorGUI()
    10.     {
    11.         base.OnInspectorGUI();
    12.  
    13.         Jumping script = (Jumping)target;
    14.  
    15.         DrawDefaultInspector();
    16.  
    17.         if (GUILayout.Button("Delete Saves"))
    18.         {
    19.             script.DeleteSaves();
    20.         }
    21.     }
    22. }
    23.  
    Wait for it to compile!
    Tip: You don't have to be in play mode for the button to work.

    Cheers!
    Have fun making games!
     
    Last edited: Mar 9, 2019
    Geejayz and Ryiah like this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Good one.

    Here's a trick that may save some time: you don't need to make a custom editor just to execute a bit of script in the Inspector. You can instead use [ContextMenu("Some Command")], as shown here. You could put that in the first script, right on the DeleteSaves method, and not need the second script at all. Then you'd use it by clicking the little gear icon on the Jumping component, and picking your command from the menu.
     
    Geejayz, SparrowGS, sharkapps and 2 others like this.
  3. milos_nov

    milos_nov

    Joined:
    Jun 2, 2017
    Posts:
    3
    Huh, didn't know about that.
    Your method is wayy better.

    Anyone out there that is too lazy to click on his link:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ContextTesting : MonoBehaviour
    4. {
    5.     /// Add a context menu named "Do Something" in the inspector
    6.     /// of the attached script.
    7.     [ContextMenu("Do Something")]
    8.     void DoSomething()
    9.     {
    10.         Debug.Log("Perform operation");
    11.     }
    12. }
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    It also doesn't have to be on a MonoBehaviour. You can tag static functions with [MenuItem(...)] and they'll appear in the Unity Editor menu at the path you specified. This means you can add your own menus with functions specific to your game!

    The example here is in a MonoBehaviour, but it doesn't have to be.
     
    JoeStrout likes this.
  5. Deleted User

    Deleted User

    Guest

    Um ok but why though??
     

    Attached Files:

    jamesrfinch1 likes this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Well, that wasn't always there, and you might not want to change engine versions on a legacy project just for that.

    Also, people might not want to delete all PlayerPrefs, so writing their own script to clear just relevant ones could be useful.

    Also, the method disucssed is useful for plenty of things aside from this specific task.
     
    elenzil, milos_nov and Deleted User like this.
  7. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    There is a simple way ..Edit>Clear All PayerPrefs
     
  8. Letters99

    Letters99

    Joined:
    Feb 11, 2020
    Posts:
    3
    Yes but it doesnt appear on versions 2017 and earlier.