Created
January 25, 2021 11:54
-
-
Save Mulperi/35534429740cbe6fe4e88b33054c0958 to your computer and use it in GitHub Desktop.
Simple gamepad manager, Unity new Input System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class InputController : MonoBehaviour | |
{ | |
public List<GameObject> players; | |
public Dictionary<int, GameObject> inputPlayerMap = new Dictionary<int, GameObject>(); | |
void Start() | |
{ | |
ListenInputChanges(); | |
} | |
private void ListenInputChanges() | |
{ | |
InputSystem.onDeviceChange += | |
(device, change) => | |
{ | |
switch (change) | |
{ | |
case InputDeviceChange.Added: | |
inputPlayerMap.Add(device.deviceId, players[inputPlayerMap.Count]); | |
inputPlayerMap[device.deviceId].SetActive(true); | |
break; | |
case InputDeviceChange.Removed: | |
inputPlayerMap[device.deviceId].SetActive(false); | |
inputPlayerMap.Remove(device.deviceId); | |
break; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment