首先:需要需要一些OC知識,在OC中所有的界面都是基於View顯示的,而要使用第三方提供的ios sdk 僅需簡單修改源代碼,把相應view添加到unity build出來的xcode工程的根view上即可。也許有人在罵,這不廢話,跟沒說一樣,我坦然接受,因為確實沒有干活。
下面給大家實例分析一下:首先要了解Unity發出來xcode工程的結構。
注意紅框和綠框,其中紅框在此沒用,可以忽略。焦點放在綠框上,UIApplicationMain(argc, argv, nil, @"AppController");是程序的入口,一般的xcode工程都是UIApplicationMain(argc, argv, nil, @"XXXAppDelegete");表示要進入XXXAppDelegete里面的某個方法。本文中會進入到AppController.m中的(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法中。為什么會進入此方法,OC規定的(可以自己看看相關文檔,我是加斷點看出來的)。
2、(BOOL)application:(UIApplication*)application中會調用 [self startUnity:application];進入startUnity方法,這個方法完了后最終會到 OpenEAGL_UnityCallback,這是我們的焦點。以下是此函數中核心代碼:
_window = [[UIWindow alloc] initWithFrame:rect];//創建主窗口
EAGLView* view = [[EAGLView alloc] initWithFrame:rect];//創建根view
#ifdef __IPHONE_6_0
UnityViewController *controller = _ios60orNewer ? [[UnityViewController_IOS6 alloc] init] : [[UnityViewController_preIOS6 alloc] init];
#else
UnityViewController *controller = [[UnityViewController alloc] init];
#endif
[_window addSubview:view];//根view 添加到主窗口
if( [_window respondsToSelector:@selector(rootViewController)] )
{
_window.rootViewController = controller;
}
因此其他任何第三方sdk,幾乎都是要將他的view,添加到根view上,當上面函數被執行完后,根view,主window就存在了 現在大家可以為所欲為了。
附帶簡單demo,希望對大家理解有幫助。
芒果sdK調用 MangGuoController.h #import <UIKit/UIKit.h> #import "AdMoGoDelegateProtocol.h" #import "AdMoGoView.h" #import "AdMoGoWebBrowserControllerUserDelegate.h" @interface MangGuoController:NSObject<AdMoGoDelegate,AdMoGoWebBrowserControllerUserDelegate>{ AdMoGoView *largeAd; } @property(nonatomic,retain) UIView *view; @end MangGuoController.m #import "MangGuoController.h" #import <QuartzCore/QuartzCore.h> @implementation MangGuoController @synthesize view; -(id)init{ self=[super init]; if(self) { UIWindow *window= [[UIApplication sharedApplication] keyWindow];//獲取主窗口 self.view=[window.subviews objectAtIndex:0];獲取根view
largeAd = [[AdMoGoView alloc] initWithAppKey:@"芒果ID" adType:AdViewTypeNormalBanner expressMode:NO adMoGoViewDelegate:self]; largeAd.adWebBrowswerDelegate = self; largeAd.frame=CGRectZero; [ self.view addSubview:largeAd];//將芒果view 添加到根view上。 [largeAd release]; } return self; }
通過MangGuoController* mg=[[MangGuoController alloc]init];[mg release]; 即可調用廣告。特別說明 前面的實例化 最好這樣寫:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
printf_console("-> applicationDidFinishLaunching()\n");
[self startUnity:application];//執行完后 才會實例化出 根view 和主window
MangGuoController* mg=[[MangGuoController alloc]init];//確保存在了根view和主window
[mg release];
return NO;
}
我也是半路出家的OC初學者,詞里行間不妥之處,還往大家多多指教。
UIApplication知識點:http://johnlv.blog.sohu.com/185994960.html