Monday, August 28, 2017

Using Player Prefs on Unity outside play mode

Often I like to use Player Prefs to store user data, because is really easy to use. But what happens when we want to delete it?. I used to put this line of code:

PlayerPrefs.DeleteAll();

Complite, execute, erase line of code, compile and execute again.

Now I know better!, so we are going to setup a Menu Item to execute this at any time.

Lets create a new C# script, name it  DeletePlayerPrefsData  and add the following code:

using UnityEngine;
using UnityEditor;

public class DeletePlayerPrefsData {
[MenuItem("Assets/Delete Player Prefs")]
public static void DeletePlayerPrefs() {
PlayerPrefs.DeleteAll();
}
}

Here we put the location of the script function on the menu.
[MenuItem("Assets/Delete Player Prefs")]

Lastly we declare a static function just below  [MenuItem]  which would be the function that will execute when  Delete Player Prefs  is clicked.



No comments:

Post a Comment

Usando Player Prefs fuera del modo de juego

Muchas veces usamos la clase Player Prefs para guardar datos. Pero que pasa cuando queremos borrar los datos guardados?. Yo solia colocar e...