Demo.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using cn.sharesdk.unity3d;
  6. public class Demo : MonoBehaviour {
  7. public GUISkin demoSkin;
  8. public ShareSDK ssdk;
  9. // Use this for initialization
  10. void Start ()
  11. {
  12. ssdk = gameObject.GetComponent<ShareSDK>();
  13. ssdk.authHandler = OnAuthResultHandler;
  14. ssdk.shareHandler = OnShareResultHandler;
  15. ssdk.showUserHandler = OnGetUserInfoResultHandler;
  16. ssdk.getFriendsHandler = OnGetFriendsResultHandler;
  17. ssdk.followFriendHandler = OnFollowFriendResultHandler;
  18. }
  19. // Update is called once per frame
  20. void Update ()
  21. {
  22. if (Input.GetKeyDown(KeyCode.Escape)) {
  23. Application.Quit();
  24. }
  25. }
  26. void OnGUI ()
  27. {
  28. GUI.skin = demoSkin;
  29. float scale = 1.0f;
  30. if (Application.platform == RuntimePlatform.IPhonePlayer)
  31. {
  32. scale = Screen.width / 320;
  33. }
  34. float btnWidth = 165 * scale;
  35. float btnHeight = 30 * scale;
  36. float btnTop = 20 * scale;
  37. float btnGap = 20 * scale;
  38. GUI.skin.button.fontSize = Convert.ToInt32(14 * scale);
  39. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Authorize"))
  40. {
  41. print(ssdk == null);
  42. ssdk.Authorize(PlatformType.QQ);
  43. }
  44. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 + btnGap, btnTop, btnWidth, btnHeight), "Get User Info"))
  45. {
  46. ssdk.GetUserInfo(PlatformType.SinaWeibo);
  47. }
  48. btnTop += btnHeight + 20 * scale;
  49. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Show Share Menu"))
  50. {
  51. ShareContent content = new ShareContent();
  52. content.SetText("this is a test string.");
  53. content.SetImageUrl("http://ww3.sinaimg.cn/mw690/be159dedgw1evgxdt9h3fj218g0xctod.jpg");
  54. content.SetTitle("test title");
  55. content.SetTitleUrl("http://www.mob.com");
  56. content.SetSite("Mob-ShareSDK");
  57. content.SetSiteUrl("http://www.mob.com");
  58. content.SetUrl("http://www.mob.com");
  59. content.SetComment("test description");
  60. content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
  61. content.SetShareType(ContentType.Image);
  62. //不同平台分享不同内容
  63. ShareContent customizeShareParams = new ShareContent();
  64. customizeShareParams.SetText("Sina share content");
  65. customizeShareParams.SetImageUrl("http://git.oschina.net/alexyu.yxj/MyTmpFiles/raw/master/kmk_pic_fld/small/107.JPG");
  66. customizeShareParams.SetShareType(ContentType.Text);
  67. customizeShareParams.SetObjectID("SinaID");
  68. content.SetShareContentCustomize(PlatformType.SinaWeibo, customizeShareParams);
  69. //优先客户端分享
  70. // content.SetEnableClientShare(true);
  71. //使用微博高级接口进行本地图片 文字 应用内分享 17年6月30日后需申请高级接口
  72. // content.SetEnableAdvancedInterfaceShare(true);
  73. //通过分享菜单分享
  74. ssdk.ShowPlatformList (null, content, 100, 100);
  75. }
  76. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 + btnGap, btnTop, btnWidth, btnHeight), "Show Share View"))
  77. {
  78. ShareContent content = new ShareContent();
  79. content.SetText("this is a test string.");
  80. content.SetImageUrl("http://ww3.sinaimg.cn/mw690/be159dedgw1evgxdt9h3fj218g0xctod.jpg");
  81. content.SetTitle("test title");
  82. content.SetTitleUrl("http://www.mob.com");
  83. content.SetSite("Mob-ShareSDK");
  84. content.SetSiteUrl("http://www.mob.com");
  85. content.SetUrl("http://www.mob.com");
  86. content.SetComment("test description");
  87. content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
  88. content.SetShareType(ContentType.Image);
  89. ssdk.ShowShareContentEditor (PlatformType.SinaWeibo, content);
  90. }
  91. btnTop += btnHeight + 20 * scale;
  92. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Share Content"))
  93. {
  94. ShareContent content = new ShareContent();
  95. content.SetText("this is a test string.");
  96. content.SetImageUrl("http://ww3.sinaimg.cn/mw690/be159dedgw1evgxdt9h3fj218g0xctod.jpg");
  97. content.SetTitle("test title");
  98. // content.SetTitleUrl("http://www.mob.com");
  99. // content.SetSite("Mob-ShareSDK");
  100. // content.SetSiteUrl("http://www.mob.com");
  101. content.SetUrl("http://qjsj.youzu.com/jycs/");
  102. // content.SetComment("test description");
  103. // content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
  104. content.SetShareType(ContentType.Webpage);
  105. ssdk.ShareContent (PlatformType.WeChat, content);
  106. }
  107. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 + btnGap, btnTop, btnWidth, btnHeight), "Get Friends SinaWeibo "))
  108. {
  109. //获取新浪微博好友,第一页,每页15条数据
  110. print ("Click Btn Of Get Friends SinaWeibo");
  111. ssdk.GetFriendList (PlatformType.SinaWeibo, 15, 0);
  112. }
  113. btnTop += btnHeight + 20 * scale;
  114. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Get Token SinaWeibo "))
  115. {
  116. Hashtable authInfo = ssdk.GetAuthInfo (PlatformType.SinaWeibo);
  117. print ("share result :");
  118. print (MiniJSON.jsonEncode(authInfo));
  119. }
  120. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 + btnGap , btnTop, btnWidth, btnHeight), "Close SSO Auth"))
  121. {
  122. ssdk.DisableSSO (true);
  123. }
  124. btnTop += btnHeight + 20 * scale;
  125. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 - btnWidth, btnTop, btnWidth, btnHeight), "Remove Authorize "))
  126. {
  127. ssdk.CancelAuthorize (PlatformType.SinaWeibo);
  128. }
  129. if (GUI.Button(new Rect((Screen.width - btnGap) / 2 + btnGap, btnTop, btnWidth, btnHeight), "Add Friend "))
  130. {
  131. //关注新浪微博
  132. ssdk.AddFriend (PlatformType.SinaWeibo, "3189087725");
  133. }
  134. btnTop += btnHeight + 20 * scale;
  135. if (GUI.Button(new Rect((Screen.width - btnWidth) / 2 , btnTop, btnWidth, btnHeight), "ShareWithContentName"))
  136. {
  137. Hashtable customFields = new Hashtable ();
  138. customFields["imgUrl"] = "http://ww1.sinaimg.cn/mw690/006dJESWgw1f6iyb8bzraj31kw0v67a2.jpg";
  139. //根据配置文件分享【本接口功能仅暂时支持iOS】
  140. ssdk.ShareWithContentName(PlatformType.SinaWeibo, "ShareSDK", customFields);
  141. }
  142. btnWidth += 80 * scale;
  143. btnTop += btnHeight + 20 * scale;
  144. if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "ShowShareMenuWithContentName"))
  145. {
  146. Hashtable customFields = new Hashtable ();
  147. customFields["imgUrl"] = "http://ww1.sinaimg.cn/mw690/006dJESWgw1f6iyb8bzraj31kw0v67a2.jpg";
  148. //根据配置文件展示分享菜单分享【本接口功能仅暂时支持iOS】
  149. ssdk.ShowPlatformListWithContentName ("ShareSDK", customFields, null, 100, 100);
  150. }
  151. btnTop += btnHeight + 20 * scale;
  152. if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "ShowShareViewWithContentName"))
  153. {
  154. Hashtable customFields = new Hashtable ();
  155. //根据配置文件展示编辑界面分享【本接口功能仅暂时支持iOS】
  156. customFields["imgUrl"] = "http://ww1.sinaimg.cn/mw690/006dJESWgw1f6iyb8bzraj31kw0v67a2.jpg";
  157. ssdk.ShowShareContentEditorWithContentName(PlatformType.SinaWeibo, "ShareSDK", customFields);
  158. }
  159. }
  160. void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
  161. {
  162. if (state == ResponseState.Success)
  163. {
  164. print ("authorize success !" + "Platform :" + type);
  165. }
  166. else if (state == ResponseState.Fail)
  167. {
  168. #if UNITY_ANDROID
  169. print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
  170. #elif UNITY_IPHONE
  171. print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
  172. #endif
  173. }
  174. else if (state == ResponseState.Cancel)
  175. {
  176. print ("cancel !");
  177. }
  178. }
  179. void OnGetUserInfoResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
  180. {
  181. if (state == ResponseState.Success)
  182. {
  183. print ("get user info result :");
  184. print (MiniJSON.jsonEncode(result));
  185. print ("Get userInfo success !Platform :" + type );
  186. }
  187. else if (state == ResponseState.Fail)
  188. {
  189. #if UNITY_ANDROID
  190. print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
  191. #elif UNITY_IPHONE
  192. print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
  193. #endif
  194. }
  195. else if (state == ResponseState.Cancel)
  196. {
  197. print ("cancel !");
  198. }
  199. }
  200. void OnShareResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
  201. {
  202. if (state == ResponseState.Success)
  203. {
  204. print ("share successfully - share result :");
  205. print (MiniJSON.jsonEncode(result));
  206. }
  207. else if (state == ResponseState.Fail)
  208. {
  209. #if UNITY_ANDROID
  210. print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
  211. #elif UNITY_IPHONE
  212. print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
  213. #endif
  214. }
  215. else if (state == ResponseState.Cancel)
  216. {
  217. print ("cancel !");
  218. }
  219. }
  220. void OnGetFriendsResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
  221. {
  222. if (state == ResponseState.Success)
  223. {
  224. print ("get friend list result :");
  225. print (MiniJSON.jsonEncode(result));
  226. }
  227. else if (state == ResponseState.Fail)
  228. {
  229. #if UNITY_ANDROID
  230. print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
  231. #elif UNITY_IPHONE
  232. print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
  233. #endif
  234. }
  235. else if (state == ResponseState.Cancel)
  236. {
  237. print ("cancel !");
  238. }
  239. }
  240. void OnFollowFriendResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
  241. {
  242. if (state == ResponseState.Success)
  243. {
  244. print ("Follow friend successfully !");
  245. }
  246. else if (state == ResponseState.Fail)
  247. {
  248. #if UNITY_ANDROID
  249. print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
  250. #elif UNITY_IPHONE
  251. print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
  252. #endif
  253. }
  254. else if (state == ResponseState.Cancel)
  255. {
  256. print ("cancel !");
  257. }
  258. }
  259. }