我的另外一篇將成型的iOS工程嵌入到u3d工程中
一 、運行環(huán)境
Unity 5.3.4
Xcode 7.3
二、 配置Unity中的iOS選項:
Auto Graphic API: 去掉勾,然后選擇OpenGLES2
Scripting Backend: IL2CPP
Target Device:iPhone +ipad
Target iOS Version: 8.0
三、 Unity和iOS的結合
第一個就是我們自己的原生工程,第二個是Unity導出的iOS工程。
五、開始整合文件
1)把U3D中的 Classes、Data、Libraries、MapFileParser.sh 文件先粘貼到iOSNative工程的根目錄下
(2)注意這里幾個文件的添加方式,如果添加方式錯誤了,會導致整合的徹底失敗
Classes、Libraries、MapFileParser.sh 通過Add 添加到項目中,注意(選擇Copy items if need 選擇 Create groups)
Data添加到項目中 (選擇Copy items if needs,選中 Create folder references)
3)添加FrameWork
下面的這些都需要添加到iOS 原生工程中去。
注意:libiconv.2.dylib 這個的添加方法
Add other 然后全局搜索 command+shift+G 然后輸入/usr/lib 查找就可以了
六、系統(tǒng)環(huán)境配置
1)添加runScript
2)添加頭文件引用和庫文件運用
3) Project->MyProject->Build Setting
在Other Linker Flags添加
-lc++
-weak_framework
CoreMotion
-weak-lSystem
-Wl,-undefined,dynamic_lookup
在Other C Flags、Other C++ Flags添加
-DINIT_SCRIPTING_BACKEND=1
設置 C Language Dialekt:C99[-std=c99]
設置 C++ Language Dialekt : C++ 11[-std=C++11]
設置 C++ Standard Library : libc++ (LLVM C++ standard Library with C++11 support)
設置 Enable Bitcode : NO
4). Project->MyProject->Build Setting
點擊如下圖的“+”號,選擇4個Add User-Defined Setting
設置key值:GCC_THUMB_SUPPORT,設置Value值:NO
設置key值:GCC_USE_INDIRECT_FUNCTION_CALLS,設置Value值:NO
設置key值:UNITY_RUNTIME_VERSION,設置Value值:5.3.4
設置key值:UNITY_SCRIPTING_BACKEND,設置Value值: il2cpp
5) 選擇Info.plist
添加key值:Unity_LoadingActivityIndicatorStyle,設置Value值:-1
七、文件配置和修改
(1)、在Supporting Files文件夾中創(chuàng)建新的PCH文件,命名為PrefixHeader,如下圖所示勾選上Target,將Classes中的Prefix.pch文件的內容全部拷貝到Supporting Files中的PrefixHeader.pch中。
在Project->MyProject->Build Setting 設置Precompile Prefix Header: YES
并在Prefix Header中添加: $(SRCROOT)/ARHere/PrefixHeader.pch
(2)、將Classes/main.mm全部內容復制到iOS的main.m 并把iOS中的main擴展名改為.mm, 修改如下代碼
然后要刪除Classes/main.mm文件 ,在iOS的main.mm中做如下修改
(3)、AppDelegate的修改
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
@property (strong, nonatomic) UnityAppController *unityController;
- (void)showUnityWindow;
- (void)hideUnityWindow;
@end
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(UIWindow *)unityWindow{
return UnityGetMainWindow();
}
-(void)showUnityWindow{
[self.unityWindow makeKeyAndVisible];
}
-(void)hideUnityWindow{
[self.window makeKeyAndVisible];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
_unityController = [[UnityAppController alloc] init];
[_unityController application:application didFinishLaunchingWithOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
self.window.rootViewController = nav;
[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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
[_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.
[_unityController applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[_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.
[_unityController applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[_unityController applicationWillTerminate:application];
}
@end
(4)、UnityAppController中的修改
#pragma once
#import <QuartzCore/CADisplayLink.h>
#include "PluginBase/RenderPluginDelegate.h"
@class UnityView;
@class DisplayConnection;
@interface UnityAppController : NSObject<UIApplicationDelegate>
{
UnityView* _unityView;
CADisplayLink* _displayLink;
UIWindow* _window;
UIView* _rootView;
UIViewController* _rootController;
UIView* _snapshotView;
DisplayConnection* _mainDisplay;
// we will cache view controllers for fixed orientation
// auto-rotation view contoller goes to index=0
// UnityViewControllerBase* _viewControllerForOrientation[5];
UIViewController* _viewControllerForOrientation[5]; //這里修改了。
#if !UNITY_TVOS
UIInterfaceOrientation _curOrientation;
#endif
id<RenderPluginDelegate> _renderDelegate;
}
// override it to add your render plugin delegate
- (void)shouldAttachRenderDelegate;
// this one is called at the very end of didFinishLaunchingWithOptions:
// after views have been created but before initing engine itself
// override it to register plugins, tweak UI etc
- (void)preStartUnity;
// this one is called at first applicationDidBecomeActive
// NB: it will be started with delay 0, so it will run on next run loop iteration
// this is done to make sure that activity indicator animation starts before blocking loading
- (void)startUnity:(UIApplication*)application;
// this is a part of UIApplicationDelegate protocol starting with ios5
// setter will be generated empty
@property (retain, nonatomic) UIWindow* window;
@property (readonly, copy, nonatomic) UnityView* unityView;
@property (readonly, copy, nonatomic) CADisplayLink* unityDisplayLink;
@property (readonly, copy, nonatomic) UIView* rootView;
@property (readonly, copy, nonatomic) UIViewController* rootViewController;
@property (readonly, copy, nonatomic) DisplayConnection* mainDisplay;
#if !UNITY_TVOS
@property (readonly, nonatomic) UIInterfaceOrientation interfaceOrientation;
#endif
@property (nonatomic, retain) id renderDelegate;
@property (nonatomic, copy) void(^quitHandler)();
@end
// Put this into mm file with your subclass implementation
// pass subclass name to define
#define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
@interface ClassName(OverrideAppDelegate) \
{ \
} \
+(void)load; \
@end \
@implementation ClassName(OverrideAppDelegate) \
+(void)load \
{ \
extern const char* AppControllerClassName; \
AppControllerClassName = #ClassName; \
} \
@end \
//inline UnityAppController* GetAppController()
//{
// return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
// 這里也修改了
#import "AppDelegate.h"
inline UnityAppController* GetAppController()
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
}
#define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
do { \
id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
if([delegate respondsToSelector:@selector(method)]) \
[delegate method]; \
} while(0)
#define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
do { \
id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
if([delegate respondsToSelector:@selector(method:)]) \
[delegate method:arg]; \
} while(0)
// these are simple wrappers about ios api, added for convenience
void AppController_SendNotification(NSString* name);
void AppController_SendNotificationWithArg(NSString* name, id arg);
void AppController_SendUnityViewControllerNotification(NSString* name);
(5)、刪除Main.storyboard
移除Main interface中的默認Main
八、啟動和隱藏U3d界面
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property(strong, nonatomic) UIButton *Btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_Btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 200, 80, 70)];
_Btn.backgroundColor = [UIColor cyanColor];
[_Btn setTitle:@"開啟" forState:UIControlStateNormal];
[_Btn addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:_Btn];
}
-(void)go{
//進入unity界面
[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
UnityPause(false);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end