using System.Collections; using System.Collections.Generic; using Conjure.Input; using Sirenix.OdinInspector; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.UI; using UnityEngine.InputSystem.Utilities; using InputDevice = UnityEngine.InputSystem.InputDevice; public class MenuInputManager : SerializedMonoBehaviour { [SerializeField, Required] private InputSystemUIInputModule inputSystemUIInputModule; [SerializeField, Required] private InputServerReference inputServerReference; // Start is called before the first frame update void Start() { inputSystemUIInputModule.actionsAsset.devices = new ReadOnlyArray(); } public void OnJoin(int playerSeat) { if (inputSystemUIInputModule.actionsAsset.devices == null) { return; } InputManagerServer inputManagerServer = inputServerReference.Value.GetComponent(); List devices = new List(inputSystemUIInputModule.actionsAsset.devices.Value.ToArray()); PlayerEnum player = inputManagerServer.GetPlayerFromInt(playerSeat); // shouldn't be reachable if (player == PlayerEnum.None) { return; } InputDevice currentDevice = FindDeviceById(inputManagerServer.GetDeviceIdFromPlayerEnum(player)); // shouldn't be reachable if (currentDevice is not null) { devices.Add(currentDevice); } inputSystemUIInputModule.actionsAsset.devices = new ReadOnlyArray(devices.ToArray()); } public void OnLeave(int playerSeat) { if (inputSystemUIInputModule.actionsAsset.devices == null) { return; } InputManagerServer inputManagerServer = inputServerReference.Value.GetComponent(); if (playerSeat == 0 && inputManagerServer.GetNumberPlayers() == 0) { inputSystemUIInputModule.actionsAsset.devices = new ReadOnlyArray(); return; } InputDevice currentDevice = FindDeviceById(inputManagerServer.GetDeviceIdFromPlayerEnum(PlayerEnum.Player1)); inputSystemUIInputModule.actionsAsset.devices = new ReadOnlyArray(new []{currentDevice}); } private InputDevice FindDeviceById(int deviceId) { foreach (InputDevice currentDevice in InputSystem.devices) { if (currentDevice.deviceId == deviceId) { return currentDevice; } } return null; } }