2023年7月13日发(作者:)
unity2019版本融合iOS基本思路:Xcode ⼯程创建⼯作空间,⼀个⼯作空间包含 iOS原⽣⼯程,⼀个为3D⼯程。通过ork 进⾏桥接通信。 unity⼯程注册代理,原⽣实现交互⽅法、unity 调取iOS 代理模式交互数据。iOS调取unity 通过UnityFramework 提供的发送消息⽅法。1.⽣成iOS⼯程⾸先在Unity编辑器打开UnityProject项⽬,选择Menu -> Window -> Package Manager,因为2.0.8版本不兼容使⽤Unity作为库,所以要移除Ads资源包,或更新Ads资源包到v 3.*版本。选择Menu -> Edit -> Player Settings -> Player -> iOS设置标签页 -> Identification Section,设置有效的Bundle Identification和SigningTeam ID,以避免后续步骤出现Xcode签名问题。打开Menu -> File -> Builds Settings,在此选择并切换平台为iOS。将UnityProject项⽬构建到iosBuild⽂件夹。2.创建Xcode⼯作空间设置Xcode⼯作空间 Xcode⼯作空间允许同时处理多个项⽬,并结合它们的结果。 我们在Xcode打开roj。选择File -> New -> Workspace,创建⼯作空间,把它保存在UaaLExample/space。关闭roj项⽬,此后的步骤会在刚创建的⼯作空间项⽬完成。选择File -> Add Files to “both”,把roj和第2步⽣成的roj添加到相同等级的⼯作空间。3.将iOS⼯程 加⼊⼯作空间将unity导出⼯程加⼊⼯作空间如果使⽤pod 将pods ⼯程加⼊⼯作空间file -> addFile to ""3个⼯作空间平级4. 添加5.设置Data⽂件夹为 ork 部分6.公开桥接⽂件⼯程配置完毕。以下是代码配置1. 修改为 MM将 main.m 与AppDelegate 写⼊同⼀个⽂件 #include
// [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {// NSInteger appMemoryBytes = qs_getAppMemoryBytes();// NSLog(@"使⽤了 %f MB 内存", appMemoryBytes / 1024.0f/ 1024.0f);// }]; } else { // Fallback on earlier versions } return YES;}//获取当前App的内存使⽤值uint64_t qs_getAppMemoryBytes() { task_vm_info_data_t vmInfo; mach_msg_type_number_t count = TASK_VM_INFO_COUNT; kern_return_t result = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count); if (result != KERN_SUCCESS) return 0; return _footprint;}#pragma mark - 初始化UnityManage#pragma mark - 初始化3D- (void)initUnity{ if([self unityIsInitialized]) { showAlert(@"Unity already initialized", @"Unload Unity first"); return; } [self setUfw: UnityFrameworkLoad()]; [[self ufw] setDataBundleId: "ork"]; [[self ufw] registerFrameworkListener:self]; [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self]; [[self ufw] runEmbeddedWithArgc: gArgc argv: gArgv appLaunchOpts: appLaunchOpts]; [NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) { [self showUnityView]; }];}- (void)unloadUnity{ if(![self unityIsInitialized]) { showAlert(@"Unity is not initialized", @"Initialize Unity first"); } else {// [UnityFrameworkLoad() unloadApplicaion: true]; }}#pragma mark - 释放回调- (void)unityDidUnload:(NSNotification*)notification{ NSLog(@"unityDidUnloaded called"); [[self ufw] unregisterFrameworkListener: self]; [self setUfw: nil]; }- (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];}@endint main(int argc, char * argv[]) { NSString * appDelegateClassName; gArgc = argc; gArgv = argv; @autoreleasepool { if (false) { // run UnityFramework as main app id ufw = UnityFrameworkLoad();
// Set UnityFramework target for Unity-iPhone/Data folder to make Data part of a ork and call to setDataBundleId // ODR is not supported in this case, ( if you need embedded and ODR you need to copy data ) [ufw setDataBundleId: "ork"]; [ufw runUIApplicationMainWithArgc: argc argv: argv]; } else { // run host app first and then unity later UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: "AppDelegate"]); } } return UIApplicationMain(argc, argv, nil, appDelegateClassName);}采坑经验:⽂件夹⼀定要 包含到 ork 不然报错CallProxy.h 包含到 ork 并且公开3. 报错 # [: Permission denied]此报错因⽂件权限问题导致执⾏指令chmod +x /Users/......./ (所在的⽬录)启动成功 直接跳⼊3D页⾯找到 unity⼯程中的⼏处跳转 全部注释关键字 [window makeKeyAndVisible];5.僵⼫断点会导致untiy⼯程内存持续增加。不要勾选
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689262502a226324.html
评论列表(0条)