Я пытаюсь сделать входное поле 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
Как реализовать поле otp ввода в Unity ⇐ C#
Место общения программистов C#
1738245846
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[i].characterLimit = 1;
otpFields[i].onValueChanged.AddListener(delegate { OnInputValueChanged(index); });
otpFields[i].onSubmit.AddListener(delegate { OnInputSubmit(index); });
// Ensure only the first input field is interactable at first
otpFields[i].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;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79399800/how-to-implement-otp-input-field-in-unity[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия