Advertisement
halleman19

captcha in unity3d

Jan 23rd, 2025 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | Software | 0 0
  1. using UnityEngine;
  2.  
  3. namespace avstn.captcha
  4. {
  5.     public delegate void CaptchaHandler();
  6.  
  7.     public class Captcha : MonoBehaviour
  8.     {
  9.         public int lengthCode;        
  10.  
  11.         public string dictWord;
  12.         public char dictSeparator;
  13.  
  14.         private int indexSymb = 0;
  15.  
  16.         private int countError = 0;
  17.  
  18.         private bool isDisplay = false;
  19.  
  20.         private string[] dictionary;
  21.  
  22.         private string[] captchaCode;
  23.  
  24.         public Texture bgImage;
  25.         public Texture borderSymbImg;
  26.  
  27.         public Rect rBgImage;
  28.  
  29.         public Vector2 symbOffs;
  30.         public Vector2 symbSize;
  31.  
  32.         public GUIStyle styleSymb;
  33.  
  34.         public event CaptchaHandler notifySuccess;
  35.         public event CaptchaHandler notifyError;
  36.  
  37.         private const int MAX_ERROR_INPUT = 2;
  38.  
  39.         private void Awake()
  40.         {
  41.             dictionary = dictWord.Split(dictSeparator);
  42.  
  43.             if (lengthCode == 0)
  44.                 lengthCode = 5;
  45.         }
  46.  
  47.         private void OnGUI()
  48.         {
  49.             if (isDisplay)
  50.             {
  51.                 displayCaptcha();
  52.                 keyDownSymb();
  53.             }
  54.         }
  55.  
  56.         public void run(int length)
  57.         {
  58.             captchaCode = createCode(length, dictionary);
  59.  
  60.             isDisplay = true;
  61.         }
  62.  
  63.         private void displayCaptcha()
  64.         {
  65.             GUI.DrawTexture(rBgImage, bgImage);
  66.  
  67.             for (int i = 0; i < captchaCode.Length; i++)
  68.                 GUI.Box(new Rect(new Vector2( symbOffs.x + (i * symbSize.x), symbOffs.y ), symbSize), captchaCode[i], styleSymb);
  69.  
  70.             GUI.DrawTexture(new Rect( new Vector2( symbOffs.x + (indexSymb * symbSize.x), symbOffs.y ), symbSize ), borderSymbImg);
  71.         }
  72.  
  73.         private string[] createCode(int length, string[] dictionary)
  74.         {
  75.             string[] code = new string[length];
  76.  
  77.             for (int i = 0; i < length; i++)
  78.                 code[i] = dictionary[Random.Range(0, dictionary.Length - 1)];
  79.  
  80.             return code;
  81.         }
  82.  
  83.         private void keyDownSymb()
  84.         {
  85.             Event ev = Event.current;
  86.  
  87.             if (ev.type == EventType.KeyDown)
  88.             {
  89.                 KeyCode key = ev.keyCode;
  90.  
  91.                 if (key != KeyCode.None)
  92.                 {
  93.                     if (key.ToString() == captchaCode[indexSymb])
  94.                     {
  95.                         indexSymb++;
  96.  
  97.                         if (indexSymb >= lengthCode)
  98.                         {
  99.                             reset();
  100.  
  101.                             if (notifySuccess != null)
  102.                                 notifySuccess();
  103.                         }
  104.                     }
  105.                     else
  106.                     {
  107.                         countError++;
  108.  
  109.                         if (countError >= MAX_ERROR_INPUT)
  110.                         {
  111.                             reset();
  112.  
  113.                             if (notifyError != null)
  114.                                 notifyError();
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.  
  121.         private void reset()
  122.         {
  123.             indexSymb = 0;
  124.             countError = 0;
  125.             isDisplay = false;
  126.         }
  127.     }
  128. }
  129.  
  130. // example of work and how to connect to your game https://youtu.be/5I46ngrCX3k
  131. // my parametrs in inspector https://imgur.com/a/dF3YEAb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement