Game Directories

Essentials.Core.GameDirectories namespace is needed for this.

Game Directories allows you to create and visualize game folders without the need to write any code. It can be found in the Essentials >> Game Directories menu button.

Game Directories window

Creating Directories

You can create new directories by filling in the name or path and clicking the Create Directory button. If the button is greyed out, it means that the name or path is incorrect.

Nesting Directories

You can nest a directory inside another by specifying the path to that directory, for example My Folder/Nested Folder. If the My Folder directory does not exist, a new one will be created.

Directory References

To understand how to use Game Directories in code, you need to know about directory references. Each directory has its own directory reference which is used in code to reference the actual directory. You need to set these references in the Settings menu in the bottom left corner in the Game Directories window.

Game Directories Settings window

Using Directories In Code

Game Directories automatically, upon applying changes, generates all the needed code for the directories to be created and to function. To use them in code all you need to do is include Essentials.Core.GameDirectories namespace and reference the target directory using DirectoriesList.<DIRECTORY_REFERENCE> by default, which will return the path to the target directory.

Example Of Referencing MyFolder Directory

MyGameDataSaver.cs
using Essentials.Core.GameDirectories;
using UnityEngine;

public class MyGameDataSaver : MonoBehaviour
{
    private string _pathToDirectory;

    private void Start()
    {
        _pathToDirectory = DirectoriesList.MyFolder;
    }
}

It is a good practice to cache the reference when you are sure of that the directory will not get deleted during runtime, since each call to DirectoriesList actually checks whether that directory exists.

MyGameDataSaver.cs
using System.IO;
using Essentials.Core.GameDirectories;
using UnityEngine;

public class MyGameDataSaver : MonoBehaviour
{
    [SerializeField] private string[] _dataArray;

    private void Start()
    {
        SaveData();
    }

    private void SaveData()
    {
        // Good idea to actually cache the value before the loop so the GameDirectories system won't have to recheck the folder every time.
        string directoryPath = DirectoriesList.MyFolder;

        foreach (string data in _dataArray)
        {
            string currentData = File.ReadAllText(directoryPath);
            File.WriteAllText(directoryPath, currentData + data + "\n");
        }
    }
}

Modifying DirectoriesList Class

You can also modify the name of DirectoriesList class or even its location. All of that can be done in the Settings menu.

Last updated