-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlsManager.cs
More file actions
167 lines (155 loc) · 5.43 KB
/
Copy pathControlsManager.cs
File metadata and controls
167 lines (155 loc) · 5.43 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ControlsManager : MonoBehaviour
{
public GameObject player;
public TextMeshProUGUI inGameControls;
public TextMeshProUGUI notice;
public TextMeshProUGUI left;
public Button rebindLeft;
public TextMeshProUGUI right;
public Button rebindRight;
public TextMeshProUGUI sprint;
public Button rebindSprint;
public TextMeshProUGUI jump;
public Button rebindJump;
private bool isRebinding = false;
private bool isRebindingLeft = false;
private bool isRebindingRight = false;
private bool isRebindingSprint = false;
private bool isRebindingJump = false;
private KeyCode moveLeft;
private KeyCode moveRight;
private KeyCode msprint;
private KeyCode mjump;
// Start is called before the first frame update
public void UpdateVisibility()
{
rebindLeft.gameObject.SetActive(PlayerPrefs.GetInt("MoveLeftState", 0) == 3);
rebindRight.gameObject.SetActive(PlayerPrefs.GetInt("MoveRightState", 0) == 3);
rebindSprint.gameObject.SetActive(PlayerPrefs.GetInt("SprintState", 0) == 3);
rebindJump.gameObject.SetActive(PlayerPrefs.GetInt("JumpState", 0) == 3);
string controlsText = "Controls:\nPause: Esc\n";
if (PlayerPrefs.GetInt("MoveLeftState", 0) > 1)
{
left.text = "Left: " + (KeyCode)PlayerPrefs.GetInt("MoveLeft", (int)KeyCode.A);
controlsText += "Left: " + (KeyCode)PlayerPrefs.GetInt("MoveLeft", (int)KeyCode.A) + "\n";
Debug.Log("Setting Left");
}
else
{
left.text = "";
}
if (PlayerPrefs.GetInt("MoveRightState", 0) > 1)
{
right.text = "Right: " + (KeyCode)PlayerPrefs.GetInt("MoveRight", (int)KeyCode.A);
controlsText += "Right: " + (KeyCode)PlayerPrefs.GetInt("MoveRight", (int)KeyCode.D) + "\n";
Debug.Log("Setting Right");
}
else
{
right.text = "";
}
if (PlayerPrefs.GetInt("SprintState", 0) > 1)
{
sprint.text = "Sprint: " + (KeyCode)PlayerPrefs.GetInt("Sprint", (int)KeyCode.A);
controlsText += "Sprint: " + (KeyCode)PlayerPrefs.GetInt("Sprint", (int)KeyCode.LeftShift) + "\n";
}
else
{
sprint.text = "";
}
if (PlayerPrefs.GetInt("JumpState", 0) > 1)
{
jump.text = "Jump: " + (KeyCode)PlayerPrefs.GetInt("Jump", (int)KeyCode.A);
controlsText += "Jump: " + (KeyCode)PlayerPrefs.GetInt("Jump", (int)KeyCode.Space) + "\n";
}
else
{
jump.text = "";
}
inGameControls.text = controlsText;
}
public void RebindLeft()
{
isRebindingLeft = true;
isRebinding = true;
}
public void RebindRight()
{
isRebindingRight = true;
isRebinding = true;
}
public void RebindSprint()
{
isRebindingSprint = true;
isRebinding = true;
}
public void RebindJump()
{
isRebindingJump = true;
isRebinding = true;
}
void LoadControls()
{
moveLeft = (KeyCode)PlayerPrefs.GetInt("MoveLeft", (int)KeyCode.A);
moveRight = (KeyCode)PlayerPrefs.GetInt("MoveRight", (int)KeyCode.D);
msprint = (KeyCode)PlayerPrefs.GetInt("Sprint", (int)KeyCode.LeftShift);
mjump = (KeyCode)PlayerPrefs.GetInt("Jump", (int)KeyCode.Space);
}
void SaveControls()
{
PlayerPrefs.SetInt("MoveLeft", (int)moveLeft);
PlayerPrefs.SetInt("MoveRight", (int)moveRight);
PlayerPrefs.SetInt("Sprint", (int)msprint);
PlayerPrefs.SetInt("Jump", (int)mjump);
PlayerPrefs.Save();
}
private void Update()
{
if (isRebinding)
{
LoadControls();
notice.text = "Press Any Key To Rebind";
foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(keyCode))
{
if (keyCode != KeyCode.Escape)
{
if (isRebindingLeft)
{
moveLeft = keyCode;
isRebindingLeft = false;
}
if (isRebindingRight)
{
moveRight = keyCode;
isRebindingRight = false;
}
if (isRebindingSprint)
{
msprint = keyCode;
isRebindingSprint = false;
}
if (isRebindingJump)
{
mjump = keyCode;
isRebindingJump = false;
}
SaveControls();
player.GetComponent<PlayerMovement>().LoadControls();
UpdateVisibility();
isRebinding = false;
notice.text = "";
break;
}
}
}
}
}
}