unity2019版本融合iOS

unity2019版本融合iOS

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 #include #include #include UnityFramework* UnityFrameworkLoad() { NSString* bundlePath = nil; bundlePath = [[NSBundle mainBundle] bundlePath]; bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/ork"]; NSBundle* bundle = [NSBundle bundleWithPath: bundlePath]; if ([bundle isLoaded] == false) [bundle load]; UnityFramework* ufw = [palClass getInstance]; if (![ufw appController]) { // unity is not initialized [ufw setExecuteHeader: &_mh_execute_header]; } return ufw; } void showAlert(NSString* title, NSString* msg) { UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; // auto delegate = [[UIApplication sharedApplication] delegate]; UIViewController *vc = [UIApplication sharedApplication].ewController; [vc presentViewController:alert animated:YES completion:nil]; } int gArgc = 0; char** gArgv = nullptr; NSDictionary* appLaunchOpts; @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @property UnityFramework* ufw; @property (assign, nonatomic) BOOL isStartUnity ; - (void)initUnity; - (void)ShowMainView; - (void)didFinishLaunching:(NSNotification*)notification; - (void)didBecomeActive:(NSNotification*)notification; - (void)willResignActive:(NSNotification*)notification; - (void)didEnterBackground:(NSNotification*)notification; - (void)willEnterForeground:(NSNotification*)notification; - (void)willTerminate:(NSNotification*)notification; - (void)unityDidUnloaded:(NSNotification*)notification; @end @implementation AppDelegate - (bool)unityIsInitialized { return [self ufw] && [[self ufw] appController]; } #pragma mark - 打开3D - (void)showUnityView { if(![self unityIsInitialized]) { showAlert(@"Unity is not initialized", @"Initialize Unity first"); } else { [[self ufw] showUnityWindow]; } } #pragma mark - 打开原⽣ #pragma mark - 打开原⽣ - (void)showNativeView { [ makeKeyAndVisible]; } #pragma mark - 发消息 - (void)sendMsgToUnity:(NSString *)strClass method:(NSString *)strMethod value:(NSString *)strValue { // [[self ufw] sendMessageToGOWithName: "Cube" functionName: "ChangeColor" message: "yellow"]; [[self ufw] sendMessageToGOWithName:8String functionName:8String message:8String]; }- (void)showHostMainWindow{ [self showHostMainWindow:@""];}- (void)showHostMainWindow:(NSString*)color{// if([color isEqualToString:@"blue"]) oundColor = lor;// else if([color isEqualToString:@"red"]) oundColor = or;// else if([color isEqualToString:@"yellow"]) oundColor = Color;// [UIApplication sharedApplication].ewController = ;// [ makeKeyAndVisible]; [self showNativeView];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self initUnity]; if (@available(iOS 10.0, *)) {

// [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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信