生活随笔
收集整理的這篇文章主要介紹了
unity(登录注册用手机号短信验证)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
短信驗(yàn)證
1、短信驗(yàn)證我是通過mob的SMSSDK實(shí)現(xiàn)的(free)
官網(wǎng)下載:http://www.mob.com/wiki/detailed?wiki=SMSSDK_for_Unity3D&id=23
2、在mob上注冊(cè)一個(gè)賬號(hào),創(chuàng)建應(yīng)用獲得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("填寫你注冊(cè)的key", "注冊(cè)的keySecret", false);ssdk
.setHandler(this);timer
.gameObject
.SetActive(false);}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");}}}public void SendCodeHandler(){lock_
= 0;if (phoneNum
.text
.Length
!= 11) {tip
.gameObject
.SetActive
(true);return;}isSend
= true;time
= 60;send
.gameObject
.SetActive(false);timer
.gameObject
.SetActive(true);ssdk
.getCode(CodeType
.TextCode
, phone
, "86", null);print
("phoneText"+phoneNum
.text
);}public void EnterCodeHandler(){if (code
.text
== "1234") {page
.SetActive
(false);print
("1234驗(yàn)證通過登錄成功");}ssdk
.commitCode(phone
, "86", code
.text
);}public void onComplete(int action
, object resp
){ActionType act
= (ActionType
)action
;if (resp
!= null){text
.text
+= "\n" + resp
.ToString();Debug
.Log(resp
.ToString());}if (act
== ActionType
.GetCode
){text
.text
+= "\n 驗(yàn)證成功!!!";string responseString
= (string)resp
;Debug
.Log("isSmart :" + responseString
);}}public void onError(int action
, object resp
){Debug
.Log("Error :" + resp
);text
.text
+= "\n 驗(yàn)證失敗!!!";text
.text
+= "\n Error : " + resp
;print("OnError ******resp" + resp
);}}
再掛上去即可
ps:這里的短信驗(yàn)證需要打包成.apk后才能驗(yàn)證
驗(yàn)證效果如下圖:
以及在mob查看驗(yàn)證記錄:
ps:
1、因?yàn)閙ob免費(fèi)所以這里驗(yàn)證次數(shù)是有限制的,如果你沒接收的短信可能是開始了智能驗(yàn)證,關(guān)閉即可。
2、這里是普通安卓應(yīng)用,如果想在vr設(shè)備中實(shí)現(xiàn)則需要搭建一個(gè)鍵盤,接下來將說明如何搭建一個(gè)鍵盤以及實(shí)現(xiàn)它的響應(yīng)。
3、在實(shí)現(xiàn)登錄注冊(cè)后需要保存用戶的賬號(hào)和密碼,這里就需要連接數(shù)據(jù)庫【我跳過與后臺(tái)的交互直接連接數(shù)據(jù)庫】,這里我將使用mysql來實(shí)現(xiàn)。
4、使用在mob官網(wǎng)上下載的SMSSDK,我在打包時(shí)出現(xiàn)了一個(gè)錯(cuò)誤,是因?yàn)槿缦聢D中的那張圖片缺失,把它換張圖片替換或刪除即可。
總結(jié)
以上是生活随笔為你收集整理的unity(登录注册用手机号短信验证)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。