Как реализовать поле otp ввода в UnityC#

Место общения программистов C#
Ответить
Anonymous
 Как реализовать поле otp ввода в Unity

Сообщение Anonymous »

Я пытаюсь сделать входное поле OTP в Unity для этого, я беру четыре поля ввода и активю их один за другим, чтобы ввести число, но для каждого выбранного входного поле открылось новая клавиатура в мобильном < /p>
Я попробовал этот скрипт, чтобы имитировать OTP Inputfield. Проблема заключается в том, что каждое входное поле открывает новый Keybord. Функция для Inputfield < /p>
using UnityEngine;
using TMPro;

public class OTPInputManager : MonoBehaviour
{
[Header("OTP Input Fields")]
public TMP_InputField[] otpFields;

private TouchScreenKeyboard keyboard;
private int currentFieldIndex = -1;

private void Start()
{
// Add listeners to all input fields
for (int i = 0; i < otpFields.Length; i++)
{
int index = i;
otpFields.characterLimit = 1;
otpFields.onValueChanged.AddListener(delegate { OnInputValueChanged(index); });
otpFields.onSubmit.AddListener(delegate { OnInputSubmit(index); });

// Ensure only the first input field is interactable at first
otpFields.interactable = (i == 0);
}
}

private void OnInputValueChanged(int index)
{
if (otpFields[index].text.Length > 0)
{
// Move to the next input field if a digit is entered
if (index < otpFields.Length - 1)
{
otpFields[index + 1].interactable = true;
FocusOnNextField(index + 1);
}
}
else if (index > 0)
{
// Go back to the previous input field if text is deleted
otpFields[index].interactable = false;
FocusOnPreviousField(index - 1);
}
}

private void FocusOnNextField(int index)
{
// Focus on the next field without reopening the keyboard
otpFields[index].Select();
if (keyboard == null || !keyboard.active)
{
// Open the keyboard only once when first selected
if (currentFieldIndex == -1)
{
keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default);
}
}
currentFieldIndex = index;
}

private void FocusOnPreviousField(int index)
{
// Focus on the previous field without reopening the keyboard
otpFields[index].Select();
currentFieldIndex = index;
}

private void OnInputSubmit(int index)
{
if (index == otpFields.Length - 1)
{
// Collect OTP when the last field is submitted
string otp = GetOTP();
Debug.Log("Entered OTP: " + otp);

// Add OTP validation logic here
}
}

private string GetOTP()
{
string otp = string.Empty;
foreach (var field in otpFields)
{
otp += field.text;
}
return otp;
}



Подробнее здесь: https://stackoverflow.com/questions/793 ... d-in-unity
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»