ModAssets class
Loads Unity asset bundles shipped with a mod, on both Mono and IL2CPP:
var bundle = Assets.LoadBundle("Assets/ui.bundle");
var panel = bundle.Load<GameObject>("Panel");This also works in IL2CPP games whose builds do not include Unity's AssetBundle class, because Lustral calls the engine directly. Bundles must be loaded on the main thread, from Mod.OnGameReady onward.
public sealed class ModAssetsRemarks
Build bundles with the game's Unity version or a close version; Unity cannot load bundles built with a newer version. Ship bundles in the mod's folder (<None Include="Assets\**" CopyToOutputDirectory="PreserveNewest" /> in the project file), or embed them in the mod's assembly and load them from bytes.
Methods
LoadBundle(string)
public ModAssetBundle LoadBundle(string path);Loads an asset bundle file. Relative paths are resolved against the mod's folder (ModInfo.Directory). Loading a file that is already loaded returns the same bundle, because Unity loads each bundle only once.
Exceptions
FileNotFoundException: The file does not exist.InvalidOperationException: Unity could not load the bundle (the game's log,Player.log, contains the reason), or the method was called before the game was ready or outside the main thread.
LoadBundle(byte[])
public ModAssetBundle LoadBundle(byte[] data);Loads an asset bundle from memory, such as a bundle embedded in the mod's assembly: Assets.LoadBundle(ReadResource("MyMod.ui.bundle")).
Exceptions
InvalidOperationException: Unity could not load the bundle (the game's log,Player.log, contains the reason), or the method was called before the game was ready or outside the main thread.