Skip to content

Instantly share code, notes, and snippets.

@btarg
Created September 30, 2019 18:09
Show Gist options
  • Save btarg/20375380ebb66f63da688730e7db201c to your computer and use it in GitHub Desktop.
Save btarg/20375380ebb66f63da688730e7db201c to your computer and use it in GitHub Desktop.
Mod.io Unity: Load mods
using System.Collections.Generic;
using UnityEngine;
using ModIO;
using System.IO;
public class InitMods : MonoBehaviour
{
public string bundleName = "modbundle";
// Start is called before the first frame update
private void Start()
{
LoadMods();
}
public void LoadMods()
{
// Get all installed mods
List<string> mods = ModManager.GetInstalledModDirectories(true);
for (int i = 0; i < mods.Count; i++)
{
string mod = mods[i];
Debug.Log("Loaded mod " + Path.GetFileName(mod));
// Get path to asset bundle
string modpath = Path.Combine(mod, bundleName);
// Try to load mod from file
var loaded = AssetBundle.LoadFromFile(modpath);
if (loaded == null)
{
Debug.LogError("Failed to load mod " + mod);
return;
}
// Below is where you add your game-specific code. Use loaded.LoadAsset to get stuff from the asset bundle.
var prefab = loaded.LoadAsset<GameObject>("MyObject");
Instantiate(prefab);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment