分享将代码变成如下所示的工具:http://www.planetb.ca/syntax-highlight-word
- 到官网下载SDK
- 将包导入
- 创建一个GameObject,将ShareSDK脚本添加上去
- 去MOB官网注册账号添加应用可以获取AppKey和AppSecret,获取后可以在官网查看分享次数和数据,可以不注册。
- 创建脚本ShareCtrl,引入命名空间cn.sharesdk.unity3d将如下代码添加进去
- 创建一个Button将OnBtnScreenCap添加上去,可以实现截屏分享功能。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using cn.sharesdk.unity3d;
- using System;
- public class ShareCtrl : MonoBehaviour {
- private ShareSDK share;
- void Start()
- {
- share = this.GetComponent<ShareSDK>();
- share.shareHandler = ShareHandler;
- share.authHandler = AuthHandler;
- share.showUserHandler = ShowUserHandler;
- }
- public void OnBtnScreenCap()
- {
- ScreenCapture.CaptureScreenshot("Screenshot.png");
- StartCoroutine("CaptureScreen");
- }
- private IEnumerator CaptureScreen()
- {
- yield return new WaitForSeconds(0.5f);
- string imagepath = Application.persistentDataPath + "/Screenshot.png";
- ShareContent content = new ShareContent();
- content.SetText("哈喽,我正在玩《FigBom》,一起来玩吧!");
- content.SetImagePath(imagepath);
- content.SetTitle("FigBom");
- content.SetTitleUrl("http://www.mob.com");
- content.SetShareType(ContentType.Webpage);
- //显示各个平台的分享图标列表
- PlatformType[] platforms = new PlatformType[4];
- platforms[0] = PlatformType.QQ;
- platforms[1] = PlatformType.WeChat;
- platforms[2] = PlatformType.QZone;
- platforms[3] = PlatformType.SinaWeibo;
- share.ShowPlatformList(platforms, content, 100, 100);
- }
- private void ShowUserHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
- {
- if (state == ResponseState.Success)
- {
- print("get user info result :");
- print(MiniJSON.jsonEncode(data));
- }
- else if (state == ResponseState.Fail)
- {
- print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);
- }
- else if (state == ResponseState.Cancel)
- {
- print("cancel !");
- }
- }
- private void AuthHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
- {
- if (state == ResponseState.Success)
- {
- print("authorize success !");
- }
- else if (state == ResponseState.Fail)
- {
- print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);
- }
- else if (state == ResponseState.Cancel)
- {
- print("cancel !");
- }
- }
- private void ShareHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
- {
- if (state == ResponseState.Success)
- {
- print("授权成功");
- }
- else if (state == ResponseState.Fail)
- {
- print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);
- }
- else if (state == ResponseState.Cancel)
- {
- print("cancel !");
- }
- }
- }