Unity-Unity工程嵌入到Xcode原生工程中的相关步骤
来源:互联网 发布:怎么在淘宝买高仿手表 编辑:程序博客网 时间:2024/06/08 14:54
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方法的优势可能也就是比较稳定这一点了。这篇博客主要是依照老外的视频做的,就是把视频内容文字化而已,和简书上那位大大写得差不多。推荐直接看视频,手把手教。
那么,正式开始:
- Unity导出iOS工程,为方便,称导出的iOS工程为iUnity工程。导出时PlayerSetting设置要注意的部分主要是Scripting Backend为IL2CPP,至于Graphics API基本不用理会,如为保险起见,可按以前的教程取消勾选,删除Metal只保留OpenGL ES2,但我此次并未作此修改。
此时,准备工作结束,到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编译。引用Framework(注意Optional的项)
配置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.1UnityAppController.h(位于Classes中)
9.2Appdelegate.h
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
- 最后:
10.1一定要用真机调试,没有真机的话不管是build还是怎么样,一定会报类似“referenced from”的错误。
10.2 懒人示例:链接: http://pan.baidu.com/s/1gfoidJP 密码: smjp
- Unity-Unity工程嵌入到Xcode原生工程中的相关步骤
- iOS Xcode原生项目集成Unity导出的工程
- Xcode原生项目集成Unity导出的工程
- Unity 导出的Xcode工程
- Unity(2017版本)嵌入现有iOS工程
- Unity导出工程到AndroidStudio
- Unity打包xcode工程编译错误整理
- Unity生成的xcode工程问题
- Xcode中一个工程嵌入到另一个工程
- [Unity&photon]photon导入Unity 建立工程 详细步骤教程
- unity工程导入到iOS项目
- AR移动应用开发(二) Unity工程嵌入Android
- unity导出ios工程导入到另一个工程中
- iOS - 将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法
- Unity打包xcode工程编译错误及解决方法
- Unity-导出Xcode工程时的自动配置
- unity打包iOS工程包含蓝牙功能,xcode添加事项
- Unity导出Xcode工程 在模拟器上调试
- 凸包 【uva11168】 Airport
- C++中一个函数作为作为另一个函数的参数
- js实现复制到剪贴板功能,兼容所有浏览器-下载下来demo一看就明白了
- su切换用户
- 【Android 应用开发】Android中使用ViewPager制作广告栏效果 - 解决ViewPager占满全屏页面适配问题
- Unity-Unity工程嵌入到Xcode原生工程中的相关步骤
- Shell—参数的传递
- Android 触摸模式(Touch Mode)
- Android练手小项目---仿凤凰新闻app
- IOS界面开发之UILabel
- 2.Linux基本概念和操作
- android studio2.2.2安装SQLScout插件位置
- android studio及SDK最新版本下载
- C语言 - 数字和字母统计之函数实现