ShareSDK 简单分享功能实现

分享将代码变成如下所示的工具:http://www.planetb.ca/syntax-highlight-word


  • 到官网下载SDK


  • 将包导入


  • 创建一个GameObject,将ShareSDK脚本添加上去


  • 去MOB官网注册账号添加应用可以获取AppKey和AppSecret,获取后可以在官网查看分享次数和数据,可以不注册。


  • 创建脚本ShareCtrl,引入命名空间cn.sharesdk.unity3d将如下代码添加进去
  • 创建一个Button将OnBtnScreenCap添加上去,可以实现截屏分享功能。


  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using cn.sharesdk.unity3d;  
  5. using System;  
  6.   
  7. public class ShareCtrl : MonoBehaviour {  
  8.   
  9.     private ShareSDK share;  
  10.   
  11.     void Start()  
  12.     {  
  13.         share = this.GetComponent<ShareSDK>();  
  14.         share.shareHandler = ShareHandler;  
  15.         share.authHandler = AuthHandler;  
  16.         share.showUserHandler = ShowUserHandler;  
  17.     }  
  18.   
  19.     public void OnBtnScreenCap()  
  20.     {  
  21.         ScreenCapture.CaptureScreenshot("Screenshot.png");  
  22.         StartCoroutine("CaptureScreen");  
  23.     }  
  24.     private IEnumerator CaptureScreen()  
  25.     {  
  26.         yield return new WaitForSeconds(0.5f);  
  27.         string imagepath = Application.persistentDataPath + "/Screenshot.png";  
  28.   
  29.         ShareContent content = new ShareContent();  
  30.         content.SetText("哈喽,我正在玩《FigBom》,一起来玩吧!");  
  31.         content.SetImagePath(imagepath);  
  32.         content.SetTitle("FigBom");  
  33.         content.SetTitleUrl("http://www.mob.com");  
  34.         content.SetShareType(ContentType.Webpage);  
  35.   
  36.         //显示各个平台的分享图标列表    
  37.         PlatformType[] platforms = new PlatformType[4];  
  38.         platforms[0] = PlatformType.QQ;  
  39.         platforms[1] = PlatformType.WeChat;  
  40.         platforms[2] = PlatformType.QZone;  
  41.         platforms[3] = PlatformType.SinaWeibo;  
  42.         share.ShowPlatformList(platforms, content, 100, 100);  
  43.     }  
  44.     private void ShowUserHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)  
  45.     {  
  46.         if (state == ResponseState.Success)  
  47.         {  
  48.             print("get user info result :");  
  49.             print(MiniJSON.jsonEncode(data));  
  50.         }  
  51.         else if (state == ResponseState.Fail)  
  52.         {  
  53.             print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);  
  54.         }  
  55.         else if (state == ResponseState.Cancel)  
  56.         {  
  57.             print("cancel !");  
  58.         }  
  59.     }  
  60.   
  61.     private void AuthHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)  
  62.     {  
  63.         if (state == ResponseState.Success)  
  64.         {  
  65.             print("authorize success !");  
  66.         }  
  67.         else if (state == ResponseState.Fail)  
  68.         {  
  69.             print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);  
  70.         }  
  71.         else if (state == ResponseState.Cancel)  
  72.         {  
  73.             print("cancel !");  
  74.         }  
  75.     }  
  76.   
  77.     private void ShareHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)  
  78.     {  
  79.         if (state == ResponseState.Success)  
  80.         {  
  81.             print("授权成功");  
  82.         }  
  83.         else if (state == ResponseState.Fail)  
  84.         {  
  85.             print("fail! throwable stack = " + data["stack"] + "; error msg = " + data["msg"]);  
  86.         }  
  87.         else if (state == ResponseState.Cancel)  
  88.         {  
  89.             print("cancel !");  
  90.         }  
  91.     }  
  92. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值