MonoBehaviour Mod Extensions

All mods are based on the MonoBehaviour class. Additional methods can be defined on your mod to handle various actions.

You can have as many or as few of these, none of these are technically required, you can use all the normal methods available via a MonoBehaviour, however, it is recommended you take advantage of the AzzaMods APIs.

These APIs have exception handling built in, any exceptions will automatically be caught and pushed to the Launcher logging tab.

OnModLoaded

void OnModLoaded()

This is called each time your mod is loaded.

It is recommended your register options here, do patching and start to load assets.

// Some options
private static string optionMyOption = "My Option";
private static string actionMyAction = "My Action";

void OnModLoaded()
{
    // Your mod is now loaded
    AzzaMods.Log.Info("Hello from ExampleMod");

    // Register an option
    AzzaMods.Options.RegisterBool(optionMyOption, false);
    AzzaMods.Options.RegisterAction(actionMyAction);
}

OnModUnloaded

void OnModUnloaded()

This is called when your mod is unloaded. You need to undo what ever your mod did if it made any major changes to the game. Think about what you'd expect to happen if you turned off a mod, that should happen here.

Options are automatically unregistered and patches are automatically removed if applied via the Patching APIs.

void OnModUnloaded()
{
    // Undo everything, your mod is being unloaded

    // Options are automatically unregistered
    // Patches via the AzzaMods patching system are automatically unhooked
}

OnOptionChanged

void OnOptionChanged(string optionName)

This is called when an option is changed, this could include:

  • A new option is registered
  • The value of an option is changed
  • The state (e.g. lock / toggle) of an option is changed
// An option
private static string optionMyOption = "My Option";

void OnOptionChanged(string optionName)
{
    // Handle the option changing
    if(optionName == optionMyOption)
    {
        AzzaMods.Log.Info("My Option was changed to " + AzzaMods.Options.GetBool(optionName));
    }
}

OnSceneChanged

void OnSceneChanged(string oldScene, string newScene)

This is called each time the scene is changed.

void OnSceneChanged(string oldScene, string newScene)
{
    AzzaMods.Log.Info("Scene was changed from " + oldScene + " to " + newScene);
}

OnAction

void OnAction(string actionName, string actionType)

This is called when the user clicks on a button that was registered via the Options.RegisterAction API.

// An option
private static string actionMyAction = "My Action";

void OnAction(string actionName, string actionType)
{
    // Which action was executed?
    if (actionName == actionMyAction)
    {
        AzzaMods.Log.Info("You executed my action!");
    }
}

OnBundleLoaded

void OnBundleLoaded(string bundleName, object bundle)

This is called when an asset bundle finishes loading.

You can load asset bundles via the Assets APIs.

void OnBundleLoaded(string bundleName, object bundle)
{
    // Cast to an asset bundle
    AssetBundle thisBundle = (AssetBundle)bundle;

    // Check which asset bundle was loaded
    if (bundleName == "newselection")
    {
        Object prefabNewSelection = thisBundle.LoadAsset("AzzaFishSelection");

        // Consider checking if all assets are loaded, or doing something with the asset
    }
}

OnDataLoaded

void OnDataLoaded(string fileName, byte[] loadedData)

This is called when a binary file finishes loading.

You can load binary files via the Assets APIs.

void OnDataLoaded(string fileName, byte[] loadedData)
{
    // Check which asset bundle was loaded
    if (fileName == "somefile.txt")
    {
        // Do something with the loadedData
        Log.info("Loaded a binary file with the contents: " + System.Text.Encoding.UTF8.GetString(loadedData));
    }
}