最近有朋友问MOMO如何在Unity3D中使用新浪微博接口,刚好最近我们的工程中也需要添加新浪微博接口,那么MOMO就捎带手学习一下。目前我已经成功将新浪微博接口移植在Unity3D中,希望可以帮助到之前问我的那个朋友,哇咔咔。OK!如下图所示,这是我在U3D程序中发出的微薄消息,图片是在U3D中截屏的,效果还是不错的吧,蛤蛤。
在继续学习之前请朋友确认你所需要的微薄密钥是否准备完毕。
1.没有密钥的朋友
请在这里注册一个移动应用,http://open.weibo.com/ 。注册成功后在应用信息-》基本信息中即可获得APP KEY 和 APP SECRET,没有这两个KEY你是无法发送微薄的。 仅仅这些还是不够,因为是刚刚注册的新密钥所以是不能被公众所使用的,你应当继续在 应用信息-> 测试帐号 中添加测试账号,只有添加过的测试帐号才能使用新注册的密钥发送微薄。
2.有密钥的朋友
可以直接使用你的密钥来进行开发。
接着开始下载IOS新浪微博的开发包 http://code.google.com/p/sinaweibosdkforoauth2/downloads/list 将下载的SinaWeiBoSDK2 -> src -> SinaWeiBoSDK 开发包添加至Unity3D 生成的工程当中。
如下图所示,SinaWeiBoSDK文件夹就是刚刚下载的开发包,另外,记得将Security.framework加入在工程中。最下面的MyViewController 就是负责在U3D之上发送微薄的。
接着大家参照上一篇文章将 MyViewController 加入在U3D之上,请大家看一下我的U3D的工程。
http://www.xuanyusong.com/archives/517
在 OpenEAGL_UnityCallback方法中,在此方法的末尾添加代码:
MyViewController * myView = [[MyViewController alloc] init];
[sGLViewController.view addSubview:myView.view];
Test.cs 挂在摄像机上 首先截屏一张图存在IOS的沙盒中,当用户点击发送消息时开始调用MyViewConroller中的_AuthoLogin方法进行微薄登录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { void Start () { Application.CaptureScreenshot("yusong.JPG"); } void Update () { } void OnGUI() { if(GUILayout.Button("sendSinaMessage",GUILayout.Width(200),GUILayout.Height(100))) { UIAndDateBinding.AuthLogin(); } } } |
UIAndDateBinding.cs 这个类负责进行调用IOS前端方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using UnityEngine; using System.Runtime.InteropServices; public class UIAndDateBinding { [DllImport("__Internal")] private static extern void _AuthLogin (); public static void AuthLogin () { if (Application.platform != RuntimePlatform.OSXEditor) { _AuthLogin(); } } [DllImport("__Internal")] private static extern void _LoginOut (); public static void LoginOut () { if (Application.platform != RuntimePlatform.OSXEditor) { _LoginOut(); } } } |
因为代码比较简单,我就不做过多的说明。然后我们主要来看MyViewController,登录微薄 发送微薄 注销微薄的方式都在这里。
MyViewController.h 没啥东西
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // MyViewController.h // Unity-iPhone // // Created by 雨松MOMO on 12-9-18. // // #import <UIKit/UIKit.h> #import "WBEngine.h" @interface MyViewController : UIViewController<WBEngineDelegate, UIAlertViewDelegate> { } @end |
主要的代码都在这里,请大家仔细看喔。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
// // MyViewController.m // Unity-iPhone // // Created by 雨松MOMO on 12-9-18. // // #import "MyViewController.h" WBEngine* weiBoEngine; @interface MyViewController () @end @implementation MyViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; //初始化微薄客户端 weiBoEngine = [[WBEngine alloc] initWithAppKey:@"填写你的APPKEY" appSecret:@"填写你的APPSECRET"]; [weiBoEngine setRootViewController:self]; [weiBoEngine setDelegate:self]; [weiBoEngine setRedirectURI:@"http://xuanyusong.com"]; [weiBoEngine setIsUserExclusive:NO]; //如果之前登录过让它登出 一般应该让用户手动登出 if([weiBoEngine isLoggedIn]) { [weiBoEngine logOut]; } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)engineDidLogIn:(WBEngine *)engine { //微薄登录成功后将进入这里 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSString *imagePath = [path stringByAppendingPathComponent:@"yusong.JPG"] ; NSData *data=[NSData dataWithContentsOfFile:imagePath]; //从沙盒中将U3D中截取的图像取出 UIImage *img =[UIImage imageWithData:data]; //登录成功 直接发送微薄消息 [weiBoEngine sendWeiBoWithText:@"来自 @雨松MOMO Unity3D For IOS 的分享噢!!" image:img]; } - (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result { //微薄发送成功调用 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"新浪微博", nil) message:NSLocalizedString(@"发送微博成功", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil]; [alertView show]; [alertView release]; } - (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error { //微薄发送失败调用 //记得每次发微薄如果和上次完全一样,是不会发送成功的 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"新浪微博", nil) message:NSLocalizedString(@"发送微博失败", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil]; [alertView show]; [alertView release]; } //注册 Unity就是调用这个方法来进行登录微薄的 void _AuthLogin() { //没有登录的话就登录 if(![weiBoEngine isLoggedIn]) { [weiBoEngine logIn]; } } //登出 void _LoginOut() { [weiBoEngine logOut]; } @end |
发送成功的完整图片。代码我就不上传了,这篇博客也不是很难,仔细看看大家都可以把文字和图片发送在新浪微博中的噢。看了一下表已经不完了,MOMO也得去睡觉了 不然有人会很 很生气 很生气 很生气的,蛤蛤。。
由于我这里的unity版本是3.5 听说在unity4下面会出现无法分享图片的情况, 后来有一个朋友在留言中它解决了这个问题, 我发现还是有朋友会问我这个,所以我还是把他写的文章粘贴过来
单机游戏如果没有服务端,那微博就是一个推广和讨论的好地方。
首先,可以看一下雨松的教程:
http://www.xuanyusong.com/archives/1794
我用的Unity4.0的beta版,发现了不少问题:
一、ViewControl的获得改变了:
从sGLViewController 变化为–》UnityGetGLViewController()
二、游戏内截图所放的位置有变化:
Application.persistentDataPath打印出来的是:
/var/mobile/Applications/27F8B3B1-8E33-4196-8610-40D87D6E7F1A/Documents
从IOS中读取的图片路径是:
/var/mobile/Applications/27F8B3B1-8E33-4196-8610-40D87D6E7F1A/Library/Documentation/XX.png
所以我一直无法发出图片。
解决方式是:把路径从Unity3D中传出来。
为了测试,我找了个把截屏的图放到手机相册里的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class IOSTestSDK : MonoBehaviour { [DllImport("__Internal")] private static extern void _AuthLogin(); [DllImport("__Internal")] private static extern void _ScreenshotWriteToAlbum(string path); public static void ActivateButton() { if (Application.platform != RuntimePlatform.OSXEditor) { //_PressButton(); Debug.Log("Before _AuthLogin"); _AuthLogin(); Debug.Log("After _AuthLogin"); } } public static void ScreenshotWriteToAlbum(string path) { _ScreenshotWriteToAlbum(path); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
NSString* imgPath; void _ScreenshotWriteToAlbum(const char* path) { //imgPath = [NSString stringWithUTF8String:path]; imgPath = [[NSString alloc] initWithCString:path encoding:NSStringEncodingConversionAllowLossy]; sendImg = [UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:path]]; ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease]; NSMutableDictionary* metadata = [[[NSMutableDictionary alloc] init] autorelease]; [library writeImageToSavedPhotosAlbum:sendImg.CGImage metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { if(error) { NSLog(@"Screenshot error -%@", error); } else { NSLog(@"Screenshot taken - %@", error); } }]; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
-(void)engineDidLogIn:(WBEngine *)engine { // login success // NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); // NSString* path = [paths objectAtIndex:0]; // NSString* imagePath = [path stringByAppendingPathComponent:@"kitchen.png"]; // NSData* data = [NSData dataWithContentsOfFile:imagePath]; // UIImage* img = [UIImage imageWithData:data]; // NSLog(@"path: -%@",imgPath); UIImage* img = [UIImage imageWithContentsOfFile:imgPath]; [weiboEngine sendWeiBoWithText:@"测试IOS发微博" image:img]; } |
- 本文固定链接: https://www.xuanyusong.com/archives/1794
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
要是PC端呢?
momo 现在的纯C#实现的可以用到IOS上么
MOMO,unity4的那个路径没问题的,iPhone存放持久性数据一直都是存放在Documents,我测试过了,你的代码依旧能运行,图片也能读到。现在有C#接口了,如果能直接在unity里面调用新浪接口会方便很多
嗯, 现在好像有这方面的插件, 不果我没用过,我都是自己写的 哈哈。。
请问这个是只能通过object c来实现吗 单纯c#是不行的么
纯C# 现在有插件了。。 用插件吧。。
MOMO C# 微博插件的名字和地址是 ?
我想问一个问题,怎么通过Unity3D 添加一个房贷计算机到ios程序?
一种是用n3d来做界面, 一种是在u3d上覆盖一个oc 的view
如果有一个现成的计算机的话,怎么添加呢?这种技术应该怎么称呼?我想找找相关的教程
什么叫现成的计算机? 逻辑部分可以公用 可是UI部分只能是 u3d或者oc
计算机是一个单机的htm文件,如何让它显示到ios上呢?
这个我就不清楚了, 我感觉应该显示不了。。没做过这样的、 不好意思
嗯,谢谢了
不会苹果编程啊,好多地方看不懂。。。
学习苹果开发吧。。
没有取消功能吗?为什么只能分享一次呢?
可以多次分享的啊。。
我在应用里面分享第一次成功,再点就没反应了,需要关闭应用才可以再次分响,调出分享界面,不想分享了,怎么关闭这个界面呢?求指点 我初学
对啊,不想分享的时候怎么关闭这个界面
通过发送消息把这个界面关掉吧!
那应该在哪发送消息呢?
加入测试了可以用了,但是必须用weiboEngine.login(),这样只能用默认的新浪弹窗,我想用自己的弹窗,接受用户名,密码,用weiboEngine.login(username,pwd)这个函数没效果,官方文档说貌似要满足一定的条件,没说什么条件。。怎么办啊。
Undefined symbols for architecture armv7: “__sinaWeiboIsLoggedIn”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiBoPsotWithLocalImage”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiBoAccessToken”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiboInit”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiBoPsot”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiboLogin”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiboLogout”, referenced from: RegisterMonoModules() in RegisterMonoModules.o “__sinaWeiBoUserID”, referenced from: RegisterMonoModules() in RegisterMonoModules.o报错 呀 大神
已经更新了, 应该是没有把myviewcontroller 加到视图之上
加过了 按照你前一篇文章加的
这样吧,晚上我回去看看吧。
这样吧,晚上我回去看看吧。
好的 多谢了
悲剧。。我也是啥都没弹。。直接好像跳过那函数了。。
你是真机上跳过的吗??
对,似乎viewDidLoad这个函数都没进去。。
调试了下,才知道没对把自己的view初始化并加入APPControll.mm。其实就是这一句“接着大家参照上一篇文章将 MyViewController 加入在U3D之上,请大家看一下我的U3D的工程。” 果然不熟悉苹果的编程就SB了。。现在还有一个问题是图片没发出去。。只有文字,得再调试看看。十分感谢!
嗯 那句话我写的太不起眼了 所以大家没注意 呵呵
我用的4.0添加的方法已经不一样了。 WeiBoView* weiboView = [[WeiBoView alloc] init]; [UnityGetGLViewController().view addSubview:weiboView.view]; 现在按您的demo是发不了游戏截图,我看了一下应该是路径有问题, Application.persistentDataPath打印出来的是: Path:/var/mobile/Applications/27F8B3B1-8E33-4196-8610-40D87D6E7F1A/Documents 从IOS中读取的图片路径是: /var/mobile/Applications/27F8B3B1-8E33-4196-8610-40D87D6E7F1A/Library/Documentation/XX.png 所以图片一直是空的。。
噢。。 原来是这样 可能是新升级的原因了吧??
你说的4.0指的是 新浪最新的包吗??
不错, 有问题的朋友看看他的那篇文章吧。 估计是4.0的问题 我当时写的是3.5的
不应该啊。。
momo,用你的例子写了,问题是点击按钮之后,程学就死掉了。打印之后发现程序转入ios前台后_AuthLogin函数并没有执行。不知道具体的原因。。求解答
是不是那里有点不对?
具体的不清楚 Security.framework添加了,MyViewController.h和.m也添加了,代码直接用你的,不知道问题出在哪里
应该是hi MyViewController 没有添加在视图之上
添加了。。Unable to resolve pinvoke method ‘sinaData:_AuthLogin ()’ Re-run with MONO_LOG_LEVEL=debug for more information. Xcode报得这个错误。。。我得unity版本是3.5.6f4 貌似是mono得dll那边出问题连不上xcode得函数…
和你一样。同样的问题。希望能解释啊。。Unity3D里面出不来登录页面啊。。
你好,今天用了您的例子调用微博,但是显示不出登陆界面。并且确定已经进入void _AuthLogin()这个函数,做了测试可以在这个函数里面弹出UIAlertView,但是没有登录界面显示。希望能解答一下
“接着大家参照上一篇文章将 MyViewController 加入在U3D之上,请大家看一下我的U3D的工程。” 这个是在哪里?上一篇文章在哪?
少一个授权页面的弹出界面啊
有的。
momo你好,我按照上面的方法试了,ipad测试返回invalid client错误。请问怎么回事啊,已经有appkey了啊。
加入测试帐号了吗?
感谢分享哈
客气啦 蛤蛤
期待出一个安卓版本的
今天晚上就出了, 代码已经写的差不多了,晚上回家更新博客。。
我也申请了一个开发者应用了 不过感觉如果要真上线的话好多东西不会填啊 呵呵
最后打包的时候出了点问题 今天是写不完了 明天在看
看来小夭是专职安卓啊。。。
” 不然有人会很 很生气 很生气 很生气的蛤蛤。。” 这结尾……
哈哈哈。。