最近因為公司的項目需要添加一些實用性的功能,需要添加第三方登錄及分享,采用的是Mob的SDK,可以先到其官網下載對應的SDK 點擊這里,為了方便后期進行數據統計和分析,所以可以先添加一個應用,添加成功后,就可以獲取到對應的AppKey和App Secret,這兩個東西稍后需要用到。
准備工作
打開Unity,創建一個空的2D項目,因為只是用來測試第三方登錄及分享,所以可以不需要創建3D項目。然后把我們剛剛下載的Share SDK導入。導入完成后在Project下就會出現一個Plugins文件,這里面就是包含的是Mob為我們封裝好的Android和iOS的一些接口,不需要過多的去探究,只要會用就行。然后隨便選擇場景中一個物體,為它添加Share SDK組建,即在Inspector面板點擊AddComponent添加Share SDK腳本,然后將Mob官網上申請的應用的App Key及AppSecret替換組建上原有的數據。接下來我利用UGUI搭建了一個簡單的UI界面,用來實現登錄及分享功能。因為要發布到移動端測試,所以我做了一個假的控制台用來輸出信息,以便Debug使用。
1、首先先說最簡單的QQ分享及第三方登錄
進行QQ第三方登錄時,需要先到QQ互聯官網上申請一個自己的應用,如果只是為了測試功能,也可以不申請,直接使用Mob默認的Appid及AppSecret。接下來就是寫代碼了,很簡單。新建一個用來測試的腳本,我這里叫Login,先在Start中獲取到對應的按鈕引用,QQ登錄的代碼如下。
/// <summary> /// 退出按鈕 /// </summary> private Button exitBtn; /// <summary> /// QQ按鈕 /// </summary> private Button QQBtn; /// <summary> /// 微信按鈕 /// </summary> private Button weixinBtn; /// <summary> /// 微博按鈕 /// </summary> private Button weiboBtn; /// <summary> /// 分享按鈕 /// </summary> private Button ShareBtn; /// <summary> /// 用戶名輸入框 /// </summary> private InputField userName; /// <summary> /// 密碼輸入框 /// </summary> private InputField passWord; /// <summary> /// SDK /// </summary> public ShareSDK ssdk; /// <summary> /// 控制台 /// </summary> public Text text; /// <summary> /// 獲取到的用戶信息保存本地的文件名 /// </summary> private string fileName; /// <summary> /// 獲取到用戶的頭像保存到本地的文件名 /// </summary> private string iconName; /// <summary> /// 可以分享的平台 /// </summary> private PlatformType[] platforms; public void Start() { Debug.Log(ssdk); platforms = new PlatformType[] { PlatformType.WeChat,PlatformType.WeChatFavorites,PlatformType.WeChatMoments,PlatformType.WechatPlatform, PlatformType.QQ,PlatformType.QZone,PlatformType.SinaWeibo}; userName = transform.Find("UserName").GetComponentInChildren<InputField>(); passWord = transform.Find("PassWord").GetComponentInChildren<InputField>(); exitBtn = transform.Find("ExitBtn").GetComponent<Button>(); QQBtn = transform.Find("QQBtn").GetComponent<Button>(); weixinBtn = transform.Find("WeiXinBtn").GetComponent<Button>(); weiboBtn = transform.Find("WeiBoBtn").GetComponent<Button>(); ShareBtn = transform.Find("QQShareBtn").GetComponent<Button>(); exitBtn.onClick.AddListener(ExitButtonHandle); QQBtn.onClick.AddListener(QQButtonHandle); weixinBtn.onClick.AddListener(WeiXinButtonHandle); weiboBtn.onClick.AddListener(WeiBoButtonHandle); ShareBtn.onClick.AddListener(ShareButtonHandle); } /// <summary> /// QQ登錄 /// </summary> private void QQButtonHandle() { Debug.Log("點擊了QQ登錄"); fileName = "/qq.json"; iconName = "/qqIcon.jpg"; if (File.Exists(Application.persistentDataPath + fileName)) return; //注冊登錄回調事件 ssdk.authHandler = AuthHandler; //確定向哪個平台進行第三方驗證 ssdk.Authorize(PlatformType.QQ); } /// <summary> /// 登錄回調 /// </summary> /// <param name="reqID"></param> /// <param name="state"></param> /// <param name="type"></param> /// <param name="data"></param> private void AuthHandler(int reqID, ResponseState state, PlatformType type, Hashtable data) { Debug.Log("回調函數"); if (state == ResponseState.Success) { JsonData userData = JsonMapper.ToObject(JsonMapper.ToJson(data)); SaveUserInfo(JsonMapper.ToJson(data)); string icon = userData["icon"].ToString(); StartCoroutine(DownUserIcon(icon)); text.text += "\n userid : " + userData["userID"] + "\n username : " + userData["nickname"] + "\n icon : " + userData["icon"]; text.text += "\n授權成功!!!"; userName.text = userData["nickname"].ToString(); } else if (state == ResponseState.Fail) { text.text += "\n授權失敗!!!"; } } /// <summary> /// 分享 /// </summary> private void ShareButtonHandle() { //分享的內容 ShareContent content = new ShareContent(); if (iconName != null) content.SetImagePath(Application.persistentDataPath + iconName); content.SetTitle(" 分享 "); content.SetTitleUrl("https://www.baidu.com/"); content.SetText(" wecome "); content.SetSite("Mob-ShareSDK"); content.SetSiteUrl("https://www.baidu.com/"); content.SetUrl("https://www.baidu.com/"); content.SetComment("test description"); content.SetMusicUrl("http://fjdx.sc.chinaz.com/Files/DownLoad/sound1/201807/10300.mp3"); content.SetShareType(ContentType.Webpage); Debug.Log(" ******* 001 "); //注冊分享回調事件 ssdk.shareHandler = ShareHandler; //傳遞需要分享的平台及分享內容 ssdk.ShowPlatformList(platforms, content, 0, 0); } /// <summary> /// 分享回調 /// </summary> /// <param name="reqID"></param> /// <param name="state"></param> /// <param name="type"></param> /// <param name="data"></param> private void ShareHandler(int reqID, ResponseState state, PlatformType type, Hashtable data) { if (state == ResponseState.Success) { Debug.Log(" share is success "); Debug.Log(JsonMapper.ToJson(data)); } else if(state==ResponseState.Fail) { Debug.Log(" share is fail "); } } /// <summary> /// 將用戶的頭像下載 /// </summary> /// <param name="icon"></param> /// <returns></returns> private IEnumerator DownUserIcon(string icon) { Debug.Log("開啟協程進行資源下載"); WWW www = new WWW(icon); yield return www; FileStream stream = File.Create(Application.persistentDataPath + iconName); Texture2D texture = new Texture2D(www.texture.width, www.texture.height); www.LoadImageIntoTexture(texture); byte[] bytes = texture.EncodeToJPG(); stream.Write(bytes, 0, bytes.Length); stream.Close(); stream.Dispose(); } /// <summary> /// 將得到的用戶信息保存 /// </summary> /// <param name="jsonFile"></param> private void SaveUserInfo(string jsonFile) { if (File.Exists(Application.persistentDataPath + "/" + fileName)) File.Delete(Application.persistentDataPath + "/" + fileName); File.WriteAllText(Application.persistentDataPath + "/"+fileName, jsonFile); }
在分享的方法中后面的兩個參數
ssdk.ShowPlatformList(platforms, content, 0, 0);
如果去看源碼的話,就會發現沒有什么用的,所以傳什么數字都可以,我覺得過於雞肋,所以就將這兩個參數刪除了。實現上訴代碼后,就可以進行測試了,第三方登錄可以成功,且分享內容也沒問題了。
2、微信第三方登錄及分享
進行微信第三方登錄及分享時,需要先到微信開放平台申請屬於自己的應用,等待審核通過后,將獲取到的Appid及App secret填入Share SDK對應的微信平台的配置信息處,不過前提是你必須是要在微信開放平台的開發者信息提交並且審核通過了,因為直接創建出來的應用沒有第三方登錄權限,還需要申請開通,而申請開通的話就需要開發者資格認證,不過微信的開發者資格認證需要3張毛爺爺,真的心疼。這些都弄好之后,得到的appid及App Secret才可以使用,不過分享是都可以的。在開始需要自己打一個屬於自己的jar包,利用eclipse打開前面從Mob官網上下載的sdk包下的Android_Jave_Demo文件。
在eclipse中打開后,然后將cn.sharesdk.demo.apshare、cn.sharesdk.demo.wxapi、cn.sharesdk.demo.yxapi這三個包的包名改成自己包名,然后在src單擊右鍵,選擇導出,命名為DemoCallback.jar。
導出成功后,將Unity工程目錄下的Plugins/Android/ShareSDK/libs下的Demo Callback替換為剛剛自己導出的jar。
接下來則是對微信登錄及分享進行事件注冊,代碼和QQ的基本一樣。分享和QQ采用的是同一個方法,就不重復寫了
/// <summary> /// 微信登錄 /// </summary> private void WeiXinButtonHandle() { Debug.Log("點擊了微信登錄"); fileName = "/wechat.json"; iconName = "/wechatIcon.jpg"; if (File.Exists(Application.persistentDataPath + fileName)) return; ssdk.authHandler = AuthHandler; ssdk.Authorize(PlatformType.WeChat); }
微信分享及登錄功能到此就結束了。
3、微博的登錄及分享
微博跟微信是一樣的,也需要到微博的開發者平台先進行注冊及應用的申請,獲取到應用的App ID及AppSecret,然后將其填寫到微博對應的配置信息位置。微博登錄代碼如下,微博的分享功能也是直接和QQ的一樣的,所以使用了同一個方法。微博登錄可以只創建一個測試應用,然后不需要去申請審核,直接給這個應用添加測試賬號,不過最多只能添加15個,不過如果只是測試的話,這些是夠了的。然后就可以直接將得到的應用的App ID及AppSecret填入到對應位置即可。注:在填寫安卓包名的時候,必須要跟項目的包名一致。
/// <summary> /// 微博登錄 /// </summary> private void WeiBoButtonHandle() { Debug.Log("點擊了微博登錄"); fileName = "/sina.json"; iconName = "/sinaIcon.jpg"; if (File.Exists(Application.persistentDataPath + fileName)) return; ssdk.authHandler = AuthHandler; ssdk.Authorize(PlatformType.SinaWeibo); }
到此為止,微博登錄並沒有實現,你會發現在登錄的時候會出現21322錯誤,微博提示為重定向錯誤,這個是真的有點坑的。通過查找微博開放平台文檔才明白了,我們需要到微博的后台管理平台,找到自己申請的應用,點擊設置,進入到應用信息中的高級信息,在OAuth2.0授權設置中設置一個回調的網站,然后確定后即可,接下來是到Unity中在ShareSDK中的微博配置信息設置處的Redirect Url將剛剛設置的網站設置過來,這三個必須保持一致。
至此利用Mob實現Unity第三方登錄就結束了,如果需要源碼的可以到我的https://github.com/Iamdevelope/MobTest上獲取。
由於都是自己在摸索,所以難免會有很多的不足及錯誤,如文中有BUG或者錯誤的地方,還望大神指出,在此謝過了!