Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c# - How to get all PlayerPrefs keys to make a Drop() function?

I have a problem with Drop function in my game. I need to set all my variables to 0/null, but to make a string to each one separately is very time consuming and costly in the code. Maybe know someone how to get an array of keys in PlayerPrefs?

public void Drop()
{
    PlayerPrefs.SetInt("AntivirusLevel", 0);
    PlayerPrefs.SetFloat("CPU", 0);
    PlayerPrefs.SetInt("CPUlvl", 0);
    PlayerPrefs.SetFloat("GPU", 0);
    PlayerPrefs.SetInt("GPUlvl", 0);
    PlayerPrefs.SetInt("RAM", 0);
    PlayerPrefs.SetInt("HDD", 0);
    PlayerPrefs.SetInt("MB", 0);
    PlayerPrefs.SetInt("Level", 0);
    PlayerPrefs.SetInt("LastScore", 0);
    PlayerPrefs.SetString("Training", null);
    PlayerPrefs.Save();
    SceneManager.LoadScene(0);
}
question from:https://stackoverflow.com/questions/65836198/how-to-get-all-playerprefs-keys-to-make-a-drop-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no built-in way to get a list of all existing PlayerPrefs in Unity (moreover you also need to each one's type).

There is a plugin that can do that in the editor, maybe you can use it and modify it to work in runtime as well: https://assetstore.unity.com/packages/tools/playersprefs-editor-and-utilities-26656

A second alternative would be to manually search the place where the prefs are saved (in windows its in the registery, for example). You can see the location the prefs are saved in each platform here: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Last, and this would be my suggestion - you can use PlayerPrefs.DeleteAll() to delete all player prefs. However, this will delete them and not set a default value like in your code, so you will have to change the code that accesses the player prefs and provide it with a default value.

public void Drop()
{
    PlayerPrefs.DeleteAll(); // Remove all keys so we can get their default values
    PlayerPrefs.Save();
    SceneManager.LoadScene(0);
}

public int MyGetInt(string prefName) {
    return PlayerPrefs.GetInt(prefName, 0);
}

public float MyGetFloat(string prefName) {
    return PlayerPrefs.GetFloat(prefName, 0f);
}

private void Update() {
    Debug.Log("CPU Value: " + MyGetFloat("CPU")); // When using this instead of directly PlayerPrefs.GetFloat, then it will return 0 if the value doesn't exist, including after Drop() was run
}

Note that this will delete ALL your keys in the game. If you only want to reset a certain list of keys, there is no avoiding making a list of it in a JSON file, array, or some other structure. The program can not guess which values you want to reset and which not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...