Unity3d game status saves: BINARYFORMATTER, PlayerpRefs and JSON comparison
When developing games in Unity, it is important to save the progress of the game. It is important to find an efficient and reliable game data storage method. BINARYFORMATTER, PlayerPrefs and JSON are commonly used.
Initially, BINARYFORMATTER was often used in data serialization, but it had some potential problems. In some devices, it may encounter problems, especially when updating or modifying classes. The non -compatibility between the old settings and the updated class may cause data loss. In addition, on iOS, using BinaryFormatter needs to set the environment variable "Mono_reflection_Serializer" to "Yes".
PlayerPrefs is another choice, but it is mainly used to store data such as high scores or game settings. Its limited function and binary format make it less suitable for complex or sensitive data.
JSON: Flexible and cross -platform solution
JSON (JavaScript object) became the first choice to preserve the status of the game. It is a text -based format that makes it easy to read and is compatible with platforms. JSON can represent complex data structures, objects, and arrays in a structured and portable manner.
converted to byte array
Although JSON needs a string format, your game requires byte array. For this reason, just use Encoding.ascii.getBytes () to convert the json string into byte array. You can then use File.Writeallbytes and File.readallBytes to save and load the byte array.
The data is used to save the data
In order to facilitate use, you can use generic categories such as DataSaver. It provides a method for saving and loading, and can operate any type of object.Conclusion
When saving the game data in Unity, due to the flexibility, reliability and cross -platform compatibility of JSON, it is better than the BINARYFORMATTER and becomes a recommended method. By converting the JSON string into byte array, you can meet your specific needs while maintaining the advantage of JSON. The use of generic data saving classes can further simplify the process.
The above is the detailed content of Which Unity3D Game State Saving Method is Best: BinaryFormatter, PlayerPrefs, or Json?. For more information, please follow other related articles on the PHP Chinese website!