將U3d工程嵌入iOS工程中

我的另外一篇將成型的iOS工程嵌入到u3d工程中

一 、運行環(huán)境

Unity 5.3.4
Xcode 7.3

二、 配置Unity中的iOS選項:

2.png
1.png

Auto Graphic API: 去掉勾,然后選擇OpenGLES2
Scripting Backend: IL2CPP
Target Device:iPhone +ipad
Target iOS Version: 8.0

三、 Unity和iOS的結合

屏幕快照 2016-10-23 下午5.17.01.png

第一個就是我們自己的原生工程,第二個是Unity導出的iOS工程。

五、開始整合文件

1)把U3D中的 Classes、Data、Libraries、MapFileParser.sh 文件先粘貼到iOSNative工程的根目錄下


屏幕快照 2016-10-23 下午5.20.28.png

(2)注意這里幾個文件的添加方式,如果添加方式錯誤了,會導致整合的徹底失敗

Classes、Libraries、MapFileParser.sh 通過Add 添加到項目中,注意(選擇Copy items if need 選擇 Create groups)
Data添加到項目中 (選擇Copy items if needs,選中 Create folder references)

3.png
屏幕快照 2016-10-23 下午5.23.22.png

3)添加FrameWork

下面的這些都需要添加到iOS 原生工程中去。
注意:libiconv.2.dylib 這個的添加方法
Add other 然后全局搜索 command+shift+G 然后輸入/usr/lib 查找就可以了

4.jpeg

六、系統(tǒng)環(huán)境配置

1)添加runScript

5.jpeg

2)添加頭文件引用和庫文件運用

屏幕快照 2016-10-23 下午5.31.59.png

屏幕快照 2016-10-23 下午5.32.12.png

3) Project->MyProject->Build Setting
在Other Linker Flags添加
-lc++
-weak_framework
CoreMotion
-weak-lSystem
-Wl,-undefined,dynamic_lookup

屏幕快照 2016-10-23 下午5.34.52.png

在Other C Flags、Other C++ Flags添加
-DINIT_SCRIPTING_BACKEND=1

9.jpeg

設置 C Language Dialekt:C99[-std=c99]
設置 C++ Language Dialekt : C++ 11[-std=C++11]
設置 C++ Standard Library : libc++ (LLVM C++ standard Library with C++11 support)
設置 Enable Bitcode : NO

屏幕快照 2016-10-23 下午5.38.13.png

4). Project->MyProject->Build Setting
點擊如下圖的“+”號,選擇4個Add User-Defined Setting
設置key值:GCC_THUMB_SUPPORT,設置Value值:NO
設置key值:GCC_USE_INDIRECT_FUNCTION_CALLS,設置Value值:NO
設置key值:UNITY_RUNTIME_VERSION,設置Value值:5.3.4
設置key值:UNITY_SCRIPTING_BACKEND,設置Value值: il2cpp


8.jpeg

5) 選擇Info.plist

添加key值:Unity_LoadingActivityIndicatorStyle,設置Value值:-1

七、文件配置和修改

(1)、在Supporting Files文件夾中創(chuàng)建新的PCH文件,命名為PrefixHeader,如下圖所示勾選上Target,將Classes中的Prefix.pch文件的內容全部拷貝到Supporting Files中的PrefixHeader.pch中。
在Project->MyProject->Build Setting 設置Precompile Prefix Header: YES
并在Prefix Header中添加: $(SRCROOT)/ARHere/PrefixHeader.pch

6.jpeg

(2)、將Classes/main.mm全部內容復制到iOS的main.m 并把iOS中的main擴展名改為.mm, 修改如下代碼

然后要刪除Classes/main.mm文件 ,在iOS的main.mm中做如下修改


10.jpeg

(3)、AppDelegate的修改

#import <UIKit/UIKit.h>
#import "UnityAppController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
@property (strong, nonatomic) UnityAppController *unityController;
- (void)showUnityWindow;
- (void)hideUnityWindow;
@end

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


-(UIWindow *)unityWindow{
    
    return  UnityGetMainWindow();
}

-(void)showUnityWindow{
    
    [self.unityWindow makeKeyAndVisible];
}

-(void)hideUnityWindow{
    
    [self.window makeKeyAndVisible];
}




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _unityController = [[UnityAppController alloc] init];
    [_unityController application:application didFinishLaunchingWithOptions:launchOptions];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = nav;
    
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    
    [_unityController applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    [_unityController applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    [_unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
    [_unityController applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
    [_unityController applicationWillTerminate:application];
}

@end

(4)、UnityAppController中的修改

#pragma once

#import <QuartzCore/CADisplayLink.h>

#include "PluginBase/RenderPluginDelegate.h"

@class UnityView;
@class DisplayConnection;

@interface UnityAppController : NSObject<UIApplicationDelegate>
{
    UnityView*          _unityView;
    CADisplayLink*      _displayLink;

    UIWindow*           _window;
    UIView*             _rootView;
    UIViewController*   _rootController;
    UIView*             _snapshotView;

    DisplayConnection*  _mainDisplay;

    // we will cache view controllers for fixed orientation
    // auto-rotation view contoller goes to index=0
    //  UnityViewControllerBase* _viewControllerForOrientation[5];
    UIViewController* _viewControllerForOrientation[5];      //這里修改了。
#if !UNITY_TVOS
    UIInterfaceOrientation  _curOrientation;
#endif

    id<RenderPluginDelegate>    _renderDelegate;
}

// override it to add your render plugin delegate
- (void)shouldAttachRenderDelegate;

// this one is called at the very end of didFinishLaunchingWithOptions:
// after views have been created but before initing engine itself
// override it to register plugins, tweak UI etc
- (void)preStartUnity;

// this one is called at first applicationDidBecomeActive
// NB: it will be started with delay 0, so it will run on next run loop iteration
// this is done to make sure that activity indicator animation starts before blocking loading
- (void)startUnity:(UIApplication*)application;

// this is a part of UIApplicationDelegate protocol starting with ios5
// setter will be generated empty
@property (retain, nonatomic) UIWindow* window;

@property (readonly, copy, nonatomic) UnityView*            unityView;
@property (readonly, copy, nonatomic) CADisplayLink*        unityDisplayLink;

@property (readonly, copy, nonatomic) UIView*               rootView;
@property (readonly, copy, nonatomic) UIViewController*     rootViewController;
@property (readonly, copy, nonatomic) DisplayConnection*    mainDisplay;

#if !UNITY_TVOS
@property (readonly, nonatomic) UIInterfaceOrientation      interfaceOrientation;
#endif

@property (nonatomic, retain) id                            renderDelegate;
@property (nonatomic, copy)                                 void(^quitHandler)();

@end

// Put this into mm file with your subclass implementation
// pass subclass name to define

#define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
@interface ClassName(OverrideAppDelegate)       \
{                                               \
}                                               \
+(void)load;                                    \
@end                                            \
@implementation ClassName(OverrideAppDelegate)  \
+(void)load                                     \
{                                               \
    extern const char* AppControllerClassName;  \
    AppControllerClassName = #ClassName;        \
}                                               \
@end                                            \

//inline UnityAppController*    GetAppController()
//{
//  return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
// 這里也修改了
#import "AppDelegate.h"
inline UnityAppController*  GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate.unityController;

}


#define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method)                         \
do {                                                                        \
    id<RenderPluginDelegate> delegate = GetAppController().renderDelegate;  \
    if([delegate respondsToSelector:@selector(method)])                     \
        [delegate method];                                                  \
} while(0)

#define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg)                \
do {                                                                        \
    id<RenderPluginDelegate> delegate = GetAppController().renderDelegate;  \
    if([delegate respondsToSelector:@selector(method:)])                    \
        [delegate method:arg];                                              \
} while(0)



// these are simple wrappers about ios api, added for convenience
void AppController_SendNotification(NSString* name);
void AppController_SendNotificationWithArg(NSString* name, id arg);

void AppController_SendUnityViewControllerNotification(NSString* name);

屏幕快照 2016-10-23 下午6.02.57.png

屏幕快照 2016-10-23 下午6.03.17.png

(5)、刪除Main.storyboard
移除Main interface中的默認Main

八、啟動和隱藏U3d界面


#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property(strong, nonatomic) UIButton *Btn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _Btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 200, 80, 70)];
    _Btn.backgroundColor = [UIColor cyanColor];
    [_Btn setTitle:@"開啟" forState:UIControlStateNormal];
    [_Btn addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:_Btn];
}

-(void)go{
    
    //進入unity界面
    [(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
    UnityPause(false);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內容