How can multiple users control a single UI canvas using InputSystem?
Hi, I am working on a local co-op game where each user uses a different device and controls a different character. The game does not feature split-screen and only makes use of a single UI canvas, which each user should be able to control.
The issue:
When the game is launched, the scene only contains the main menu and it can be used by any device. From there, users are able to join and select a device to use. When the game starts, different PlayerInput are spawned for each user + device combo.
At this point each device controls its corresponding character just fine, but once the "Pause Menu" is displayed (and the InputActionMaps are switched) only one of the users is able to use the UI.
My current configuration:
I have a PlayerInputActions named MyActions with two maps: Gameplay, Menu
Each character prefab contains a PlayerInput that uses MyActions
The UI has a EventSystem and InputSystemUIInputModule that uses MyActions/Menu
What I have already tried:
Replace the UI EventSystem for a MultiplayerEventSystem, didn't work
Replace the UI EventSystem for a MultiplayerEventSystem and add new MultiplayerEventSystem and InputSystemUIInputModule to each character that spawns, didn't work
All of the above plus pointing the PlayerRoot field of each MultiplayerEventSystem to the UI and pointing the UI Module field of each PlayerInput to its corresponding module, didn't work
Exactly the same but pointing the UI Module field of each PlayerInput to the module on the UI, didn't work
I ran out of ideas already, I can not find any help in either the docs or the forums. Has anyone faced a similar issue? Could you please explain to me how to use these event systems and ui modules to accept inputs from different users?
Answer by AlbertoVgdd · May 07, 2021 at 09:10 AM
I figured it out. I will post the solution here for anyone trying to achieve the same behavior. Basically, what needs to be in the scene is:
The different player characters, each with a PlayerInput.
A single UI Canvas with Event System and Input System UI Input Module.
A single InputActionAsset with 2 actions maps (Gameplay and Menu)
What needs to happen when the game is paused:
1- Deactivate the inputs of all the characters except the one who paused the game using PlayerInput.Deactivate()
2- Use PlayerInput.SwitchCurrentActionMap("Menu") on the character who paused the game.
3- Assign the action map instance of the player who paused to the Input Sytem UI Input Module using: InputSystemUiInputModule.ActionsAsset = PlayerInput.actions
Do you think it's possible to have multiple players control the UI at the same time? Not just the one that paused the game? I am trying to make the UI controllable by all players at all times
Answer by GetLitGames · May 01, 2021 at 06:50 PM
I found this from a forum thread: https://forum.unity.com/threads/request-tutorial-or-guide-on-how-to-use-the-new-multiplayer-event-system.736787/
Also, here is a recent video: https://www.youtube.com/watch?v=81GecyuNapg
Also another video tutorial on creating local multiplayer intput: https://www.youtube.com/watch?v=_5pOiYHJgl0
Thanks for the quick response. Unfortunately, I have already visited all of these resources before. None of them apply to what I want to achieve, as all of them explain how to setup a scene with multiple UI canvas, while I want to just use 1 UI canvas.
Use one Canvas and all the supporting UI. Have a script on either the Canvas or another script high in the hierarchy that has a PlayerNumber property that can be changed to be 1,2,3,4 etc. Create a prefab from the Canvas then duplicate it in the hierarchy and set the PlayerNumber to be different for each Canvas. All your button clicks that go to functions can have some code to lookup their PlayerNumber from the top level script (simply GetComponentInParent) if needed to know which player is using the buttons etc. Basically it sounds like maybe you are trying to work around a technical detail that doesn't need to be worked around - there is no reason why you can't use multiple Canvas if that's what the system requires. Simply use the Prefab system to have multiple Canvas but are from the same prefab so are not really separate Canvas.
But that would imply having multiple canvas, which is precisely what I am trying to avoid at the moment.
Answer by luizguilherme0856 · Mar 23 at 03:10 PM
If anyone still have this problem, I solved it by simply making the button interaction from scratch. When the game is paused and the player press the arrows on the keyboard or the gamepad, it call a method that change the current selected button from the event system. And then you just need another method that clicks the current selected button.
I know its a mess but it works and you don't need to use multiple canvas. Also @Pangamini both players can control the screen at the same time.
public void SelectButton(bool goUp)
{
var buttonSelected = eventSystem.currentSelectedGameObject.name;
if(buttonSelected == "Resume button")
{
if (goUp)
{
eventSystem.SetSelectedGameObject(quitButton);
}
else
{
eventSystem.SetSelectedGameObject(mainMenuButton);
}
}
else if (buttonSelected == "Main menu button")
{
if (goUp)
{
eventSystem.SetSelectedGameObject(resumeGameButton);
}
else
{
eventSystem.SetSelectedGameObject(quitButton);
}
}
else if( buttonSelected == "Quit game button")
{
if (goUp)
{
eventSystem.SetSelectedGameObject(mainMenuButton);
}
else
{
eventSystem.SetSelectedGameObject(quitButton);
}
}
else { Debug.LogError("Button not found."); }
}