1.先准备好原生工程【KW_OC】和导出的unity工程【UnityProject】
2.打开UnityProject,检查Data文件是否勾选UnityFramework
3.选择真机设备,Command+B 进行编译,编译成功后会看到 UnityFramework文件变色了
4.点击Show in Finder ,打开文件目录
5.将Unity项目嵌入原生项目
-
打开原生工程,左下角 + 按钮
-
选中xcodeproj文件,右下角add
-
现在原生工程文件目录就变成这样了
-
编译UnityFramework 【Command + B】
- 配置Framework
-
- Frameworks,Librarys,and... 展开后点击+ , AddFile
- Frameworks,Librarys,and... 展开后点击+ , AddFile
-
- 把刚才的framework文件拖进来,点击open
- 把刚才的framework文件拖进来,点击open
6.嵌入完成 进入代码部分
- appdelegate.h 文件
#include <UnityFramework/UnityFramework.h>
#include <UnityFramework/NativeCallProxy.h>
@property UnityFramework* ufw;
- appdelegate.m 文件
#import "AppDelegate.h"
UnityFramework* UnityFrameworkLoad() {
NSString* bundlePath = nil;
bundlePath = [[NSBundle mainBundle] bundlePath];
bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];
NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
if ([bundle isLoaded] == false) [bundle load];
UnityFramework* ufw = [bundle.principalClass getInstance];
if (![ufw appController]) {
[ufw setExecuteHeader: &_mh_execute_header];
}
return ufw;
}
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initUnityWithLaunchOptions:launchOptions]
}
//初始化unity
- (void)initUnityWithLaunchOptions:(NSDictionary *)options {
/*
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *receiveUnityMsg = [user objectForKey:@"receiveUnityMsg"];
NSString *unityType = [user objectForKey:@"Unity_Type"];
if (![receiveUnityMsg isEqualToString:unityType]) {
[[self ufw] unregisterFrameworkListener: self];
[self setUfw: nil];
}
_shouldChangeOrientation = YES;
if([self unityIsInitialized]) {
[[self ufw] showUnityWindow];
[[self ufw]pause:false];
}
*/
[self setUfw: UnityFrameworkLoad()];
// Set UnityFramework target for Unity-iPhone/Data folder to make Data part of a UnityFramework.framework and uncomment call to setDataBundleId
// ODR is not supported in this case, ( if you need embedded and ODR you need to copy data )
[[self ufw] setDataBundleId: "com.unity3d.framework"];
[[self ufw] registerFrameworkListener: self];
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
[[self ufw] runEmbeddedWithArgc: gArgc argv: gArgv appLaunchOpts: appLaunchOpts];
}
//其他设置
- (void)applicationWillResignActive:(UIApplication *)application {
[[[self ufw] appController] applicationWillResignActive: application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[[self ufw] appController] applicationDidEnterBackground: application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[[self ufw] appController] applicationWillEnterForeground: application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[[self ufw] appController] applicationDidBecomeActive: application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[[self ufw] appController] applicationWillTerminate: application];
}
@end