欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-04-18 09:48:47|栏目:.NET代码|点击:

本文实例为大家分享了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;
  }

}

上一篇:ASP.NET 页面中动态增加的控件、添加事件第1/2页

栏    目:.NET代码

下一篇:C#将时间转成文件名使用方法

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

本文地址:http://www.codeinn.net/misctech/103537.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有