短信验证
1、短信验证我是通过mob的SMSSDK实现的(free)
官网下载:http://www.mob.com/wiki/detailed?wiki=SMSSDK_for_Unity3D&id=23
2、在mob上注册一个账号,创建应用获得key和secret
替换案例中demo中的key和secret,即可
由于案例中的ui是在c#中搭建的,故若想自己搭建可视化ui可按照如下操作
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using cn.SMSSDK.Unity;public class passW : MonoBehaviour, SMSSDKHandler
{private SMSSDK ssdk;public InputField code;public InputField phoneNum;public Button enter;public Button send;private string codeNum;private string phone;public Text timer;private bool isSend;private int time;private float t;public Text text;public GameObject page;public bool flag = false;public Text tip;private int count = 5;private int lock_ = 0;private bool lock_1 = false;void Start(){count = 5;ssdk = gameObject.GetComponent<SMSSDK>();ssdk.init("填写你注册的key", "注册的keySecret", false);ssdk.setHandler(this);timer.gameObject.SetActive(false);//phoneText = phoneNum.transform.Find ("Text").GetComponent<Text> ();}private void Update(){if (isSend){//倒计时timer.text = time.ToString();t += Time.deltaTime;if (t >= 1){time--;t = 0;}if (time < 0){isSend = false;send.gameObject.SetActive(true);timer.gameObject.SetActive(false);}}if (lock_ == 0) {if (phoneNum.isFocused) {lock_ = -1; tip.gameObject.SetActive (false);print ("phone click");lock_1 = true;} else if (code.isFocused && lock_1) {lock_ = 1;print ("code click");}}}/// <summary>/// 发送验证码/// </summary>public void SendCodeHandler(){//tip.text += phoneNum.text;lock_ = 0;if (phoneNum.text.Length != 11) {tip.gameObject.SetActive (true);return;}//GUI.Label (new Rect(50,50,50,50),"您的手机号不正确");isSend = true;time = 60;send.gameObject.SetActive(false);timer.gameObject.SetActive(true);ssdk.getCode(CodeType.TextCode, phone, "86", null);print ("phoneText"+phoneNum.text);}/// <summary>/// 点击确定,对比验证码/// </summary>public void EnterCodeHandler(){if (code.text == "1234") {page.SetActive (false);print ("1234验证通过登录成功");}ssdk.commitCode(phone, "86", code.text);}public void onComplete(int action, object resp){ActionType act = (ActionType)action;if (resp != null){//result = resp.ToString();text.text += "\n" + resp.ToString();Debug.Log(resp.ToString());}if (act == ActionType.GetCode){text.text += "\n 验证成功!!!";string responseString = (string)resp;Debug.Log("isSmart :" + responseString);}}public void onError(int action, object resp){Debug.Log("Error :" + resp);text.text += "\n 验证失败!!!";text.text += "\n Error : " + resp;print("OnError ******resp" + resp);}}
再挂上去即可
ps:这里的短信验证需要打包成.apk后才能验证
验证效果如下图:
以及在mob查看验证记录:
ps:
1、因为mob免费所以这里验证次数是有限制的,如果你没接收的短信可能是开始了智能验证,关闭即可。
2、这里是普通安卓应用,如果想在vr设备中实现则需要搭建一个键盘,接下来将说明如何搭建一个键盘以及实现它的响应。
3、在实现登录注册后需要保存用户的账号和密码,这里就需要连接数据库【我跳过与后台的交互直接连接数据库】,这里我将使用mysql来实现。
4、使用在mob官网上下载的SMSSDK,我在打包时出现了一个错误,是因为如下图中的那张图片缺失,把它换张图片替换或删除即可。