Skip to main content
Madder Games

Setting up

The Madder Starter Pack contains everything you need to integrate your Unity game with the Madder Controller, whether you use the new Unity Input System or not.
The Madder Starter Pack requires the Unity Input System package to be installed. If you do not have the Unity Input System package installed, it will be installed automatically when you import the Madder Starter Pack. However, you do not have to use the Unity Input System to use the Madder Starter Pack.

Installation

You can install the Madder Starter Pack through the Unity Package manager by using this git URL:
git@github.com:maddergames/madder-starter-package.git
We recommend you use the git URL to make updating the package easier.

Integration

The Madder Starter Pack contains two prefabs called MadderManager and MadderControllerManager that must be placed in the scene. These prefabs are Singleton classes, which means they will persist across scene loads. If using the Unity Input system, the Madder Controller layout will be automatically registered with the Unity Input System upon loading the Unity Editor. You can then assign the bindings to your desired actions in the Unity Input System. See the Madder Controller class for more information. The Madder Starter Pack provides a sample game manager, sample player manager, and sample player to show you how you might interact with the Madder Manager.
public class SamplePlayerManager : MonoBehaviour
{
    [SerializeField] private GameObject playerPrefab;
    [SerializeField] private InputActionAsset inputActions;
    private Dictionary<string, PlayerInput> playerInputs = new Dictionary<string, PlayerInput>();
    private MadderManager madderManager;

    private void Awake()
    {
        madderManager = FindObjectOfType<MadderManager>();


        //subscribe to RegisterMadderController event
        MadderManager.onRegisterMadderController += OnRegisterMadderController;

    }


    private void OnRegisterMadderController(MadderPlayer madderPlayer)
    {
        RegisterPlayer(madderPlayer.name);
    }

    public void RegisterPlayer(string gamername)
    {
        // Create a new player at a random location between (0,0,0) and (4,4,0)

        GameObject player = Instantiate(playerPrefab, new Vector3(Random.Range(0, 4), Random.Range(0, 4), 0), Quaternion.identity);

        player.name = gamername;
        Debug.Log("Player created: " + player.name);


        PlayerInput playerInput = player.AddComponent<PlayerInput>();


        if (playerInput == null)
        {
            Debug.Log("PlayerInput is null");
        }

        //We must clone the inputActions to avoid sharing the same instance between players
        InputActionAsset clonedinputActions = Instantiate(inputActions);
        playerInput.actions = clonedinputActions;

        //Store the PlayerInput instance
        playerInputs[gamername] = playerInput;

        //Initialize the player controller
        SamplePlayer playerController = player.GetComponent<SamplePlayer>();
        playerController.Initialize(playerInput);



    }

    public void UnregisterPlayer(string gamername)
    {
        if (playerInputs.TryGetValue(gamername, out var playerInput))
        {
            playerInputs.Remove(gamername);
            Destroy(playerInput.gameObject);
            madderManager.UnregisterMadderController(gamername);
        }
    }

    public bool TryGetPlayerInput(string gamername, out PlayerInput playerInput)
    {
        return playerInputs.TryGetValue(gamername, out playerInput);
    }

}

Testing

The Madder Controller Starter Pack contains a MadderControllerTest file that you can use or copy from to test the Madder Controller in your game. The MadderControllerTest is a simple script that will simulate the Madder Controller input for testing purposes. We have also provided a sample game manager to show how you could call the MadderControllerTest.
public class MadderControllerTest : MonoBehaviour
{
    public MadderManager madderManager;

    void Start()
    {
        madderManager = FindObjectOfType<MadderManager>();
    }

    public void TestMadderInput()
    {
        //Register a new madder controller with a random name
        const string chars = "abcdefghijklmnopqrstuvwxyz";
        string randomName = "";
        for (int i = 0; i < 4; i++)
        {
            randomName += chars[Random.Range(0, chars.Length)];
        }
        MadderPlayer madderPlayer = new MadderPlayer();
        madderPlayer.name = randomName;
        string madderPlayerJson = JsonUtility.ToJson(madderPlayer);
        madderManager.RegisterMadderController(madderPlayerJson);
        //Create a new controller state that follows MadderControllerState class
        //Wait for 5 seconds
        //Send a new controller state with different values
        StartCoroutine(waiter(randomName));

    }

    IEnumerator waiter(string randomName)
    {
        //Give the controller a random direction to move in
        float x = Random.Range(-1f, 1f);
        float y = Random.Range(-1f, 1f);
        string jsonControllerState = "{\"name\":\"" + randomName + "\",\"joystick\":{\"x\":" + x + ",\"y\":" + y + "},\"circle\":false,\"triangle\":false,\"plus\":false}";
        madderManager.UpdateMadderControllerState(jsonControllerState);
        //wait for 5 seconds
        yield return new WaitForSeconds(5);

        //send a new controller state with different values
        jsonControllerState = "{\"name\":\"" + randomName + "\",\"joystick\":{\"x\":0,\"y\":0},\"circle\":false,\"triangle\":false,\"plus\":false}";
        madderManager.UpdateMadderControllerState(jsonControllerState);

    }

}

Once you have integrated with Madder successfully, you can test your game with the Madder Controller app within the Madder Developer Portal.
I