How to make a File System?

I’m making a game inside of a simulated computer. I’ve made the basis for the UI and now it’s time to make the File System. I need a system that allows files to be moved at runtime. I had a few ideas on how to implement this, but they all had flaws:


First, I thought about using the Resources folder itself as the File System but there’s no way to move files around from script (and even if there was, after building this folder wouldn’t exist so it wouldn’t work there).


Then I thought about creating the File System on a MonoBehaviour and attaching it to a gameObject that I would then save as a prefab. The problem here is that there’s no type that can represent every type of file (TextAsset, Sprite, AudioClip, etc.). I created an abstract file class and then create a subclass for each type of file but by doing this I lose the ability to see the file variables in the inspector. To load this class I use the Resources.LoadAll method for each type of asset I want to keep.


[System.Serializable]
public class FileSystem
{
    [SerializeField]
    public List<File> files;

    public FileSystem()
    {
        files = new List<File>();
    }
}

[System.Serializable]
public abstract class File
{
    public File(string name)
    {
        this.name = name;
    }

    public string name;
}

[System.Serializable]
public class TextFile : File
{
    public TextFile(string name, TextAsset asset) : base(name)
    {
        this.asset = asset;
    }

    public TextAsset asset;
}

[System.Serializable]
public class ImageFile : File
{
    public ImageFile(string name, Sprite asset) : base(name)
    {
        this.asset = asset;
    }

    public Sprite asset;
}

Is there a better way of doing this that I’m missing?

For anyone interested, this is what I ended up using:

[System.Serializable]
public class OSFileSystem
{
    [SerializeField] private string homePath;
    [SerializeField] private List<OSFolder> folders;
}

[System.Serializable]
public class OSFolder : IComparable<OSFolder>
{
    public string folderPath;
    public List<UnityEngine.Object> files;
    public int subfolderIdx = -1;
}

I use a big list of Folders for the file system. For the Folders, I just sticked with the UnityEngine.Object class, and at the end it isn’t that bad.

I still need to construct every folder manually, but with some custom unity editors and a validation/sorting method it gets easier to work with.