Unity3D for SMSSDK Android短信驗證開發文檔

以下轉自http://blog.csdn.net/cjb_king/article/details/75147859

Unity3D for SMSSDK Android短信驗證開發文檔

         SMSSDK的Unity3D插件主要爲用戶提供了兩種集成的方式:一種是通過橋接文件直接調用SMSSDK的原生API,另外一種是使用SMSSDKGUI中的UI。這兩種方式的集成,方便用戶按需選擇。

在進行集成之前,需要到Mob官網獲取使用的appKey和appSerect。

 

集成部分:

 

1、下載SMSSDK的Unity3D的工具類

打開Github下載Unity3D-For-SMSSDK項目。將項目中的Unity3DForSMSSDK/Assets/Plugins目錄拷貝到您的項目的Assets目錄中,或雙擊SMSSDKPackageForUnity.unitypackage導入相關文件。注意該操作可能會覆蓋您原來已經存在的文件!

 

2、掛接SMSSDK腳本

選擇好需要掛接的GameObject(例如MainCamera),在右側欄中點擊Add Component,選擇SMSSDK 進行掛接。如果需要使用Demo.cs文件,也需要進行掛接主相機。方法同掛接SMSSDK相同。

 

3、使用SDK

首先,要先引入命名空間,using cn.SMSSDK.Unity,並聲明SMSSDK.cs的實例,如下:private SMSSDK smssdk,並且進行設置:smssdk= gameObject.GetComponet();

其次,初始化SDK,在初始化SDK的方法的中,需要用戶appKey和appSerect(可從Mob官網上面獲得),如下:smssdk.init(“114d7a34cf7ea”,”678ff550d7328de446585757c4e5de3f”,false);
    再次,實現SMSSDKHandler並將它設置給SMSSDK,用來處理回調,如下圖:


現在你就可以使用我們提供的API實現自己的功能了,如下:

  a、獲取驗證碼:

 smssdk.getCode(CodeType.TextCode,”86″,”186*******″);

  b、提交驗證碼:

 smssdk.commitCode (“186********”, "86",code);

  c、獲取通訊錄好友信息:
     smssdk.getFriends();

  d、獲取國家支持列表:

 smssdk.getSupportedCountryCode ();

  e、提交用戶資料:

 smssdk.submitUserInfo (userInfo);

  f、獲取SDK版本號:

 smssdk.getVersion ();

  j、設置訪問通訊錄權限

 smssdk.enableWarn (true)

關於回調數據的處理:

有些API將會把回調數據發到你設置的SMSSDKHandler。回調數據(resp)是一個json字符串。你可以使用任意的JSON庫進行處理。其中action是你調用的API的類型。

onComplete(int action, object resp):當操作完成並且無錯誤時會調用此方法,對於一些需要返回值的操作,可以在此方法里根據action類型來對返回值做出不同的處理
    onError(int action, object resp):當操作出現錯誤時會調用此方法,

關於UI 部分

以上幾個接口是單純的使用API,並沒有牽涉到相關界面,如果開發者需要使用Mob官網中SMSSDK Demo中UI部分,我們也提供了使用UI的相關方法,如下:

showRegisterPage(CodeType getCodeMethodType)
    showContactsPage():會顯示邀請通訊錄聯繫人面板,並且返回一個json字符串,包含手機內所有聯繫人的聯繫方式

至此,Unity3D插件部分的工作已經完成。

Android部分

我們默認集成了Android 的SMSSDKGUI,如果你不需要(即不使用上面的兩個UI方法),可以將 Assets/Plugins/Android/ShortMessageSDKGUI文件夾刪除。如果你想自定義這個GUI,你可以在SMSSDK-for-Android中找到這個GUI開源庫,你可以任意修改它,然後打包成Unity能使用的插件


以下轉自http://blog.csdn.net/qq_33747722/article/details/70409198

在遊戲開發中登錄、修改密碼等情況下往往需要使用短信驗證

本文介紹在Unity中訪問SMSSDK來實現短信驗證這一功能

實現了在Android平臺上運行,我已經上傳了本文源碼點擊打開鏈接,大家可以下載參考學習(含APK、SMSDK)

大家也可以訪問其官網來下載最新SMSSDK

準備開發環境

解壓下載下來的SMSSDK,按下圖路徑將Plugins目錄拖到Unity Asset目錄下

按如下圖步驟切換到Android平臺下開發

接下來新建一個Scene,在Main Camera(可以選擇其他遊戲對象)下添加SMSSDK腳本組件

添加上需要填寫App Key和App Serect,這個需要在官網註冊獲取,獲取後填寫進去

編寫代碼

新建一個C#腳本掛載到Main Camera上(其他對象也行)

    using UnityEngine;  
    using System.Collections;  
    using UnityEngine.UI;  
    using cn.SMSSDK.Unity;  
      
    public class MySelfDemoText : MonoBehaviour ,SMSSDKHandler//實現接口,用於設置回調  
    {  
        public InputField PhoneNumberInput;  
        public InputField VerificationInput;  
      
        public Text respText;  
        public Text MessText;  
      
        private SMSSDK smssdk;  
      
        private UserInfo userInfo;  
      
        private string phoneNum = "";  
        private string zone = "86";  
      
        private string appKey = "1d3d7e144fc11";  
        private string appSerect = "056678a6f8570d521368968e80d62b91";  
      
        // Use this for initialization  
        void Start ()   
        {  
            smssdk = GameObject.Find("Main Camera").GetComponent<SMSSDK>();  
            smssdk.init(appKey,appSerect,false);//初始化SDK  
      
            smssdk.setHandler(this);//設置回調,用戶處理從客戶端返回的信息  
      
            userInfo = new UserInfo();  
        }  
          
        // Update is called once per frame  
        void Update ()   
        {  
            if (Input.GetKeyDown(KeyCode.Escape))  
            {  
                Application.Quit();  
            }  
        }  
      
        public void OnBtnVerification()  
        {  
            phoneNum = PhoneNumberInput.text;  
            Debug.Log(phoneNum);  
            //通過手機號獲取驗證碼  
            smssdk.getCode(CodeType.TextCode,phoneNum,zone);  
        }  
      
        public void OnBtnOK()  
        {  
            //提交驗證碼  
            smssdk.commitCode(phoneNum,zone,VerificationInput.text);  
        }  
      
        public void onComplete(int action, object resp)  
        {  
            ActionType act = (ActionType)action;  
            if (resp != null)  
            {  
                respText.text = resp.ToString();  
            }  
            if (act == ActionType.GetCode)  
            {  
                string responseString = (string)resp;  
                Debug.Log("isSmart :" + responseString);  
                MessText.text = "isSmart :" + responseString;  
            }  
            else if (act == ActionType.GetVersion)  
            {  
                string version = (string)resp;  
                MessText.text = "version :" + version;  
            }  
            else if (act == ActionType.GetSupportedCountries)  
            {  
      
                string responseString = (string)resp;  
                Debug.Log("zoneString :" + responseString);  
                MessText.text = "zoneString :" + responseString;  
      
            }  
            else if (act == ActionType.GetFriends)  
            {  
                string responseString = (string)resp;  
                Debug.Log("friendsString :" + responseString);  
                MessText.text = "friendsString :" + responseString;  
            }  
            else if (act == ActionType.CommitCode)  
            {  
      
                string responseString = (string)resp;  
                Debug.Log("commitCodeString :" + responseString);  
                MessText.text = "commitCodeString :" + responseString;  
      
            }  
            else if (act == ActionType.SubmitUserInfo)  
            {  
      
                string responseString = (string)resp;  
                Debug.Log("submitString :" + responseString);  
                MessText.text = "submitString :" + responseString;  
            }  
            else if (act == ActionType.ShowRegisterView)  
            {  
      
                string responseString = (string)resp;  
                Debug.Log("showRegisterView :" + responseString);  
                MessText.text = "showRegisterView :" + responseString;  
            }  
            else if (act == ActionType.ShowContractFriendsView)  
            {  
      
                string responseString = (string)resp;  
                Debug.Log("showContractFriendsView :" + responseString);  
                MessText.text = "showContractFriendsView :" + responseString;  
            }  
        }  
      
        public void onError(int action, object resp)  
        {  
            Debug.Log("Error :" + resp);  
            respText.text = resp.ToString();  
        }  
    }  
最後將其打包成APK,需要做下如下圖修改



之後便能在Android上接收短信並完成驗證
源碼我會一併上傳,提供大家參考
如果本文對你有幫助,請多多支持,轉發註明出處,謝謝!!!

==================================================================================================================================

20180102

最新的SMSSDK版本3.1.0中,獲取驗證碼的方法GetCode參數變成了4個,最後一個多出來的參數直接傳null即可!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章