单机游戏如果没有服务端,那微博就是一个推广和讨论的好地方。

 

首先,可以看一下雨松的教程:

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中传出来。

为了测试,我找了个把截屏的图放到手机相册里的代码。

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);
     }
 }
 

 

 

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); } }]; }

 

-(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];
 }

 

最后结果: