Logging player actions and game events to a file
The comma separated values (CSV) text file format is a good format to save data in, since it is easily read into rows and columns by almost all spreadsheet applications.
In this recipe, we’ll write a script that offers a method that can be called from anywhere in our game, from any scene, which will add a row of text to a log file. The following is an example of the saved data when viewed in Microsoft Excel.
Figure 10.14: CSV log data viewed in a spreadsheet application
How to do it...
To log player actions and game events to a file, follow these steps:
- Create a new 2D project.
- Create a new
AddToLogFile C#
script class containing the following code:using UnityEngine; using System.IO; using System; using UnityEngine.SceneManagement; public class AddToLogFile : MonoBehaviour { private static string _fileName = ""; private static string _folderName = "Logs";...