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.
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.
publicclassSamplePlayerManager:MonoBehaviour{[SerializeField]privateGameObject playerPrefab;[SerializeField]privateInputActionAsset inputActions;privateDictionary<string, PlayerInput> playerInputs =newDictionary<string, PlayerInput>();privateMadderManager madderManager;privatevoidAwake(){ madderManager =FindObjectOfType<MadderManager>();//subscribe to RegisterMadderController event MadderManager.onRegisterMadderController += OnRegisterMadderController;}privatevoidOnRegisterMadderController(MadderPlayer madderPlayer){RegisterPlayer(madderPlayer.name);}publicvoidRegisterPlayer(string gamername){// Create a new player at a random location between (0,0,0) and (4,4,0)GameObject player =Instantiate(playerPrefab,newVector3(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 playersInputActionAsset clonedinputActions =Instantiate(inputActions); playerInput.actions = clonedinputActions;//Store the PlayerInput instance playerInputs[gamername]= playerInput;//Initialize the player controllerSamplePlayer playerController = player.GetComponent<SamplePlayer>(); playerController.Initialize(playerInput);}publicvoidUnregisterPlayer(string gamername){if(playerInputs.TryGetValue(gamername,outvar playerInput)){ playerInputs.Remove(gamername);Destroy(playerInput.gameObject); madderManager.UnregisterMadderController(gamername);}}publicboolTryGetPlayerInput(string gamername,outPlayerInput playerInput){return playerInputs.TryGetValue(gamername,out playerInput);}}
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.
publicclassMadderControllerTest:MonoBehaviour{publicMadderManager madderManager;voidStart(){ madderManager =FindObjectOfType<MadderManager>();}publicvoidTestMadderInput(){//Register a new madder controller with a random nameconststring chars ="abcdefghijklmnopqrstuvwxyz";string randomName ="";for(int i =0; i <4; i++){ randomName += chars[Random.Range(0, chars.Length)];}MadderPlayer madderPlayer =newMadderPlayer(); 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 valuesStartCoroutine(waiter(randomName));}IEnumeratorwaiter(string randomName){//Give the controller a random direction to move infloat 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 secondsyieldreturnnewWaitForSeconds(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.