经过各种google总共发现了两个方案:
1,直接嵌入工程:尝试各种报文件找不到。。。放弃
2,编译产物接入:成功
产物有两个:1UnityFramework.framework。2,Data资源。文件拖入即可
代码步骤:
1,使用单例保存main函数参数
[ConfigObj shareInstance].gArgc = argc;
[ConfigObj shareInstance].gArgv = argv;
2,AppDelegate 添加协议UnityFrameworkListener,和相应的property
#import <UIKit/UIKit.h>
#include <UnityFramework/UnityFramework.h>
#import "ConfigObj.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,UnityFrameworkListener>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) UnityFramework *ufw;
@end
3,加载framework,添加监听等操作
#import "AppDelegate.h"
#import "UnityFramework.framework/Headers/UnityAppController.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])
{
// unity is not initialized
[ufw setExecuteHeader: &_mh_execute_header];
}
return ufw;
}
@interface AppDelegate ()
@end
@implementation AppDelegate
- (bool)unityIsInitialized {
return [self ufw] && [[self ufw] appController];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self initUnityWithOptions:launchOptions];
});
return YES;
}
- (void)initUnityWithOptions:(NSDictionary *)launchOptions
{
[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.UnityIOS.I12"];
[[self ufw] registerFrameworkListener: self];
// [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
[[self ufw] runEmbeddedWithArgc: [ConfigObj shareInstance].gArgc argv: [ConfigObj shareInstance].gArgv appLaunchOpts: launchOptions];
UIView *view = [[[self ufw] appController] rootView];
[ConfigObj shareInstance].unityView = view;
}
- (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
问题记录:
bundleId必须和framework的bundle名字一样否则报错
[[self ufw] setDataBundleId: "com.unity3d.framework"];
工程的inplist文件配置copy一下,可能有重要信息
参考
https://blog.csdn.net/baidu_25743639/article/details/73250667