-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyBindManager.cs
More file actions
155 lines (137 loc) · 4.62 KB
/
Copy pathKeyBindManager.cs
File metadata and controls
155 lines (137 loc) · 4.62 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
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.InputSystem;
[System.Serializable]
public class KeyBindEntry
{
public Button button;
public TMP_Text text;
public string actionName;
[HideInInspector]
public InputAction action;
}
public class KeyBindManager : MonoBehaviour
{
public static KeyBindManager Instance { get; private set; } // Singleton instance
public List<KeyBindEntry> keyBindEntries;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject); // Destroy duplicate instance
return;
}
Instance = this; // Set singleton instance
}
private void Start()
{
LoadKeyBindings();
InitializeKeyBindEntries();
}
private void InitializeKeyBindEntries()
{
foreach (var entry in keyBindEntries)
{
// Using Keybinds.Instance to fetch the action by name
entry.action = KeybindInstance.Instance.FindAction(entry.actionName); // Fetch the action by name
if (entry.action != null && entry.action.bindings.Count > 0)
{
string binding = PlayerPrefs.GetString(entry.actionName, entry.action.bindings[0].effectivePath);
ApplyBinding(entry, binding);
entry.button.onClick.AddListener(() => StartRebind(entry));
}
else
{
Debug.LogError($"Action '{entry.actionName}' not found in Keybinds.");
}
}
}
private void ApplyBinding(KeyBindEntry entry, string binding)
{
entry.action.ApplyBindingOverride(binding);
UpdateButtonText(entry, binding);
}
private void UpdateButtonText(KeyBindEntry entry, string binding)
{
string cleanedBinding = CleanBindingText(binding);
entry.text.text = cleanedBinding;
Debug.Log($"Updating button text for {entry.actionName} to: {cleanedBinding}");
}
private string CleanBindingText(string binding)
{
string cleaned = binding.Replace("<Keyboard>/", "")
.Replace("<Gamepad>/", "")
.Replace("<Mouse>/", "")
.Trim();
return char.ToUpper(cleaned[0]) + cleaned.Substring(1);
}
private void StartRebind(KeyBindEntry entry)
{
entry.action.Disable();
string originalText = entry.text.text;
entry.text.text = "Press a key to rebind...";
entry.action.PerformInteractiveRebinding()
.WithCancelingThrough("<Keyboard>/escape")
.OnComplete(operation =>
{
string newBinding = entry.action.bindings[0].effectivePath;
ApplyNewBinding(entry, newBinding);
operation.Dispose();
})
.OnCancel(operation =>
{
entry.text.text = originalText;
entry.action.Enable();
operation.Dispose();
})
.Start();
}
private void ApplyNewBinding(KeyBindEntry entry, string newBinding)
{
entry.action.Enable();
entry.action.ApplyBindingOverride(newBinding);
Debug.Log($"New binding applied for {entry.actionName}: {newBinding}");
UpdateButtonText(entry, newBinding);
SaveKeyBindings();
}
private void SaveKeyBindings()
{
foreach (var entry in keyBindEntries)
{
if (entry.action != null)
{
string binding = entry.action.bindings[0].effectivePath;
PlayerPrefs.SetString(entry.actionName, binding);
}
}
PlayerPrefs.Save();
}
private void LoadKeyBindings()
{
foreach (var entry in keyBindEntries)
{
if (entry.action != null && entry.action.bindings.Count > 0)
{
string binding = PlayerPrefs.GetString(entry.actionName, entry.action.bindings[0].effectivePath);
ApplyBinding(entry, binding);
}
}
}
public void ResetToDefaultBindings()
{
foreach (var entry in keyBindEntries)
{
if (entry.action != null)
{
Debug.Log($"Resetting {entry.actionName} to default binding.");
entry.action.RemoveAllBindingOverrides();
string defaultBinding = entry.action.bindings[0].effectivePath;
UpdateButtonText(entry, defaultBinding);
PlayerPrefs.DeleteKey(entry.actionName);
}
}
PlayerPrefs.Save();
}
}