| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- namespace AppUI.Util.VerifyCode
- {
- public class VerificationCode : MonoBehaviour
- {
- [Header("真正输入框")]
- public TMP_InputField inputField;
- [Header("6个数字文本")]
- public TMP_Text[] numberTexts;
- [Header("圆圈背景")]
- public Image[] circles;
- [Header("颜色")]
- public Color normalColor = Color.gray;
- public Color selectedColor = Color.white;
- private void Start()
- {
- inputField.onValueChanged.AddListener(OnInputChanged);
- inputField.characterLimit = 6;
- RefreshUI();
- // 自动激活输入
- inputField.ActivateInputField();
- }
- private void OnInputChanged(string value)
- {
- // 只允许数字
- string filtered = "";
- foreach (char c in value)
- {
- if (char.IsDigit(c))
- {
- filtered += c;
- }
- }
- if (filtered != value)
- {
- inputField.text = filtered;
- return;
- }
- RefreshUI();
- // 输入完成
- if (filtered.Length == 6)
- {
- Debug.Log("验证码:" + filtered);
- // TODO:
- // 提交验证码
- }
- }
- private void RefreshUI()
- {
- string value = inputField.text;
- for (int i = 0; i < numberTexts.Length; i++)
- {
- if (i < value.Length)
- {
- numberTexts[i].text = value[i].ToString();
- }
- else
- {
- numberTexts[i].text = "";
- }
- // 当前输入位置高亮
- if (i == value.Length && value.Length < 6)
- {
- circles[i].color = selectedColor;
- }
- else
- {
- circles[i].color = normalColor;
- }
- }
- }
- // 点击整个区域时激活输入
- public void OnClickInputArea()
- {
- inputField.ActivateInputField();
- }
- }
- }
|