
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#. If(GUI.Button(Rect(120, Screen.Unity provides the class JsonUtility and static methods for working with JSON. Music = gameObject.GetComponent(AudioSource) Hi guys, i've made this music player, the songs are added from unity editor, how can i do for create a directory folder, so that the songs that must be reproduced are taken from there? #pragma strict
