Hello,
I am trying to make configurable user input so the player can change the keys they use to play the game.
I am using an InputField to allow the key entry and I have one issue at the moment. I would like to get around this by removing the current letter in the field when the field has been selected. How can I detect if the inputField is currently being edited?
Thanks.
EDIT:
Here is the code I currently have for the script. Everything works fine part from removing the current assigned key. Unless someone else can give me a UI option to allow editing in an easier way that using InputFields and Character Validation set to “Name”.
The code can most likely be done in a much better way but I am only testing for now.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class KeyBinding : MonoBehaviour {
[SerializeField]
private KeyCode forwardBind = KeyCode.W;
[SerializeField]
private KeyCode backBind = KeyCode.S;
[SerializeField]
private KeyCode leftBind = KeyCode.A;
[SerializeField]
private KeyCode rightBind = KeyCode.D;
[SerializeField]
public InputField _forwardKeyText;
public InputField _backKeyText;
public InputField _leftKeyText;
public InputField _rightKeyText;
private string clearText;
// Use this for initialization
void Start () {
if(PlayerPrefs.HasKey("ForwardKey"))
{
_forwardKeyText.text = LoadString("ForwardKey");
forwardBind = (KeyCode)System.Enum.Parse (typeof(KeyCode),_forwardKeyText.text);
}
else
_forwardKeyText.text = forwardBind.ToString ();
if(PlayerPrefs.HasKey("BackKey"))
{
_backKeyText.text = LoadString("BackKey");
backBind = (KeyCode)System.Enum.Parse (typeof(KeyCode),_backKeyText.text);
}
else
_backKeyText.text = backBind.ToString ();
if(PlayerPrefs.HasKey("LeftKey"))
{
_leftKeyText.text = LoadString("LeftKey");
leftBind = (KeyCode)System.Enum.Parse (typeof(KeyCode),_leftKeyText.text);
}
else
_leftKeyText.text = leftBind.ToString ();
if(PlayerPrefs.HasKey("RightKey"))
{
_rightKeyText.text = LoadString("RightKey");
rightBind = (KeyCode)System.Enum.Parse (typeof(KeyCode),_rightKeyText.text);
}
else
_rightKeyText.text = rightBind.ToString ();
}
// Update is called once per frame
void Update () {
if(_forwardKeyText.text == "")
{
forwardBind = KeyCode.None;
}
else if (_forwardKeyText.text != "")
{
forwardBind = (KeyCode)System.Enum.Parse (typeof(KeyCode),_forwardKeyText.text);
SaveString("ForwardKey",forwardBind.ToString());
}
if(_backKeyText.text == "")
{
backBind = KeyCode.None;
}
else if (_backKeyText.text != "")
{
backBind = (KeyCode)System.Enum.Parse (typeof(KeyCode), _backKeyText.text);
SaveString("BackKey",backBind.ToString());
}
if(_leftKeyText.text == "")
{
leftBind = KeyCode.None;
}
else if (_leftKeyText.text != "")
{
leftBind = (KeyCode)System.Enum.Parse (typeof(KeyCode), _leftKeyText.text);
SaveString("LeftKey",leftBind.ToString());
}
if(_rightKeyText.text == "")
{
rightBind = KeyCode.None;
}
else if (_rightKeyText.text != "")
{
rightBind = (KeyCode)System.Enum.Parse (typeof(KeyCode), _rightKeyText.text);
SaveString("RightKey",rightBind.ToString());
}
}