Unity工具类之生成文本验证码

作者:人生如逆旅,我亦是行人 时间:2021-06-21 03:38:42 

本文实例为大家分享了Unity生成文本验证码的具体代码,供大家参考,具体内容如下

文本验证码

由于我经常使用Unity进行webgl版本的开发,看到网站上面用户登录有很多的验证码验证。借鉴相关博客,写了Unity的工具类文本验证码,代码如下:

工具类:VerificationCode


using System.Collections;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 该工具类为:生成验证码
/// 作者:hys
/// 时间: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>
 /// 获取随机生成的验证码
 /// </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的非负随机数,Append将Length次随机的码进行拼接
     }
   return newRandom.ToString();
 }

}

Unity脚本


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HuangVerificationCodeTextScripts : MonoBehaviour
{
 private Text verificationCodeText; //验证码Text.
 private void Awake()
 {
   init();
 }
 void Start()
 {

}
 void Update()
 {

}
 /// <summary>
 /// 进行初始化
 /// </summary>
 private void init()
 {
   verificationCodeText = GameObject.Find("VerificationCodeText").GetComponent<Text>();
 }

/// <summary>
 /// 生成验证码
 /// </summary>
 /// <param name="length">验证码长度</param>
 /// <returns>字符串类型的验证码</returns>
 public string generateVerificationCode(int length)
 {
   string code= VerificationCode.SetDeleKey(length);
   verificationCodeText.text = code;
   return code;
 }

}

来源:https://blog.csdn.net/weixin_42132959/article/details/103762775

标签:Unity,验证码
0
投稿

猜你喜欢

  • Android仿微信联系人字母排序效果

    2021-10-01 16:06:25
  • SpringBoot整合阿里云短信服务的方法

    2022-03-24 18:17:11
  • android教程之使用popupwindow创建菜单示例

    2023-01-24 22:08:10
  • Android LayoutInflater.inflate源码分析

    2022-06-06 17:02:05
  • C#中this指针的用法示例

    2021-07-21 14:12:24
  • spring 注解如何开启声明式事务

    2023-04-09 23:07:42
  • Java设计模式七大原则之单一职责原则详解

    2022-05-12 20:48:58
  • 浅谈Spring自定义注解从入门到精通

    2023-11-25 03:59:12
  • Java实战之飞翔的小鸟小游戏

    2022-10-04 20:37:20
  • java读写oracle的blob字段示例

    2023-12-22 16:19:00
  • Android 消息机制问题总结

    2023-08-06 03:10:49
  • java导出Excel通用方法的实例详解

    2022-06-25 14:28:25
  • springboot注解Aspect实现方案

    2022-12-17 19:32:06
  • SpringBoot整合RabbitMQ实现六种工作模式的示例

    2021-10-17 06:21:08
  • 实例分析java对象的序列化和反序列化

    2022-07-11 00:22:41
  • 浅析C# 基础语法的使用

    2023-05-16 13:24:50
  • Java实现pdf转图片案例

    2022-08-11 21:45:41
  • Android CoordinatorLayout高级用法之自定义Behavior

    2022-03-20 05:47:51
  • C++异常处理 try,catch,throw,finally的用法

    2021-08-21 21:29:55
  • C语言入门篇--初识指针和指针变量

    2022-05-31 06:10:45
  • asp之家 软件编程 m.aspxhome.com