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
那么,正式开始:
此时,准备工作结束,到XCode中新建一个Single View(不用引用UI和Unit包) 或者打开已有工程,为方便,称原生工程为iNative工程。
将iUnity工程中的Classes和Libraries引入到iNative工程中,选择Create Groups。
3.1将iUnity工程中的Data引入到iNative中,选择Create folder reference。
为了安全和编译速度,可以删除iNative中Classes/Native下的所有.h文件。
4.1同样,也可以删除iNative中Libraries/libil2cpp文件夹。
在iNative中添加pct文件,此次命名为PrefixHeader.pch。
5.1将Classes中Prefix.pch文件的内容复制到Prefixheader.pch中(不是完全覆盖)。
5.2在PrefixHeader.pch中添加“#import “UnityAppController.h” ”。
Classes中的main.mm中的内容全部拷贝到Supporting Files中的main.m中(全部替换,如是已有工程,手动修改)。
6.1修改main.m为main.mm(.mm后缀才能编译c,oc,c++),到build phase的compile sources中移除Classes路径的main.mm编译。
配置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
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