生活随笔
收集整理的這篇文章主要介紹了
unity(登录注册用手机号短信验证)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
短信驗證
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);}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驗證通過登錄成功");}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 驗證成功!!!";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,我在打包時出現了一個錯誤,是因為如下圖中的那張圖片缺失,把它換張圖片替換或刪除即可。
總結
以上是生活随笔為你收集整理的unity(登录注册用手机号短信验证)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。