using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Reflection; namespace cn.sharesdk.unity3d { /// /// ShareSDK. /// public class ShareSDK : MonoBehaviour { public static string MobAppKey = "1b17e87d222c1"; public static string WeChatAppKey = "wx47450ab76d65a1b3"; public static string WeChatAppSecrete = "a3528b865b9c4db3f76400983b2bd72f"; public static string QQAppKey = "1106316278"; public static string QQAppSecrete = "MwrdToADs0yzeqxD"; public static string QQZoneAppKey = "1106316278"; public static string QQZoneAppSecreteForIOS = "MwrdToADs0yzeqxD"; public static string QQZoneAppSecreteForAndroid = "MwrdToADs0yzeqxD"; public static string SinaAppKey = "568898243"; public static string SinaAppSecrete = "38a4f8204cc784f81f9f0daaf31e02e3"; public static string FacebookAppKey = "138557386743472"; public static string FacebookAppSecrete = "ad4f8d51485a5af6a1a48d011d08bb89"; private int reqID; public DevInfoSet devInfo; public ShareSDKImpl shareSDKUtils; public EventHandler authHandler; public EventHandler shareHandler; public EventHandler showUserHandler; public EventHandler getFriendsHandler; public EventHandler followFriendHandler; void Awake() { } public void Initialize() { devInfo = new DevInfoSet(); Type type = devInfo.GetType(); Hashtable platformConfigs = new Hashtable(); FieldInfo[] devInfoFields = type.GetFields(); foreach (FieldInfo devInfoField in devInfoFields) { DevInfo info = (DevInfo)devInfoField.GetValue(devInfo); int platformId = (int)info.GetType().GetField("type").GetValue(info); FieldInfo[] fields = info.GetType().GetFields(); Hashtable table = new Hashtable(); foreach (FieldInfo field in fields) { if ("type".EndsWith(field.Name)) { continue; } else if ("Enable".EndsWith(field.Name) || "ShareByAppClient".EndsWith(field.Name) || "BypassApproval".EndsWith(field.Name)) { table.Add(field.Name, Convert.ToString(field.GetValue(info)).ToLower()); } else { table.Add(field.Name, Convert.ToString(field.GetValue(info))); } } platformConfigs.Add(platformId, table); } #if UNITY_ANDROID shareSDKUtils = new AndroidImpl(gameObject); #elif UNITY_IPHONE shareSDKUtils = new iOSImpl(gameObject); #endif shareSDKUtils.InitSDK(MobAppKey); shareSDKUtils.SetPlatformConfig(platformConfigs); } /// /// callback the specified data. /// /// /// Data. /// private void _Callback (string data) { if (data == null) { return; } Hashtable res = (Hashtable) ShareSDKMiniJSON.jsonDecode(data); if (res == null || res.Count <= 0) { return; } int status = Convert.ToInt32(res["status"]); int reqID = Convert.ToInt32(res["reqID"]); PlatformType platform = (PlatformType)Convert.ToInt32(res["platform"]); int action = Convert.ToInt32(res["action"]); // Success = 1, Fail = 2, Cancel = 3 switch(status) { case 1: { Console.WriteLine(data); Hashtable resp = (Hashtable) res["res"]; OnComplete(reqID, platform, action, resp); break; } case 2: { Console.WriteLine(data); Hashtable throwable = (Hashtable) res["res"]; OnError(reqID, platform, action, throwable); break; } case 3: { OnCancel(reqID, platform, action); break; } } } /// /// Raises the error event. /// /// Platform. /// Action. /// Throwable. public void OnError (int reqID, PlatformType platform, int action, Hashtable throwable) { switch (action) { case 1: { // 1 == Platform.ACTION_AUTHORIZING if (authHandler != null) { authHandler(reqID, ResponseState.Fail, platform, throwable); } break; } case 2: { //2 == Platform.ACTION_GETTING_FRIEND_LIST if (getFriendsHandler != null) { getFriendsHandler(reqID, ResponseState.Fail, platform, throwable); } break; } case 6: { //6 == Platform.ACTION_FOLLOWING_USER if (followFriendHandler != null) { followFriendHandler(reqID, ResponseState.Fail, platform, throwable); } break; } case 8: { // 8 == Platform.ACTION_USER_INFOR if (showUserHandler != null) { showUserHandler(reqID, ResponseState.Fail, platform, throwable); } break; } case 9: { // 9 == Platform.ACTION_SHARE if (shareHandler != null) { shareHandler(reqID, ResponseState.Fail, platform, throwable); } break; } } } /// /// Raises the success event. /// /// Platform. /// Action. /// Res. public void OnComplete (int reqID, PlatformType platform, int action, Hashtable res) { switch (action) { case 1: { // 1 == Platform.ACTION_AUTHORIZING if (authHandler != null) { authHandler(reqID, ResponseState.Success, platform, null); } break; } case 2: { //2 == Platform.ACTION_GETTING_FRIEND_LIST if (getFriendsHandler != null) { getFriendsHandler(reqID, ResponseState.Success, platform, res); } break; } case 6: { //6 == Platform.ACTION_FOLLOWING_USER if (followFriendHandler != null) { followFriendHandler(reqID, ResponseState.Success, platform, res); } break; } case 8: { // 8 == Platform.ACTION_USER_INFOR if (showUserHandler != null) { showUserHandler(reqID, ResponseState.Success, platform, res); } break; } case 9: { // 9 == Platform.ACTION_SHARE if (shareHandler != null) { shareHandler(reqID, ResponseState.Success, platform, res); } break; } } } /// /// Raises the cancel event. /// /// Platform. /// Action. public void OnCancel (int reqID, PlatformType platform, int action) { switch (action) { case 1: { // 1 == Platform.ACTION_AUTHORIZING if (authHandler != null) { authHandler(reqID, ResponseState.Cancel, platform, null); } break; } case 2: { //2 == Platform.ACTION_GETTING_FRIEND_LIST if (getFriendsHandler != null) { getFriendsHandler(reqID, ResponseState.Cancel, platform, null); } break; } case 6: { //6 == Platform.ACTION_FOLLOWING_USER if (followFriendHandler != null) { followFriendHandler(reqID, ResponseState.Cancel, platform, null); } break; } case 8: { // 8 == Platform.ACTION_USER_INFOR if (showUserHandler != null) { showUserHandler(reqID, ResponseState.Cancel, platform, null); } break; } case 9: { // 9 == Platform.ACTION_SHARE if (shareHandler != null) { shareHandler(reqID, ResponseState.Cancel, platform, null); } break; } } } /// /// init the ShareSDK. /// public void InitSDK (String appKey) { // if you don't add ShareSDK.xml in your assets folder, use the following line shareSDKUtils.InitSDK (appKey); } /// /// Sets the platform config. /// public void SetPlatformConfig (Hashtable configInfo) { shareSDKUtils.SetPlatformConfig (configInfo); } /// /// Authorize the specified type, observer and resultHandler. /// /// /// Type. /// /// /// Observer. /// /// /// Result handler. /// public int Authorize (PlatformType platform) { reqID ++; shareSDKUtils.Authorize(reqID, platform); return reqID; } /// /// Cancel authorized /// /// /// Type. /// public void CancelAuthorize (PlatformType platform) { shareSDKUtils.CancelAuthorize(platform); } /// /// Has authorized /// /// /// true has authorized, otherwise not authorized. /// /// /// Type. /// public bool IsAuthorized (PlatformType platform) { return shareSDKUtils.IsAuthorized(platform); } public bool IsClientValid (PlatformType platform) { return shareSDKUtils.IsClientValid(platform); } /// /// Gets the user info. /// /// /// Type. /// /// /// Callback. /// public int GetUserInfo (PlatformType platform) { reqID ++; shareSDKUtils.GetUserInfo(reqID, platform); return reqID; } /// /// Shares the content. /// /// /// Type. /// /// /// Content. /// /// /// Callback. /// public int ShareContent(PlatformType platform, ShareContent content) { reqID ++; shareSDKUtils.ShareContent(reqID, platform, content); return reqID; } /// /// Shares the content. /// /// /// Type. /// /// /// Content. /// /// /// Callback. /// public int ShareContent(PlatformType[] platforms, ShareContent content) { reqID ++; shareSDKUtils.ShareContent(reqID, platforms, content); return reqID; } /// /// Shows the share menu of using onekeyshare. /// /// /// Types. /// /// /// Content. /// /// /// Callback. /// public int ShowPlatformList (PlatformType[] platforms, ShareContent content, int x, int y) { reqID ++; shareSDKUtils.ShowPlatformList(reqID, platforms, content, x, y); return reqID; } /// /// Shows the share view of using onekeyshare. /// /// /// Type. /// /// /// Content. /// /// /// Callback. /// public int ShowShareContentEditor (PlatformType platform, ShareContent content) { reqID ++; shareSDKUtils.ShowShareContentEditor(reqID, platform, content); return reqID; } /// /// share according to the name of node in ShareContent.xml(you can find it in Xcode) [only valid in iOS temporarily][此接口暂时仅支持iOS环境] /// /// /// Platform Type /// /// /// the name of node in ShareContent.xml file /// /// /// your share customFields which will be replace in ShareContent.xml /// public int ShareWithContentName (PlatformType platform, string contentName, Hashtable customFields) { reqID++; shareSDKUtils.ShareWithContentName (reqID, platform, contentName, customFields); return reqID; } /// /// share according to the name of node in ShareContent.xml(you can find it in Xcode) (only valid in iOS temporarily)(此接口暂时仅支持iOS环境) /// /// /// /// the name of node in ShareContent.xml file /// /// /// your share customFields which will be replace in ShareContent.xml /// /// /// Platform Types /// /// /// the coordinates of the share menu /// public int ShowPlatformListWithContentName (string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y) { reqID++; shareSDKUtils.ShowPlatformListWithContentName (reqID, contentName, customFields, platforms, x, y); return reqID; } /// /// share according to the name of node in ShareContent.xml file (only valid in iOS temporarily)(此接口暂时仅支持iOS环境) /// /// /// Platform Type /// /// /// the name of node in ShareContent.xml file /// /// /// your share customFields which will be replace in ShareContent.xml /// public int ShowShareContentEditorWithContentName (PlatformType platform, string contentName, Hashtable customFields) { reqID++; shareSDKUtils.ShowShareContentEditorWithContentName (reqID, platform, contentName, customFields); return reqID; } /// /// Gets the friends. /// /// Type. /// Count. /// Page. public int GetFriendList (PlatformType platform, int count, int page) { reqID ++; shareSDKUtils.GetFriendList (reqID, platform, count, page); return reqID; } /// /// Follows the friend. /// /// Type. /// Account. public int AddFriend (PlatformType platform, String account) { reqID ++; shareSDKUtils.AddFriend (reqID, platform, account); return reqID; } /// /// Gets the auth info. /// /// Type. public Hashtable GetAuthInfo (PlatformType platform) { return shareSDKUtils.GetAuthInfo (platform); } /// /// Close the SSO when authorize. /// /// If set to true open. public void DisableSSO(Boolean open) { shareSDKUtils.DisableSSO (open); } /// /// Event result listener. /// public delegate void EventHandler (int reqID, ResponseState state, PlatformType type, Hashtable data); } }