Advertisement
lunkums

Keybinding

Oct 16th, 2022
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | Gaming | 0 0
  1. namespace Lunkums.Controller
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using UnityEngine;
  6.  
  7.     internal class KeyBinding
  8.     {
  9.         private Dictionary<KeyCode, Action> binding;
  10.         private KeyTrigger keyTrigger;
  11.  
  12.         public delegate bool KeyTrigger(KeyCode key);
  13.  
  14.         public KeyBinding(Dictionary<KeyCode, Action> binding, KeyTrigger keyTrigger)
  15.         {
  16.             this.binding = binding;
  17.             this.keyTrigger = keyTrigger;
  18.         }
  19.  
  20.         public void Rebind(Dictionary<KeyCode, Action> binding)
  21.         {
  22.             this.binding = binding;
  23.         }
  24.  
  25.         public void Iterate()
  26.         {
  27.             foreach (KeyValuePair<KeyCode, Action> keyBind in binding)
  28.             {
  29.                 if (keyTrigger(keyBind.Key))
  30.                 {
  31.                     keyBind.Value();
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
  37.  
Tags: C# Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement