Unity工具類之生成文本驗(yàn)證碼
本文實(shí)例為大家分享了Unity生成文本驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
文本驗(yàn)證碼
由于我經(jīng)常使用Unity進(jìn)行webgl版本的開發(fā),看到網(wǎng)站上面用戶登錄有很多的驗(yàn)證碼驗(yàn)證。借鑒相關(guān)博客,寫了Unity的工具類文本驗(yàn)證碼,代碼如下:
工具類:VerificationCode
using System.Collections; using System.Collections.Generic; using System.Text; /// <summary> /// 該工具類為:生成驗(yàn)證碼 /// 作者:hys /// 時(shí)間:2019.12.30 /// 郵箱:840917807@qq.com /// </summary> public class VerificationCode { private static char[] constant = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; /// <summary> /// 獲取隨機(jī)生成的驗(yàn)證碼 /// </summary> /// <param name="Length">長度</param> /// <returns></returns> public static string SetDeleKey(int Length) { StringBuilder newRandom = new StringBuilder(62); System.Random rd = new System.Random(); for (int i = 0; i < Length; i++) { newRandom.Append(constant[rd.Next(62)]); //rd.Next(62)返回小于62的非負(fù)隨機(jī)數(shù),Append將Length次隨機(jī)的碼進(jìn)行拼接 } return newRandom.ToString(); } }
Unity腳本
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HuangVerificationCodeTextScripts : MonoBehaviour { private Text verificationCodeText; //驗(yàn)證碼Text. private void Awake() { init(); } void Start() { } void Update() { } /// <summary> /// 進(jìn)行初始化 /// </summary> private void init() { verificationCodeText = GameObject.Find("VerificationCodeText").GetComponent<Text>(); } /// <summary> /// 生成驗(yàn)證碼 /// </summary> /// <param name="length">驗(yàn)證碼長度</param> /// <returns>字符串類型的驗(yàn)證碼</returns> public string generateVerificationCode(int length) { string code= VerificationCode.SetDeleKey(length); verificationCodeText.text = code; return code; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#基礎(chǔ)知識之GetType與typeof的區(qū)別小結(jié)
在比較對象時(shí),需要了解他們的類型,才能決定他們的值是否能比較。所有的類都從System.Object中繼承了GetType()方法,常常與typeo()運(yùn)算符一起使用。這篇文章主要給大家介紹了關(guān)于C#基礎(chǔ)知識之GetType與typeof區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-06-06Unity3D UI Text得分?jǐn)?shù)字增加的實(shí)例代碼
這篇文章主要介紹了Unity3D UI Text得分?jǐn)?shù)字增加方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04詳解C#如何對ListBox控件中的數(shù)據(jù)進(jìn)行操作
這篇文章主要為大家詳細(xì)介紹了C#中對ListBox控件中的數(shù)據(jù)進(jìn)行的操作,主要包括添加、刪除、清空、選擇、排序等,感興趣的小伙伴可以了解下2024-03-03