Advertisement
Huntrt

KeyBinder.cs (TMP)

Jan 5th, 2023 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | Source Code | 0 0
  1. using UnityEngine.UI;
  2. using UnityEngine;
  3.  
  4. public class KeyBinder : MonoBehaviour
  5. {
  6.     [SerializeField] KeyManager.Binds bindTo;
  7.     public string BindedAction { get => bindTo.ToString();}
  8.     [SerializeField] Button bindingButton;
  9.     [SerializeField] TMPro.TMP_Text keyLabel;
  10.     [Header("Auto Setup (Optional)")]
  11.     [Tooltip("Rename this object to action it will be bind to")]
  12.     [SerializeField] GameObject autoNaming;
  13.     [Tooltip("Make this label text match the action it will bind to")]
  14.     [SerializeField] TMPro.TMP_Text actionLabel;
  15.     KeyManager manager;
  16.  
  17.     void OnValidate()
  18.     {
  19.         if(autoNaming != null) gameObject.name = BindedAction;
  20.         if(actionLabel != null) actionLabel.text = BindedAction;
  21.     }
  22.  
  23.     void Start()
  24.     {
  25.         //Get the key manager
  26.         manager = KeyManager.i;
  27.         //Upon clicking bind button it will send this to be bind in manager
  28.         bindingButton.onClick.AddListener(delegate {manager.BeginBind(this);});
  29.         //Display the keycode of action binded to text label
  30.         DisplayKeyLabel(manager.ReflectActionKeycode(BindedAction).GetValue(manager).ToString());
  31.     }
  32.  
  33.     public void DisplayKeyLabel(string display)
  34.     {
  35.         //Display the given text to label
  36.         keyLabel.text = display;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement