unity3D 搞定任意ios外掛程式

來源:互聯網
上載者:User

標籤:blog   class   code   java   tar   c   

原地址:http://www.cnblogs.com/U-tansuo/archive/2012/11/22/unity_ios-plugin.html

說起unity調ios外掛程式,好多淫比較頭痛,探索再此給大家獻上通用手冊。

首先:需要需要一些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;@endMangGuoController.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.