這兩天在做一個(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("分享操作被取消");
? ? ? ? }
? ? }
}