Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class InputController : MonoBehaviour {
- private List<KeyCode> keys = new List<KeyCode>();
- private List<bool> keysDown = new List<bool>();
- public delegate void KeyPress(KeyCode key);
- public event KeyPress OnKeyPress = delegate { };
- public event KeyPress OnKeyHold = delegate { };
- public event KeyPress OnKeyRelease = delegate { };
- public void AddKey(KeyCode _key)
- {
- keys.Add(_key);
- keysDown.Add(false);
- }
- public void DeleteKey(KeyCode _key)
- {
- keysDown.RemoveAt(keys.IndexOf(_key));
- keys.RemoveAt(keys.IndexOf(_key));
- }
- public void ClearKeys()
- {
- keys.Clear();
- keysDown.Clear();
- }
- void Update () {
- for (int i=0; i<keys.Count; i++)
- {
- if (!keysDown[i] && Event.current.keyCode == keys[i])
- {
- OnKeyPress(Event.current.keyCode);
- keysDown[i] = true;
- continue;
- }
- if (Event.current.keyCode == keys[i] && Event.current.type == EventType.KeyUp) {
- OnKeyRelease(Event.current.keyCode);
- keysDown[i] = false;
- continue;
- }
- if (keysDown[i] && Event.current.keyCode == keys[i] && Event.current.type != EventType.KeyUp)
- {
- OnKeyHold(Event.current.keyCode);
- continue;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement