Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Created January 25, 2021 11:54
Show Gist options
  • Save Mulperi/35534429740cbe6fe4e88b33054c0958 to your computer and use it in GitHub Desktop.
Save Mulperi/35534429740cbe6fe4e88b33054c0958 to your computer and use it in GitHub Desktop.
Simple gamepad manager, Unity new Input System
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