从偏好中删除所有键和值。请谨慎使用。
Call this function in a script to delete all current settings in the PlayerPrefs.
The following example demonstrates creating a button that deletes all PlayerPrefs.
//This example creates a button on the screen that deletes any PlayerPrefs settings when you press it. //Note that you must set values or keys in the PlayerPrefs first to see the button.
using UnityEngine;
public class Example : MonoBehaviour { void OnGUI() { //Delete all of the PlayerPrefs settings by pressing this button. if (GUI.Button(new Rect(100, 200, 200, 60), "Delete")) { PlayerPrefs.DeleteAll(); } } }
The following example demonstrates setting PlayerPrefs and deleting them afterwards.
//First attach this script to a GameObject in the Scene to set up the PlayerPrefs.
using UnityEngine; using UnityEngine.SceneManagement;
public class SetUpPlayerPrefsExample : MonoBehaviour { string m_PlayerName;
void Start() { m_PlayerName = "Enter Your Name"; }
void Update() { //Give the PlayerPrefs some values to send over to the next Scene PlayerPrefs.SetFloat("Health", 50.0F); PlayerPrefs.SetInt("Score", 20); PlayerPrefs.SetString("Name", m_PlayerName); }
void OnGUI() { //Create a Text Field where the user inputs their name m_PlayerName = GUI.TextField(new Rect(10, 10, 200, 20), m_PlayerName, 25);
//Create a button which loads the appropriate level when you press it if (GUI.Button(new Rect(10, 30, 200, 60), "Next Scene")) { SceneManager.LoadScene("Scene2"); } } }
//This other script shows how the values of the PlayerPrefs reset using the PlayerPrefs.DeleteAll() function. //Open a different Scene (the one you named before- "Scene2") and attach this script to a new GameObject. //Use this script to fetch the settings and show them as text on the screen. //Use the button included in the script to delete all these settings and the text on the screen will also reset to reflect this.
using UnityEngine; using UnityEngine.UI;
public class PlayerPrefsDeleteAllExample : MonoBehaviour { int m_Score; float m_Health; string m_PlayerName;
void Start() { //Fetch the PlayerPref settings SetText(); }
void SetText() { //Fetch the score, health and name from the PlayerPrefs (set these Playerprefs in another script) m_Health = PlayerPrefs.GetFloat("Health", 0); m_Score = PlayerPrefs.GetInt("Score", 0); m_PlayerName = PlayerPrefs.GetString("Name", "No Name"); }
void OnGUI() { //Fetch the PlayerPrefs settings and output them to the screen using Labels GUI.Label(new Rect(50, 50, 200, 30), "Name : " + m_PlayerName); GUI.Label(new Rect(50, 90, 200, 30), "Health : " + m_Health); GUI.Label(new Rect(50, 130, 200, 30), "Score : " + m_Score);
//Delete all of the PlayerPrefs settings by pressing this Button if (GUI.Button(new Rect(50, 0, 100, 30), "Delete")) { PlayerPrefs.DeleteAll(); //Fetch the updated settings to change the Text SetText(); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.