Modding support

Question asked 100000 times but still cant find answer.
I want simple modding support, text files with data and some pictures so my question is:
How to make unity load resources (like data in text file or some pictures) that are just placed in mod folder in data folder or something like that. So player just edit text files to edit game or add pictures to mod folder so he has more to choose in game, i also want game to be made like that so native resources are accessible by player instead of those ugly unity assets file. So how to make game loads resources like that instead of unitys assets files?

Simple, you need to create your own resource importers or use assetbundles. I personally just set up a way to load the raw bytes of a file into memory and then use a callback to parse it. You will of course need to write all the functions to parse the data.

Example Loader:

static ExternalResources()
{
    m_WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
    if (m_WorkingDirectory == null || m_WorkingDirectory.Length < 1)
    {
        m_WorkingDirectory = Environment.CurrentDirectory;
    }
}

public static List<T> LoadResource<T>(string pDirectorySubPath, string[] pFileFormats, Func<string, string, byte[], T> pParse)
{
    List<T> results = new List<T>();
    string path = Path.Combine(m_WorkingDirectory, pDirectorySubPath);
    if (!Directory.Exists(path))
    {
        return results;
    }
    DirectoryInfo directoryInfo = new DirectoryInfo(path);
    for (int i = 0; i < pFileFormats.Length; ++i)
    {
        FileInfo[] files = directoryInfo.GetFiles(pFileFormats[i], SearchOption.AllDirectories);
        for (int j = 0; j < files.Length; ++j)
        {
            byte[] bytes = File.ReadAllBytes(files[j].FullName);
            if (bytes != null && bytes.Length > 0)
            {
                T result = pParse(directoryInfo.FullName, files[j].FullName, bytes);
                if (result != null)
                {
                    results.Add(result);
                }
            }
        }
    }
    return results;
}

Unity can already parse png and jpg images into a texture with LoadImage.
Example usage:

public string TexturePath = "Textures/";
public string[] ImageFormats = new string[] { "*.jpg", "*.jpeg", "*.jpe", "*.jif", "*.jfif", "*.jfi", "*.png", };

List<Texture2D> textures = ExternalResources.LoadResource(TexturePath, ImageFormats, (d, fp, b) =>
{
    Texture2D texture = new Texture2D(2, 2);
    texture.LoadImage(b);
    texture.name = fp.Substring(d.Length);
    return texture;
});

P.S. It would be wonderful for people that want to allow mods if Unity had an interface to all the existing importers in the engine.

2 Likes