Saving external text files with C# file streams

This recipe illustrates how to use C# streams to write text data to a text file, either into the standalone project's Data folder or to the Resources folder.

Note

This technique only works when you compile to a Windows or Mac standalone executable.

Getting ready

In the 0423_08_02 folder, we have provided the FileReadWriteManager.cs class script file:

How to do it...

To save external text files using C# file streams, follow these steps:

  1. Import the C# script FileReadWriteManager.cs into your project.
  2. Add the following C# script to Main Camera:
    // file: SaveTextFile.cs
    using UnityEngine; 
    using System.Collections;
    using System.IO;
    
    public class SaveTextFile : MonoBehaviour {
        public string fileName = "hello.txt";
        public string folderName = "Data";
        private string filePath = "(no file path yet)";
        private string message = "(trying to save data)";
        private FileReadWriteManager fileManager;
    
        void Start () {
            fileManager = new FileReadWriteManager();
            filePath = Application.dataPath + Path.DirectorySeparatorChar + fileName;
    
            string textData = "hello 
     and goodbye";
            fileManager.WriteTextFile( filePath, textData );
    
            message = "file should have been written now ...";
        }
    
       void OnGUI() 
       {
            GUILayout.Label("filepath = " + filePath);
            GUILayout.Label("message = " + message);
        }
    }
  3. Build and run your (Windows or Mac) standalone executable. You'll need to save the current scene and then add this to the scenes in the build.
  4. You should now find a new text file named hello.txt in the Data folder of your project's standalone files, containing the lines "hello" and " and goodbye".

Note

It is possible to test this when running within the Unity editor (that is, before building a standalone application). In order to test this way, you'll need to create a Data folder in your project panel.

How it works...

When the game runs, the Start() method creates the filePath string from the public variables fileName and folderName, and then calls the WriteTextFile() method from the fileReadWriteManager object, to which it passes the filePath and textData strings. This method creates (or overwrites) a text file (for the given file path and filename) containing the string data received.

There's more...

Some details you don't want to miss:

Choosing the Data or the Resources folder

Standalone build applications contain both a Data folder and a Resources folder. Either of these can be used for writing (or some other folder, if desired). We generally put read-only files into the Resources folder and use the Data folder for files that are to be created from scratch or have had their contents changed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.225