'Is it possible to get the deleted/removed character on backspace in unity?
I want to know the deleted character on backspace in unity. I need to check the particular end character of the string.
e.g. abcd<quad material =0 size => 89 >
If the string ends with '>' - checks for the condition, and will do some operations based on this.
This check is happening on backspace but, on pressing backspace, the '>' gets deleted and this condition remains false always. So, is there any way I can know the deleted character on backspace.
Solution 1:[1]
Assuming you are talking about an UI.InputField you could probably use onValidateInput and check if the input was a backspace char (\b).
Maybe something like e.g.
[SerializeField] private InputField _inputField;
private void Awake ()
{
if(!_inputField) _inputField = GetComponent<InputField>();
_inputField.onValidateInput += CheckInput;
}
private void OnDestroy()
{
_inputField.onValidateInput -= CheckInput;
}
private char CheckInput(char charToValidate)
{
if (charToValidate == '\b')
{
var lastChar = _inputField.text[_inputField.text.Length - 1];
//ToDo Do something with lastChar
}
return charToValidate;
}
onValidateInput as far as I remember is executed before the character is actually added to the input, thus before e.g. onValueChanged is called.
As alternative you could of course also simply store the last value of the field and check it onValueChanged like e.g.
[SerializeField] private InputField _inputField;
private string _lastValue;
private void Awake ()
{
if(!_inputField) _inputField = GetComponent<InputField>();
_inputField.onValueChanged += CheckInput;
_lastValue = _inputField.text;
}
private void OnDestroy()
{
_inputField.onValueChanged -= CheckInput;
}
private char CheckInput()
{
var lastChar = _lastValue[_lastValue.Length - 1];
//ToDo Do something with lastChar
// Update the last value
_lastValue = _inputField.text;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
