It uses the ToJson() and FromJson() for these tasks. The class JsonUtility handles the work of serializing and deserializing data into and out of a JSON format. The class GameData contains values and the class GameDataManager “manages” these values, working with File to read and write to a file. The new GameData class does not need to be complex, as it will only hold values. Right-click on the file to rename it something other than its default name. Note: A new C# script file can be created in the Project View through Create -> C# Script. A new class, GameData, will serve this purpose. It is often useful to create a class whose purpose is to hold a collection of values used by other classes or processes. String fileContents = File.ReadAllText(saveFile) Read the entire file and save its contents. Update the path once the persistent path exists. They either read or write to a file and handle the opening and closing internally. The methods ReadAllText() and WriteAllText() collapse two of those steps. When working with files, the normal series of steps is to open a file, work with it, and then close it. SaveFile = Application.persistentDataPath + "/gamedata.json" Update the field once the persistent path exists. Create a field of this class for the file. Public class GameDataManager : MonoBehaviour As the actions of “reading data” and “writing data” are separate actions, these should also be separate methods of the class. To help better organize classes, a new, named class called GameDataManager can be created to “manage” the data eventually to be used with JsonUtility and read and saved using FileStream. This could mean it was deleted or has not been created yet. String saveFile = Application.persistentDataPath + "/gamedata.data" When working with Application.persistentDataPath, this is a good first step to identifying if a file exists before attempting to read it (which would fail) or if it needs to be created for the first time. The class File also provides the method Exists() for checking if a specific files exists. Because users, systems, and processes can edit files, this verifies a file exists before an attempt to read or otherwise work with it is done. The first step in working with files should always be to check if they exist. To use a file, an additional filename is needed such as the above example where a “/” and the name of the file is used. The field Application.persistentDataPath only contains a directory. This is set when the project starts and can only be used as part of the Initialization System within Unity as part of messages such as the method Awake() or Start() within a Scripting Component.Īpplication.persistentDataPath + "/gamedata.json" While a Unity project is running, it provides a field called Application.persistentDataPath set with the current “persistent” directory. When creating a new Scripting Component in Unity, the namespace will need to be added. Note: File is part of the System.IO namespace, a collection of classes and methods connected to working with input (“I”) and output (“O”). As the action of needing to read text files is a common one, C# provides two useful methods for working with them as part of File class: ReadAllText()and WriteAllText(). The C# class File provides methods for accessing files without knowing if the files are locally or remotely saved. Like BinaryFormatter, the class JsonUtility can be combined with other classes like File to read and write files, but also comes with the same warning: files on a user’s system, because they can be edited by the user, system, or other processes, should not be considered trusted sources. These provide a way to “serialize” and “deserialize” to and from JSON based on simple data structures in C#. So I made a script in Unity and name it SaveData.cs.Unity provides the class JsonUtility and static methods for working with JSON. For further information you can access unity documentation ( ). Unity provide a class that can be used to serialized a class into JSON file or otherwise, the class is JsonUtility. Potion name has value and the value is magic potion, value has a number value, effect has an array object value. Values, A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. In the sample above, you can see an array start at line 4 until line 13. (source: ).Īrray, An array is an ordered collection of values. Each name is followed by : colon and the name/value pairs are separated by, comma. Object, An object is an unordered set of name/value pairs.
0 Comments
Leave a Reply. |