CaptchaController.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. /* Http控制器-验证码 */
  7. public class CaptchaController : JCUnityLib.Singleton<CaptchaController>
  8. {
  9. int[] builtInCaptchaList = {3825, 4472, 9483};
  10. public IEnumerator GetCaptcha(Image targetRenderImage, Action<int> cb)
  11. {
  12. if (targetRenderImage.sprite == null)
  13. {
  14. int codeA = builtInCaptchaList[UnityEngine.Random.Range(0, 3)];
  15. targetRenderImage.sprite = Resources.Load<Sprite>("Captcha/" + codeA);
  16. if (cb != null) cb(codeA);
  17. }
  18. int code = UnityEngine.Random.Range(1000, 10000);
  19. using (UnityWebRequest uwr = new UnityWebRequest(CommonConfig.gateServerURL + "/api/createCaptcha?code=" + code, UnityWebRequest.kHttpVerbGET))
  20. {
  21. uwr.downloadHandler = new DownloadHandlerTexture();
  22. yield return uwr.SendWebRequest();
  23. Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
  24. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  25. targetRenderImage.sprite = sprite;
  26. if (cb != null) cb(code);
  27. }
  28. }
  29. }