iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1

iOS原生集成Unity相互跳转流程

PS:使用2018.3.5f1版本进行生产Unity工程,Xcode版本10.1

Unity工程生成设置

  1. 打开Unity创建一个工程,然后点击上方的File->BuildSetting,在弹出的设置框中,选择iOS然后点击player Setting进行设置
  2. 设置如图(需要注意这边的Bundle Identifier与原生工程应该保持一致):
    iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1_第1张图片
    Unity Setting
  3. 设置完毕之后选择Build进行导出,导出名称和位置放在自己方便拷贝和运行的地方
  4. 使用Xcode运行Unity工程,确保导出的工程没有问题

原生工程集成Unity

PS:(如有用删除操作全部使用Remove Reference)

  1. 首先新建一个工程项目
  2. 设置新项目的Bundle Identifier,跟之前的Unity工程一样
  3. 在我们从Unity里面导出来ios工程中找到Libraries、Classes、MapFileParse.sh和Data拖进工程,此时要注意 Libraries、MapFileParse.sh和Classes过程中Copy items if needed -->不选 Create groups--> 选 ; 在Data时 Copy items if needed --> 不选 ,Create folder references -->选。(PS:比较旧版的Unity可能在Classes中生成很多".h"文件,需要进行删除操作,但是最新的Unity版本没有生成这些文件,没有比较进行操作)
  4. 如果原生工程没有.pch文件,可以直接在Build Settings -> Precomplies Prxfix Header --> YES, 并且Prefix Header直接指向Classes的Prefix.pch文件;如果原来工程存在.pch文件,则复制Prefix.pch的内容到原工程的.pch中,删除Prefix.pch。在关联的.pch文件中导入Unity控制器文件
    iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1_第2张图片
    导入文件
  5. Enable Bitcode --> YES, Enable Testability --> NO
  6. Other Linker Flags设置4个属性 $(inherited) -weak_framework CoreMotion -weak-ISystem
  7. Header Search Paths和Library Search Paths:
    iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1_第3张图片
    头文件

  8. Other C Flags设置为:$(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DNET_4_0 -DRUNTIME_IL2CPP=1
  9. C Language Dialect --> GNU99[-std=gnu99] (最新版本的才设置为这个值)
  10. C++ Language Dialect --> C++11[-std=c++11]
  11. Enable C++ Runtime Types --> NO
  12. Overriding Deprecated Objective-C Methods --> YES
  13. Unintentional Root Class --> YES
  14. 添加User-defined设置,需要注意版本设置需要用你导出Unity工程的版本,我的为最新2018.3.5f1版本:
    iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1_第4张图片
    User-defined
  15. 导入使用到的库文件:
    iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1_第5张图片
    库文件
  16. 修改Main.mMain.mm,将Classes里面的Main.mm文件内容复制到你修改的文件里面,并且删除Classes的Main.mm
    到这个步骤基本的工程导入已经完成,接下来要增加和修改的就是跳转的代码

原生和Unity互相跳转

  1. 修改AppDelegate.h文件:
@interface AppDelegate : UIResponder 
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong) UnityAppController *unityController;
@property (nonatomic,strong) UIWindow *unityWindow;
//在ViewController实现也可
-(void)showUnityWindow;
-(void)hideUnityWindow;
@end

新增Unity的控制器和控制窗口

  1. 修改AppDelegate.m文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ViewController *viewController = [[ViewController alloc] init];
    viewController.view.backgroundColor = [UIColor whiteColor];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = viewController;

    self.unityController = [[UnityAppController alloc] init];
    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];

    [self.window makeKeyAndVisible];

    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    [self.unityController applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.unityController applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self.unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self.unityController applicationDidBecomeActive:application];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    [self.unityController applicationWillTerminate:application];
}

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

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

-(void)hideUnityWindow {
    [self.window makeKeyAndVisible];
}
  1. 修改Main.mm文件:在文件中找到const char AppControllerClassName = "UnityAppController"* 和 UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]) 修改为
//const char* AppControllerClassName = "UnityAppController";
const char* AppControllerClassName = "AppDelegate";

//UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  1. 修改UnityAppController.h
#import "AppDelegate.h"

extern UnityAppController* _UnityAppController;
inline UnityAppController* GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate.unityController;
//    return _UnityAppController;
}
  1. 修改ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.showUnityButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.showUnityButton.frame = CGRectMake(0, 0, 100, 50);
    self.showUnityButton.tintColor = [UIColor blueColor];
    [self.showUnityButton setTitle:@"Go To Unity" forState:UIControlStateNormal];
    self.showUnityButton.center = self.view.center;
    [self.showUnityButton addTarget:self action:@selector(showUnityView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.showUnityButton];

}

-(void)showUnityView:(id)sender {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 44, 44);
    button.backgroundColor = [UIColor redColor];
    button.center = delegate.unityWindow.center;
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
    [delegate.unityWindow addSubview:button];

    [delegate.unityWindow makeKeyAndVisible];
}

-(void)hideUnityWindow {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window makeKeyAndVisible];
}

你可能感兴趣的:(iOS原生集成Unity并实现互相跳转,Unity最新版2018.3.5f1)