Unity study notes–the use of PlayerPrefs for data persistence
Unity study notes–Usage of PlayerPrefs for data persistence Data persistence PlayerPrefs related PlayerPrefs is a class in the Unity game engine used to store and access player preferences and data in the game. It can be used to save the player’s game progress, setting options, highest scores and other information. PlayerPrefs stores data in local files so data is persisted across game restarts. //PlayerPrefs data storage is similar to key-value pair storage, one key corresponds to one value //Provides methods to store 3 types of data int float string //Key: string type //Value: int float string corresponds to 3 APIs PlayerPrefs.SetInt(“myAge”, 18); PlayerPrefs.SetFloat(“myHeight”, 177.5f); PlayerPrefs.SetString(“myName”, “TonyChang”); //Directly calling Set-related methods will only store the data in memory. //When the game ends, Unity will automatically save the data to the hard disk. //If the game does not end normally but crashes, the data will not be saved to the hard disk. //As long as this method is called, it will be stored in the hard disk immediately. PlayerPrefs.Save(); //PlayerPrefs is limited. It can only store 3 types of data. //If you want to store other types of data, you can only reduce the precision or increase the precision for storage. bool…