将U3d工程嵌入iOS工程中

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

一 、运行环境

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

六、系统环境配置

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文件夹中创建新的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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容