View difference between Paste ID: FccDFUZ6 and 5bYSRXWm
SHOW: | | - or go back to the newest paste.
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] 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] 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
}