- Home /
PlayerPrefs.DeleteAll() not working in Editor
I tried to build my game and let it run at a fixed resolution.
I changed the setting on Project Setting/Player, but it didn't work. I searched the Internet and found the solution (How to set a Custom Unity Fullscreen Resolution). As I'm using Windows10, Unity saves a registry key in the system. I have to clear the registry key, then the new setting would function well.
Then I found that "PlayerPrefs.DeleteAll()" can help me clear the registry key. I use "PlayerPrefs.DeleteAll()" in game runtime, it works pretty well.
But if I use "PlayerPrefs.DeleteAll()" in Editor (Unity Answers - Why doesn't Standalone Build Resolution settings...), it doesn't work. I checked the registry key, nothing changed.
can you create a new script and add this lines in the awake event for example? just for making sure that the issue is with delete all and not with the code re-registring the key after getting deleted
PlayerPrefs.SetFloat("Unityyyyyy", 5);
Debug.Log(PlayerPrefs.GetFloat("Unityyyyyy"));
PlayerPrefs.DeleteAll();
Debug.Log(PlayerPrefs.GetFloat("Unityyyyyy"));
The issue is with delete all.
It seems that new options will not function unless old options have been deleted. So I want to delete all the old options.
I tired your code in my project.
I put them in Awake(). I run the game in Unity Editor (Game View of Unity Editor), getting output 5 and 0.
I put them in editor script (just like the way in Unity Answers - Why doesn't Standalone Build Resolution settings...), getting output 5 and 0.
I also put them in Awake() of a builded game. But I do not know where to see the output.
I re$$anonymous$$ds that, maybe Unity stores registry keys of Unity Editor and builded game in different paths. Using your code I may find the paths.
I change the code, drop most of them, only keep the first line.
I put PlayerPrefs.SetInt("Unityaaaa", 5);
in Awake(), run the game in Unity Editor. I put PlayerPrefs.SetInt("Unitybbbb", 5);
in editor script, run it. I put PlayerPrefs.SetInt("Unitycccc", 5);
in Awake(), then run in a builded game.
Then I search "Unityaaaa" "Unitybbbb" "Unitycccc" in regedit of Windows10. "Unityaaaa" "Unitybbbb" are in H$$anonymous$$EY_CURRENT_USER\Software\Unity\UnityEditor\[company name]\[product name] , "Unitycccc" is in H$$anonymous$$EY_CURRENT_USER\Software\\company name]\[product name].
Very happy seeing things are clear here. Unity stores registry keys of Unity Editor and builded game in different paths. registry keys of Unity Editor are in H$$anonymous$$EY_CURRENT_USER\Software\Unity\UnityEditor\[company name]\[product name]. registry keys of builded game are in H$$anonymous$$EY_CURRENT_USER\Software\[company name]\[product name]. So, if I run PlayerPrefs.DeleteAll() in Unity Editor, registry keys of builded game are not deleted.
Answer in Unity Answers - Why doesn't Standalone Build Resolution settings... works well for many guys, but it is written in 2014. I guess Unity has changed the paths ever since, so the answer not works for me now.
Answer by djnX · Aug 07, 2019 at 04:28 AM
With help from @xxmariofer I may have found the reason.
I'm using Unity 2019.3.0a8 on Win10.
It seems Unity stores registry keys of game running in Unity Editor and game running as standalone in different paths. If I run PlayerPrefs.DeleteAll()
in Unity Editor, registry keys of standalone are not deleted.
registry keys of Unity Editor are in HKEY_CURRENT_USER\Software\Unity\UnityEditor\[company name]\[product name]. registry keys of standlone are in HKEY_CURRENT_USER\Software\[company name]\[product name].
makes sense since the build version should be totally independent to the editor version, do you still need to find a solution to this issue? you will need to create your own playerprefs if you need share key location, i was testing just in case if you added a \ to the key name if you could edit the directory of the keys but no its not posible it would just add the \ to the name. what i suggest is creaating "your own player prefs" and share it accross build and editor you can use the using $$anonymous$$icrosoft.Win32.Registry$$anonymous$$ey for creating keys, checking if keys exist etc...
yes, the issue is stilled somehow unsolved.
if I want to delete playerprefs, I have to open regedit and delete manually. Or I can put a button in the game, then delete by click the button in a build version. Either way is kind of wired and inconvenient.
Thank you for your suggestion about $$anonymous$$icrosoft.Win32.Registry$$anonymous$$ey. So I can put a menu item in Editor and then edit keys of build version.
Actually I'm quite new to Unity, many things seem unclear to me. I am considering using my own files as playerprefs, maybe a .json file. Then I may have to control resolution with code. If you have any suggestions about this, very glad to know.
(Another Question. I don't quite understand the pros of using registry on Windows. I think using an option file is much more convenient. But there must be some pros, so many softwares on earth are using registry. What's your opinion?)
well, i think that one of the advantages (and i think the main) of usiung windows key registry is that if you want to write to for example Program Files and edit the config you would need to open the app always as an ad$$anonymous$$istrative, also when deleting the game all configuration would get deleted if you use a subdirectory, but whne using the registry you would save it in case they re-install it, i cant think of other advantages but maybe there are. also PlayerPrefs is available across platforms, when you use it with a mobile target for example all player prefs getwritten to a json in a directory. Retrieving data from a json is fairly common and there are easy well-written tutorials that will help you more than me for this porpuse, check this official unity tutorial https://unity3d.com/es/learn/tutorials/topics/scripting/loading-game-data-json