Unity-Unity工程嵌入到Xcode原生工程中的相关步骤

2016-12-17/Unity5.5.0f3/Xcode_8.1(8B62)/MacBook/OS X 10.11.6
参考地址:
https://the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/
http://www.jianshu.com/p/715adc38e451

  • 最近接到不少单子的需求都是要求把AR模块嵌入原有APP,而不开发新的APP,那不管是高通的Vuforia还是视+的EasyAR,若要实现模型动画,要么自己用OpenGLES写渲染,要么直接用Unity,显然,用OpenGLES从头写渲染的工程量很大,而嵌入Unity的工作量则小得多,且渲染效果也要更好,而OpenGLES方法的优势可能也就是比较稳定这一点了。这篇博客主要是依照老外的视频做的,就是把视频内容文字化而已,和简书上那位大大写得差不多。推荐直接看视频,手把手教。

那么,正式开始:

  1. Unity导出iOS工程,为方便,称导出的iOS工程为iUnity工程。导出时PlayerSetting设置要注意的部分主要是Scripting Backend为IL2CPP,至于Graphics API基本不用理会,如为保险起见,可按以前的教程取消勾选,删除Metal只保留OpenGL ES2,但我此次并未作此修改。

Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第1张图片

  1. 此时,准备工作结束,到XCode中新建一个Single View(不用引用UI和Unit包) 或者打开已有工程,为方便,称原生工程为iNative工程。

  2. 将iUnity工程中的Classes和Libraries引入到iNative工程中,选择Create Groups。
    3.1将iUnity工程中的Data引入到iNative中,选择Create folder reference。
    Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第2张图片

  3. 为了安全和编译速度,可以删除iNative中Classes/Native下的所有.h文件。
    4.1同样,也可以删除iNative中Libraries/libil2cpp文件夹。

  4. 在iNative中添加pct文件,此次命名为PrefixHeader.pch。
    5.1将Classes中Prefix.pch文件的内容复制到Prefixheader.pch中(不是完全覆盖)。
    5.2在PrefixHeader.pch中添加“#import “UnityAppController.h” ”。
    Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第3张图片

  5. Classes中的main.mm中的内容全部拷贝到Supporting Files中的main.m中(全部替换,如是已有工程,手动修改)。
    6.1修改main.m为main.mm(.mm后缀才能编译c,oc,c++),到build phase的compile sources中移除Classes路径的main.mm编译。

  6. 引用Framework(注意Optional的项)
    Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第4张图片

  7. 配置Build Setting,若教程失效时可以打开自己导出的iUnity工程,这一步不过是要将iNative的Build Setting设置为iUnity的Build Setting。
    还可参考简书大大的那篇配图。
    8.1 Header Search Paths
    8.2 Library Search Paths
    8.3 C++ Language Dialect :C++11,Enable C++ Runtime Types No
    8.4 Precompile Prefix Header :Yes ,Prefix Header :iNative/PrefixHeader.pch(根据自己的路径调节)。
    8.5 C Language Dialect : C99
    8.6 Other C Flags : -DINIT_SCRIPTING_BACKEND=1
    8.7 User-Defined:
    GCC_THUMB_SUPPORT : NO
    GCC_USE_INDIRECT_FUNCTION_CALLS : NO
    UNITY_RUNTIME_VERSION : 5.5.0f3
    UNITY_SCRIPTING_BACKEND : il2cpp
    8.8 Other Linker Flags: -weak_framework CoreMotion -weak-lSystem

  8. 代码修改部分:
    9.1UnityAppController.h(位于Classes中)
    Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第5张图片

9.2Appdelegate.h
Unity-Unity工程嵌入到Xcode原生工程中的相关步骤_第6张图片

9.3Appdelegate.m

//
//  AppDelegate.m
//  iOSNativeUnity
//
//  Created by Develop on 2016/12/16.
//  Copyright © 2016年 Develop. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic ,strong) UINavigationController *navVC;

@end

@implementation AppDelegate

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

//Add by PGW
-(void) showUnityWindow
{
    [self.unityWindow makeKeyAndVisible];
}

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


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];

    ViewController *viewcontroller = [[ViewController alloc] initWithNibName:nil bundle:nil];

    self.navVC = [[UINavigationController alloc] initWithRootViewController:viewcontroller];

    self.window.rootViewController = self.navVC;

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

    [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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
    [self.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.

    [self.unityController applicationDidEnterBackground:application];
}


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

    [self.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.
    [self.unityController applicationDidBecomeActive:application];

}


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

    [self.unityController applicationWillTerminate:application];
}


@end

9.4ViewController.m

//
//  ViewController.m
//  iOSNativeUnity
//
//  Created by Develop on 2016/12/16.
//  Copyright © 2016年 Develop. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *showUnityButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor blueColor];

    self.showUnityButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.showUnityButton setTitle:@"Show Unity" forState:UIControlStateNormal];
    self.showUnityButton.frame = CGRectMake(0, 0, 100, 44);
    self.showUnityButton.center = self.view.center;
    [self.view addSubview:self.showUnityButton];

    [self.showUnityButton addTarget:self action:@selector(showunitybuttonTouched:) forControlEvents:UIControlEventTouchUpInside];



}

-(void)showunitybuttonTouched:(UIButton *)sender
{
    NSLog(@"[Viewcontroller Button Touch]");

    [(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
}

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


@end
  1. 最后:
    10.1一定要用真机调试,没有真机的话不管是build还是怎么样,一定会报类似“referenced from”的错误。
    10.2 懒人示例:链接: http://pan.baidu.com/s/1gfoidJP 密码: smjp

你可能感兴趣的:(Unity)