/
ControlModeSwitcher.cs
57 lines (50 loc) · 1.3 KB
/
ControlModeSwitcher.cs
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
using UnityEngine;
using System.Linq;
using System;
public class ControlModeSwitcher : MonoBehaviour {
[System.Serializable]
public class ControlMode {
public KeyCode KeyCode;
public MonoBehaviour Behaviour;
}
public ControlMode[] ControlModes;
public KeyCode[] ModeSwitchKeys;
private ControlMode currentMode;
void Start() {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
if (this.ControlModes.Any()) {
this.SetMode(this.ControlModes[0]);
}
foreach (var mode in this.ControlModes) {
if (mode != this.currentMode) {
mode.Behaviour.enabled = false;
}
}
}
void Update() {
foreach (var mode in this.ControlModes) {
if (Input.GetKeyDown(mode.KeyCode)) {
this.SetMode(mode);
}
}
if (this.currentMode != null && this.ModeSwitchKeys != null) {
foreach (var keyCode in this.ModeSwitchKeys) {
if (Input.GetKeyDown(keyCode)) {
int currentIndex = Array.IndexOf(this.ControlModes, this.currentMode);
this.SetMode(this.ControlModes[(currentIndex + 1) % this.ControlModes.Length]);
}
}
}
}
void SetMode(ControlMode mode) {
if (this.currentMode == mode) {
return;
}
if (this.currentMode != null) {
this.currentMode.Behaviour.enabled = false;
}
this.currentMode = mode;
this.currentMode.Behaviour.enabled = true;
}
}