第1步 unity3D导出Xcode项目
第2步 将项目导入到ios原生项目
1.将Classes,Libraries,MapFileParser.sh拖入到项目(选中Copy items if needed,选中Create groups)
2.将Data拖入到项目(选中Copy items if needed,选中Create folder references)
*拖入方式
如果选择了Create groups方式添加了一个文件,我们会发现被添加进来的文件,文件夹是黄色的,groups下的cpp文件是会被编译的
选择了 Create folder references方式添加进来的文件的文件夹是蓝色的, 只是将文件单纯的创建了引用,这些文件不会被编译,所以在使用的时候需要加入其路径
3.修改ios原生项目的bit code为NO
4.添加framework
*由于每个Unity 3D项目不同,导致所需要framework也不同,打开Unity 3DXcode项目,把所有的framework在原生项目中添加,如果有第三方的库,把库从Unity 3DXcode项目中导出到原生项目,并且在Framework Search Paths添加路径
*Unity 3DXcode项目中有个别库的Status 选择的Optional,如图
5.在Header Search Paths和Library Search Paths 添加Unity3D 文件夹 Classes、Native、include 路径(根据自己项目实际文件路径配置)
*$(SRCROOT) 代表的是项目根目录下 $(PROJECT_DIR)代表的是整个项目
6.修改Build Settings 下 other C Flags -> -DINIT_SCRIPTING_BACKEND=1
7.添加User-Defined 将Unity 3D项目中的User-Defined里的配置项添加到原生项目中
8.添加Run Script
*MapFileParser.sh 路径根据实际项目路径添加
第3步 修改原生代码
1.将Classes/main.mm内容复制到main.m 并把扩展名改为.mm,删除Unity3D Class 文件夹下main.mm 文件,删除操作选择Remove Reference
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#include "RegisterFeatures.h"
#include "RegisterMonoModules.h"
#include <csignal>
static const int constsection = 0;
void UnityInitTrampoline();
const char* AppControllerClassName = "AppDelegate";
int main(int argc, char * argv[]) {
@autoreleasepool {
UnityInitTrampoline();
UnityInitRuntime(argc, argv);
RegisterMonoModules();
NSLog(@"-> registered mono modules %p\n", &constsection);
RegisterFeatures();
std::signal(SIGPIPE, SIG_IGN);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
#include <pthread.h>
extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
{ return pthread_cond_init(cond, attr); }
extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
{ return pthread_cond_destroy(cond); }
extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ return pthread_cond_wait(cond, mutex); }
extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime)
{ return pthread_cond_timedwait(cond, mutex, abstime); }
#endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
2 修改AppDelegate.h
#import <UIKit/UIKit.h>
@class UnityAppController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UnityAppController * unityController;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
-(void)showUnityWindow;
-(void)hideUnityWindow;
@end
3 修改AppDelegate.m 添加如下代码
*核心就是切换window
#import "UnityAppController.h"
@implementation AppDelegate
-(UIWindow *)unityWindow{
return UnityGetMainWindow();
}
-(void)showUnityWindow{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 44, 44);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
[self.unityWindow addSubview:button];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.unityWindow makeKeyAndVisible];
}
-(void)hideUnityWindow{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[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 whiteColor];
ViewController * vc = [[ViewController alloc]init];
self.window.rootViewController = vc;
self.unityController = [[UnityAppController alloc]init];
[self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
[self.window makeKeyAndVisible];
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];
}
@end
4 合并 .pch 文件,如果之前没有创建一个,删除Unity/Classes/Prefix.pch,删除操作选择Remove Reference
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#include "UnityAppController.h"
#import <UIKit/UIKit.h>
#endif
#include "Preprocessor.h"
#include "UnityTrampolineConfigure.h"
#include "UnityInterface.h"
#ifndef __OBJC__
#if USE_IL2CPP_PCH
#include "il2cpp_precompiled_header.h"
#endif
#endif
#ifndef TARGET_IPHONE_SIMULATOR
#define TARGET_IPHONE_SIMULATOR 0
#endif
#define printf_console printf
#endif /* PrefixHeader_pch */
5.修改UnityAppController.mm
#import "AppDelegate.h"
inline UnityAppController* GetAppController()
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
// return (UnityAppController*)[UIApplication sharedApplication].delegate;
// return (UnityAppController*)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityAppController"];
}
第4步 有可能遇到的问题收集来的,也有自己遇到的
1 Unity 3D不支持竖屏,原生项目支持竖屏
//#pragma mark - 横竖屏处理
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if(isUnity){
//游戏的转屏方向 isUnity 在showUnityWindow 中赋值为YES
return UIInterfaceOrientationMaskLandscapeLeft||UIInterfaceOrientationMaskLandscapeRight;
}
//应用的转屏方向
return UIInterfaceOrientationMaskPortrait;
}
2.Unknown type name 'UnityViewControllerBase'
解决办法就是把它改成UIViewController
3.iOS本地应用跳转unity的时候出现黑屏
原因是你的u3d工程集成了EasyAR插件,而这个插件下有个Assets/Plugins/iOS/EasyARAppController.mm,
@interface EasyARAppController : UnityAppController,这是整个app启动时的第一个controller,自己修改合并这个controller就OK了
或者是将EasyARAppController.mm的内容复制到你的自定义的子类中去,然后删除引用也是OK的
在UnityAppController.mm里这个方法中写