I am facing Issue with the multiple Tmpro_Input fields. I am making Login With pin in which I have the 4 different Input field, Each Input field will contain the 1 input in int and the input is using Int Keyboard.
When i am on the first input field and enter next which allows to focus on the next input field. The old keyboard closes and the new keyboard opens which is not good.
I want when the first input is selected and when i enter the value in it the keyboard will remain same, or do not open or closes. I also want to handle the backspace in Both Android and IOS, for Better UX.
I am using the new Input system, so the currentSelectedGameobject is not working on there.
Is there anyone with the solution it will be helpful. Thanks In advance.
1 Like
ok so this is bit tricky
since unity TMP_Inputfield’s default working is open keyboard on focus and close on another focused or unfocused from active one
check the “hide soft keyboard” in input fields and add this script on the parent of all the input fields
it gets all the input fields and put them in a list and then on click of any of them activates the keyboard by code manually and fetch the value written on it to the input fields set character limit of keyboard according to the number of input fields you have and clear the values when reponing keyboard again
this is how i did it.
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class OTPInputManager : MonoBehaviour
{
public List<TMP_InputField> otpInputFields = new List<TMP_InputField>();
private TouchScreenKeyboard keyboard;
private void OnValidate()
{
otpInputFields.Clear();
foreach (Transform child in transform)
{
var inputField = child.GetComponent<TMP_InputField>();
if (inputField != null)
{
otpInputFields.Add(inputField);
}
}
}
void Start()
{
foreach (var inputField in otpInputFields)
{
inputField.onSelect.AddListener(delegate { OnInputFieldSelected(); });
}
}
void OnInputFieldSelected()
{
if (keyboard == null || !keyboard.active)
{
keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.NumberPad);
}
}
void Update()
{
if (keyboard != null && keyboard.active)
{
keyboard.characterLimit = 6;
string input = keyboard.text;
// Clear all input fields
foreach (var inputField in otpInputFields)
{
inputField.text = "";
}
// Distribute input across fields
for (int i = 0; i < input.Length && i < otpInputFields.Count; i++)
{
otpInputFields[i].text = input[i].ToString();
}
}
}
}