溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用UGUI怎么實(shí)現(xiàn)一個(gè)4位數(shù)驗(yàn)證碼功能

發(fā)布時(shí)間:2021-04-17 16:06:27 來源:億速云 閱讀:144 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)使用UGUI怎么實(shí)現(xiàn)一個(gè)4位數(shù)驗(yàn)證碼功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

預(yù)制體結(jié)構(gòu):

使用UGUI怎么實(shí)現(xiàn)一個(gè)4位數(shù)驗(yàn)證碼功能

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class VerifyCodePanel : ILoginChild
{
 
  public const int VERIFY_CODE_LENGTH = 4;
 
  public InputField VCodeInput;
  public Text[] VCodes = new Text[VERIFY_CODE_LENGTH];
 
  //外部選擇藍(lán)色與灰色,分別表示已輸入(Blue)和未輸入(Gray)
  public Color Blue;
  public Color Gray;
 
  private int m_lastVcodeLength = 0;
 
  void Start()
  {
    //外部設(shè)置VCodeInput等組件,或使用代碼獲取,這必須在初始化或之前設(shè)置完成
    //UIUtility是自己實(shí)現(xiàn)的一個(gè)針對(duì)UI組件獲取與綁定的一個(gè)工具
    VCodeInput = UIUtility.GetChildControl<InputField>("VCodeInput", transform);
    BindUIEvents();
  }
 
  private void BindUIEvents()
  {
    //綁定VCodeInput的值變化,外部綁定和代碼綁定皆可,這里使用的是自己實(shí)現(xiàn)的一個(gè)UI工具
    UIUtility.BindUIEvent(EventTypeEnum.InputFieldOnValueChanged,    
      OnVCodeValueChanged, VCodeInput.transform);
  }
 
  // 監(jiān)聽輸入的字符變化
  private void OnVCodeValueChanged(string value)
  {
    //外部已經(jīng)設(shè)置好VCodeInput的長(zhǎng)度限制,最多為4,但是為了避免意外情況,限制字符長(zhǎng)度
    if (value.Length < 0 || value.Length > 4)
    {
      return;
    }
    //判斷字符是邊長(zhǎng)了還是變短了,用以決定Text的跳轉(zhuǎn)方向
    bool next = false;
    //分割字符,默認(rèn)為空,如果是長(zhǎng)度增加,則說明輸入了新的驗(yàn)證碼,將這個(gè)驗(yàn)證碼記錄下來
    string character = string.Empty;
    //比較與上一次輸入的驗(yàn)證碼長(zhǎng)度,缺省長(zhǎng)度為0
    if(value.Length > m_lastVcodeLength)
    {
      next = true;
      character = value[value.Length - 1].ToString();
    }
    m_lastVcodeLength = value.Length;
    int which = value.Length - 1;
    OnMoveCursor(next, which, character);
  }
 
  //移動(dòng)光標(biāo)(實(shí)際上是操作用來顯示驗(yàn)證碼的Text)
  private void OnMoveCursor(bool next, int which, string character)
  {
    //將值賦給對(duì)應(yīng)的Text
    if (next)
    {
      VCodes[which].text = character;
      SetLineColor(which, Blue);
    }
    else
    {
      /*這里比較繞,如果是回退,則說明當(dāng)前的which其實(shí)是已經(jīng)退格后的位數(shù),
      * 比如說,原本是輸入了123,退格后則變成了12,
      * 那么which的值為value.length - 1 = 1; value.length為2
      * 而我們需要將原本第三位(從0開始計(jì)數(shù),值為2)的Text設(shè)置為空
      * 因此需要i + 1,而為了避免用戶全選并刪除所有驗(yàn)證碼的情況,需要遍歷
      */
      for (int i = which; i < VERIFY_CODE_LENGTH - 1; ++i)
      {
        VCodes[i + 1].text = character;
        SetLineColor(i + 1, Gray);
      }
    }
  }
 
  //設(shè)置相應(yīng)Text的Line的顏色
  private void SetLineColor(int which, Color color)
  {
    VCodes[which].transform.GetChild(0).GetComponent<Image>().color = color;
  }
  
}

關(guān)于使用UGUI怎么實(shí)現(xiàn)一個(gè)4位數(shù)驗(yàn)證碼功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI