当前位置:   article > 正文

unity通过点击按钮获取手机验证码_unity手机验证码

unity手机验证码
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.Networking;
  4. using UnityEngine;
  5. using System;
  6. using System.Text;
  7. using UnityEngine.UI;
  8. using DG.Tweening;
  9. using LitJson;
  10. using UniRx;使用UniRx 插件链接:https://link.zhihu.com/?target=https%3A//assetstore.unity.com/packages/tools/integration/unirx-reactive-extensions-for-unity-17276
  11. public class YanZhen : MonoBehaviour
  12. {
  13. public InputField phoneNumberInput;//手机号输入框
  14. public InputField VerificationcodeInput;//验证码输入框
  15. private Button sent;
  16. private Text times;
  17. private bool ison;
  18. private int time;
  19. private float t;
  20. private float movedis;//移动的距离
  21. private void Start()
  22. {
  23. //查找当前层级下的控件
  24. sent = transform.Find("send").GetComponent<Button>();
  25. times = transform.Find("Timer").GetComponent<Text>();
  26. times.gameObject.SetActive(false);
  27. sent.onClick.AddListener(LoginButton);
  28. movedis = GetComponent<RectTransform>().rect.height;
  29. //使用litjson创建json格式的参数数据
  30. //JsonData data = new JsonData();
  31. //data["phone"] = "13759190279";
  32. }
  33. // Update is called once per frame
  34. /// <summary>
  35. /// 发送验证码
  36. /// </summary>
  37. private void Update()//验证码时间
  38. {
  39. if (ison)
  40. {
  41. times.text = time.ToString();
  42. t += Time.deltaTime;
  43. if (t >= 1)
  44. {
  45. time--;
  46. t = 0;
  47. }
  48. if (time < 0)
  49. {
  50. ison = false;
  51. sent.gameObject.SetActive(true);
  52. times.gameObject.SetActive(false);
  53. }
  54. }
  55. }
  56. public void LoginButton()//方法:点击按钮调用这个方法
  57. {
  58. string strSend = GetInputDate();//获取输入框字符串
  59. //使用litjson创建json格式的参数数据
  60. JsonData data = new JsonData();
  61. data["phone"] = strSend;
  62. if (strSend != null)
  63. {
  64. ison = true;
  65. time = 120;
  66. sent.gameObject.SetActive(false);
  67. times.gameObject.SetActive(true);
  68. byte[] postBytes = System.Text.Encoding.Default.GetBytes(data.ToJson());
  69. StartCoroutine(UnityWebRequestPost("后台url地址", postBytes));
  70. //使用UniRx
  71. ObservableWWW.Post("后台url地址", postBytes, new Dictionary<string, string>() { { "Content-Type", "application/json" } }).Subscribe(result => Debug.Log(result));
  72. }
  73. else
  74. {
  75. ison = false;
  76. sent.gameObject.SetActive(true);
  77. times.gameObject.SetActive(false);
  78. Debug.Log("手机号为空,请输入!");
  79. }
  80. }
  81. public string GetInputDate()//合并字符串方法
  82. {
  83. if (phoneNumberInput.text != "" /*&& VerificationcodeInput.text != ""*/)
  84. {
  85. //获取输入框文本,并剔除掉空格
  86. string userName = phoneNumberInput.text.Replace(" ", "");
  87. //string password = VerificationcodeInput.text.Replace(" ", "");
  88. //将字符串合并
  89. string strSend = userName ;
  90. return strSend;
  91. }
  92. return null;
  93. }
  94. IEnumerator UnityWebRequestPost(string url, byte[] postBytes)
  95. {
  96. UnityWebRequest request = new UnityWebRequest(url, "POST");
  97. request.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
  98. request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  99. request.SetRequestHeader("Content-Type", "application/json");
  100. yield return request.SendWebRequest();
  101. Debug.Log("Status Code: " + request.responseCode);
  102. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  103. {
  104. Debug.LogError(request.error);
  105. }
  106. else
  107. {
  108. Debug.Log(request.downloadHandler.text);
  109. }
  110. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/233571
推荐阅读
相关标签
  

闽ICP备14008679号