1.5 unity 分享功能(登錄+分享)的實(shí)現(xiàn) (Android+微博+shareSDK為例)

這兩天在做一個(gè)分享功能,首先要在shareSDK官網(wǎng)和微博官網(wǎng)上獲取app key和app secret之類的信息 這樣的方法網(wǎng)上有很多? 就不一一贅述了

shareSDK進(jìn)行社會(huì)化分享(圖文教程)

最終的代碼實(shí)現(xiàn) 給貼上


using UnityEngine;

using System.Collections;

using System.IO;

using System.Text.RegularExpressions;

using cn.sharesdk.unity3d;

using UnityEngine.UI;

public class ShareButton : MonoBehaviour

{?

?????private ShareSDK ssdk;?

?????private Camera camera1;?

?????private static ShareButton _instance;?

?????public static ShareButton Instance { get { return _instance; } }?

?????void Start ()?

?????{

?????????_instance = this;

?????????DontDestroyOnLoad(gameObject);?

?????????ssdk = GetComponent();?

?????????Instance.ssdk.shareHandler = OnShareResultHandler;//注冊(cè)回調(diào)函數(shù)

?????????camera1 = GameObject.Find("Main Camera").GetComponent();

????}

? ? public void OnShareButtonClick()

? ? {

? ? ? ? OnScreenShot();//自定義截屏

? ? ? ? ShareContent content = new ShareContent();//分享前,創(chuàng)建對(duì)象

? ? ? ? content.SetText("恭喜您獲得數(shù)碼獸");//分享文字

? ? ? ? content.SetImagePath("/sdcard/DCIM/screensshots/ScreenShot.png");//分享圖片

? ? ? ? //設(shè)置分享來(lái)源的站點(diǎn)與站點(diǎn)的url

? ? ? ? content.SetSite("鼴鼠游戲");

? ? ? ? content.SetSiteUrl("http://www.baidu.com");

? ? ? ? content.SetShareType(ContentType.Image);//設(shè)定分享內(nèi)容的主要類型

? ? ? ? //PlatformType[] platforms = {PlatformType.SinaWeibo, PlatformType.WeChat};

? ? ? ? //設(shè)置哪些平臺(tái)不顯示 具體參數(shù) PlatformType 到定義出查看 只對(duì)安卓有效? ios的只需上一行的方法就行了

? ? ? ? string[] platfsList = {"2", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "23",

? ? ? ? ? ? "24", "25", "26", "27", "30", "34",? "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45","46", "48", "50", "51",

? ? ? ? ? ? "52", "53", "54" };

? ? ? ? content.SetHidePlatforms(platfsList);

? ? ? ? ssdk.ShowPlatformList(null,content,100,100);

? ? }

? ? void OnScreenShot()

? ? {

? ? ? ? RenderTexture rt= new RenderTexture(Screen.width,Screen.height,1);

? ? ? ? camera1.targetTexture = rt;

? ? ? ? camera1.Render();

? ? ? ? RenderTexture.active = rt;

? ? ? ? //命名圖片

? ? ? ? string fileName = "ScreenShot.png";

? ? ? ? if (Application.platform == RuntimePlatform.Android)

? ? ? ? {

? ? ? ? ? ? //截屏

? ? ? ? ? ? Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

? ? ? ? ? ? texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

? ? ? ? ? ? texture.Apply();

? ? ? ? ? ? camera1.targetTexture = null;

? ? ? ? ? ? RenderTexture.active = null;

? ? ? ? ? ? Destroy(rt);

? ? ? ? ? ? //轉(zhuǎn)化為2進(jìn)制數(shù)組

? ? ? ? ? ? byte[] bytes = texture.EncodeToPNG();

? ? ? ? ? ? //存儲(chǔ)路徑

? ? ? ? ? ? string destination = "/sdcard/DCIM/screensshots";

? ? ? ? ? ? //若沒(méi)路徑 創(chuàng)建

? ? ? ? ? ? if (!Directory.Exists((destination)))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Directory.CreateDirectory(destination);

? ? ? ? ? ? }

? ? ? ? ? ? //保存路徑

? ? ? ? ? ? string pathSave = destination + "/" + fileName;

? ? ? ? ? ? //寫入圖片

? ? ? ? ? ? File.WriteAllBytes(pathSave, bytes);

? ? ? ? }

? ? }

? ? void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)

? ? {

? ? ? ? //并非所有平臺(tái)都會(huì)報(bào)告正確的狀態(tài)

? ? ? ? if (state == ResponseState.Success)

? ? ? ? {

? ? ? ? ? ? Utility.MakeToast("分享成功");

? ? ? ? }

? ? ? ? else if (state == ResponseState.Fail)

? ? ? ? {

? ? ? ? ? ? Utility.MakeToast("分享失敗");

? ? ? ? }

? ? ? ? else if (state == ResponseState.Cancel)

? ? ? ? {

? ? ? ? ? ? Utility.MakeToast("分享操作被取消");

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 美食,名詞。美味的食物,包括飲食。也可以理解為美麗的食物吧。 那么,是不是也可以理解為美美地食用,動(dòng)詞,以美麗的心...
    我這半輩子閱讀 348評(píng)論 0 3
  • 1 暴雨在瘋狂地下著,明亮的閃電像銀蛇一樣在空中穿梭著,一次又一次地照亮了整個(gè)屋子,轟隆隆的雷聲震耳欲聾,好像可以...
    呆萌的老張看世界687閱讀 1,507評(píng)論 27 51
  • “不過(guò)是幻覺(jué)罷了!”落雪瑩在做作業(yè)時(shí)又產(chǎn)生幻覺(jué)了。她的臉上露出了一種微妙的神情。在薔薇花的海洋里,她看見(jiàn)了她,她笑...
    靈薔薇閱讀 435評(píng)論 1 3
  • Craquelure–它代表的不是蒼老和脆弱,他是歷經(jīng)百年之久堅(jiān)毅而且堅(jiān)強(qiáng)的象征!
    Craquelure閱讀 169評(píng)論 0 0

友情鏈接更多精彩內(nèi)容