首页 > 其他分享 >Unity跟IOS原生开发项目融合

Unity跟IOS原生开发项目融合

时间:2023-04-27 14:33:00浏览次数:52  
标签:原生 unityAppController SRCROOT void IOS application Unity UIApplication


更多精品文章

Aladdin的博客

需求:


做一款社交类的APP,主体工程是原生开发,也就是是IOS的Xcode工程,产品经理为了丰富应用的玩法,就想往里面加上现在比较流行的AR功能,组成一款AR社交APP,我们之前见识多的都是工程是主体,然后IOS原生功能或者代码作为插件添加进Unity的Plugin里面,这样的方式参见文章(举例):Unity和Android交互让手机动起来,相关IOS方面的文章也可以参见Mono的文章。但是作为AR社交我们肯定是以IOS的Xcode功能为主体,Unity的AR功能为宿主,因为毕竟还是属于社交APP。下面就进入正题,如何将Unity的功能模块添加整合进Xcode工程里面,直接上步骤。

介绍:Unity导出一个空的IOS项目,命名为UnityProject,原生应用命名为Native。

步骤:


一、拷贝文件

将UnityProject项目下 Classes Data Libraries MapFileParser MapFileParser.sh 等文件拷贝到Native主项根目录下

Unity跟IOS原生开发项目融合_#endif


Unity跟IOS原生开发项目融合_#endif_02


Unity跟IOS原生开发项目融合_#ifdef_03


二、添加Framework

Unity跟IOS原生开发项目融合_#ifdef_04


###三、添加配置

1.添加runscript

Unity跟IOS原生开发项目融合_ide_05

2.添加Search Paths

Header Search Paths 添加
“$(SRCROOT)/Classes”
“$(SRCROOT)”
$(SRCROOT)/Classes/Native
$(SRCROOT)/Libraries/bdwgc/include
$(SRCROOT)/Libraries/libil2cpp/include
 
Library Search Paths 添加
$(inherited)
“$(SRCROOT)”
“$(SRCROOT)/Libraries”

####3.添加预处理文件

Classes/Prefix.pch

Unity跟IOS原生开发项目融合_#endif_06


####4.添加 -DINIT_SCRIPTING_BACKEND=1

Unity跟IOS原生开发项目融合_#endif_07


###四、修改main.m

复制Classes/main.mm内容到main.m 修改main.m的扩展名为.mm删除Unity生成main.mm return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

###五、修改 unityAppController

inline UnityAppController* GetAppController()
{
return (UnityAppController*)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityAppController"];
}

###六、修改 appdelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    BOOL returnBool;
    if (_unityAppController == nil) {
         
        _unityAppController = [[UnityAppController alloc] init];
    }
    returnBool = [_unityAppController application:application didFinishLaunchingWithOptions:launchOptions];
//    self.window = _unityAppController.window;
     
    HelloViewController *vc = [[HelloViewController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
     
    return YES;
     
}
 
- (void)applicationWillResignActive:(UIApplication *)application {
    [_unityAppController applicationWillResignActive: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.
}
 
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_unityAppController applicationDidEnterBackground: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.
}
 
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [_unityAppController applicationWillEnterForeground: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.
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_unityAppController applicationDidBecomeActive: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.
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
    [_unityAppController applicationWillTerminate:application];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

###七、Error

####1、Expected identifier or ‘(‘

修改UnityViewControllerBaseiOS.h

Unity跟IOS原生开发项目融合_#ifdef_08

#ifdef __cplusplus
extern "C" {
#endif
    void AddViewControllerRotationHandling(Class class_, IMP willRotateToInterfaceOrientation, IMP didRotateFromInterfaceOrientation, IMP viewWillTransitionToSize);
#ifdef __cplusplus
}
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
    void AddViewControllerDefaultRotationHandling(Class class_);
#ifdef __cplusplus
}
#endif

####2、如果存在多个pch配置文件,需要整合为一个
####3、Unity和Xcode版本会存在兼容问题,最好都升级到最新,否则会碰到莫名其妙的报错

###八、关于EasyAR的提示

如果使用EasyAR的话,会出现融合后黑屏的问题,解决如下

Unity跟IOS原生开发项目融合_#endif_09


欢迎关注我的围脖

====================== 相互学习,共同进步 ===================

标签:原生,unityAppController,SRCROOT,void,IOS,application,Unity,UIApplication
From: https://blog.51cto.com/dingxiaowei/6230749

相关文章

  • Unity Profiler使用说明
    CPUUsageProfilermoduleCPU使用分析器模块的图表显示了在应用程序中花费的时间。它包含应用程序花费时间的所有重要领域的概述,比如渲染,它的脚本和动画[toc]。本文包括:-CPU使用图表-CPU使用模块详细信息窗格-Timeline视图-层次结构和原始层次结构视图-常见的样品-......
  • [Unity]AssetBundle资源更新以及多线程下载
    前言此文章适合不太了解资源加载的萌新,有了入门基础之后再去github上搜大牛写的专业的资源加载方案才能得心应手,不然的话会看的很吃力或者说一脸懵逼。Unity里面关于资源加载我们都知道是下载更新AssetBundle,关于AssetBundle我之前的文章已经详细介绍过,没看过的朋友可以在看一下。......
  • [生活日记]参与unity非游戏行业开发者大会小结
    今天下午花了半天时间公司全体都去人民广场参与了一个unity非游戏行业开发者大会,主要了解到unity这款全球顶尖之一的游戏引擎的一个发展史,从05年三个美国人技术研发开始,一直到12年开始引进中国,经过这短短两年左右的时间,获得了逛到游戏开发者的喜爱和肯定,它始于游戏,但非终止于游戏,今......
  • Unity设置中文
    Unity小科普什么是Unity:Unity是实时3D互动内容创作和运营平台。包括游戏开发、元宇宙开发、VR虚拟仿真、AR增强现实、MR混合现实、体感互动Kinect工业产品、数字孪生、三维可视化、大数据可视化、智慧城市、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意变......
  • IOS内购数据拉取
    目标:拉取appstoreconnect内购数据拉取,自己做数据报表。1:api秘钥接口需要token,token生成需要秘钥。参考官方文档:https://developer.apple.com/documentation/appstoreconnectapi/creating_api_keys_for_app_store_connect_api 。  vendorNumber在这个界面查找: ......
  • 原生JS模拟超链接在新窗口打开链接
    可用于点击<li>、<span>等元素动作1functionopenNewWindow(url){2vara=$("<ahref='"+url+"'target='_blank'>"+url+"</a>").get(0),3e=document.createEvent('MouseEvents&......
  • 使用axios下载文件
    使用axios下载文件/***下载文件*@param{string}url下载地址*@param{string}fileName文件名,例:1.png*/exportfunctiondownload(url,fileName){axios({url:url,method:'GET',responseType:'blob',}).then((r......
  • How to use axios.js instead of request.js to get data as a buffer All In One
    Howtouseaxios.jsinsteadofrequest.jstogetdataasabufferAllInOne如何使用axios.js代替request.js获取数据作为缓冲区questionconstfs=require("fs");varpath=require("path");const{exit}=require("process");//requ......
  • Unity内存浅谈一
    Unity主要使用的还是c#,就先从这里写写吧.Net内存管理机制主要还是分为托管堆内存和非托管内存。 .Net托管堆内存管理主要的核心思想就是,你只管制造垃圾,它自己会帮你回收垃圾,因为自己是无法回收自己制造的垃圾的,必须依靠它的垃圾回收机制。托管堆主要的内存产生方式就是new一......
  • iOS描述文件(.mobileprovision)一键申请
    转载:IOS描述文件制作教程iOS描述文件(.mobileprovision)一键申请在主界面上点击描述文件按钮。​编辑切换为居中添加图片注释,不超过140字(可选)  新建ios描述文件然后点击新建,然后输入描述文件名称,描述文件名称字符和数字,自己好辨识就可以。......