-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cs
More file actions
113 lines (94 loc) · 3.02 KB
/
Copy pathInputManager.cs
File metadata and controls
113 lines (94 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class InputManager : MonoBehaviour {
public List<string> Controllers;
int len;
public int numPlayers = 4;
public int playersMapped = 0;
public int[] joys = { -1, -1, -1, -1 };
void Start () {
len = Input.GetJoystickNames().Length;
int j = 0;
string[] controllers = Input.GetJoystickNames();
for (int i =0;i <controllers.Length; i++)
{
if (!String.IsNullOrEmpty(controllers[i]))
{
joys[j] = i;
j++;
playersMapped++;
}
}
for(;j<joys.Length;j++)
joys[j] = -1;
StartCoroutine(checkControllers());
}
public void detectPressedKeyOrButton()
{
for (int i = 1;i < 10; i++) {
if(Input.GetKeyDown("joystick "+i+" button 0")){
print("joystick "+i);
}
}
}
// Update is called once per frame
//void Update () {
// Controllers = new List<string>();
// foreach( string name in Input.GetJoystickNames())
// {
// Controllers.Add(name);
// }
// detectPressedKeyOrButton();
//}
private IEnumerator checkControllers()
{
while (true)
{
bool check = true;
string[] controllers = Input.GetJoystickNames();
for (int i = 0; i < joys.Length; i++)
{
if(joys[i]!=-1)
if (String.IsNullOrEmpty(controllers[joys[i]])){
check = false;
break;
}
}
if (!check || playersMapped < numPlayers)
{
Debug.Log("Remapping");
List<int> availableControllers = new List<int>();
for (int i = 0; i < controllers.Length; i++)
{
if (!String.IsNullOrEmpty(controllers[i]))
availableControllers.Add(i);
}
for (int i = 0; i < joys.Length; i++)
{
int index = availableControllers.IndexOf(joys[i]);
if (index != -1)
availableControllers.RemoveAt(index);
}
for (int i = 0; i < joys.Length; i++)
{
if (availableControllers.Count == 0)
break;
if(joys[i] == -1)
{
joys[i] = availableControllers[0];
availableControllers.RemoveAt(0);
playersMapped++;
}
else if (String.IsNullOrEmpty(controllers[joys[i]]))
{
joys[i] = availableControllers[0];
availableControllers.RemoveAt(0);
}
}
}
yield return new WaitForSeconds(1f);
}
}
}