Overview

The Madder Manager is used to interface between the WebGL build and the Unity game. The Madder Manager is a Singleton class that interacts with the Madder Controller Manager and manages Madder Controller States.

Usage

Integrating Madder with Unity

The Madder Starter Pack contains two prefabs called MadderManager and MadderControllerManager that must be placed in the scene.

Because the Madder Manager is a Singleton class, it will persist across scene loads.

RegisterMadderController

The RegisterMadderController method is called by the WebGL build when a player successfully joins using the room code. RegisterMadderController registers a Madder Controller with the Unity Input System. The jsonRegisterMadderController parameter is a MadderPlayer object serialized as a JSON string.

For information on how to set up the Madder Controller with the Unity Input System, see the Madder Controller class.

You may subscribe to the OnRegisterMadderController event to listen for when a Madder Controller is registered, for example to spawn a Player GameObject.

MadderManager.cs
public delegate void OnRegisterMadderController(MadderPlayer madderPlayer);
public static event OnRegisterMadderController onRegisterMadderController;
public void RegisterMadderController(string jsonRegisterMadderController)
{
    //Deserialize the MadderPlayer object
    MadderPlayer madderPlayer = JsonUtility.FromJson<MadderPlayer>(jsonRegisterMadderController);

    // Create a new input device for the player
    madderControllerManager.CreateController(madderPlayer.name);

    //Trigger events listening for OnRegisterMadderController, for example to spawn a playerObject
    onRegisterMadderController?.Invoke(madderPlayer);
}

UpdateMadderControllerState

The UpdateMadderControllerState method is called by the WebGL build when a player performs inputs on their device. UpdateMadderControllerState updates the Unity Input System with the input from the player’s controller. The jsonUpdateMadderControllerState parameter is a MadderControllerState object serialized as a JSON string.

If you are not using the Unity Input System, you may listen to the OnUpdateMadderControllerState event to interact with the Madder Controller State. This event provides the gamername of the player controller and the Madder Controller State so you can update your game and players accordingly.

MadderManager.cs
public delegate void OnRegisterMadderController(MadderPlayer madderPlayer);
public static event OnRegisterMadderController onRegisterMadderController;
public void UpdateMadderControllerState(string jsonControllerState)
{
    //Turn the booleans in the json string to floats for the Unity Input System
    jsonControllerState = jsonControllerState.Replace("true", "1");
    jsonControllerState = jsonControllerState.Replace("false", "0");
    //Deserialize the controller state and store it in a MadderControllerState object
    MadderControllerState controllerState = JsonUtility.FromJson<MadderControllerState>(jsonControllerState);
    // Retrieve the corresponding input device. The controller name is used as the device name, and is always unique
    MadderController controller = madderControllerManager.GetController(controllerState.name);

    //Update the input device with the received data
    InputSystem.QueueStateEvent(controller, controllerState);

    //Trigger events listening for OnUpdateMadderControllerState, for example to move a playerObject
    onUpdateMadderControllerState?.Invoke(controllerState.name, controllerState);
}

UnregisterMadderController

The UnregisterMadderController method is called by the WebGL build when a player leaves the lobby. UnregisterMadderController unregisters a Madder Controller from the Unity Input System. The gamername parameter is the gamername of the player controller being removed.

You may subscribe to the OnUnregisterMadderController event to listen for when a Madder Controller is unregistered, for example to remove a Player GameObject.

MadderManager.cs
public delegate void OnUnregisterMadderController(string gamername);
public static event OnUnregisterMadderController onUnregisterMadderController;
public void UnregisterMadderController(string gamername)
{
    madderControllerManager.RemoveController(gamername);
    onUnregisterMadderController?.Invoke(gamername);
}

GetMadderController

The GetMadderController method is used to retrieve a Madder Controller by gamername. The gamername parameter is the gamername of the player controller you want to retrieve. This method is likely not necessary but is provided for your convenience should you find a need for it.

MadderManager.cs
public MadderController GetMadderController(string gamername)
{
    return MadderControllerManager.Instance.GetController(gamername);
}