Unity跟IOS原生开发项目融合

更多精品文章

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。
[外链图片转存失败(img-wDria5OW-1564106209144)(https://img-blog.csdn.net/20160928200054444)]

步骤:


一、拷贝文件

将UnityProject项目下 Classes Data Libraries MapFileParser MapFileParser.sh 等文件拷贝到Native主项根目录下
这里写图片描述
这里写图片描述
这里写图片描述


二、添加Framework

这里写图片描述


###三、添加配置

1.添加runscript

这里写图片描述

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
这里写图片描述
####4.添加 -DINIT_SCRIPTING_BACKEND=1
这里写图片描述


###四、修改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
这里写图片描述

#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的话,会出现融合后黑屏的问题,解决如下
这里写图片描述


欢迎关注我的围脖
==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243

Unity QQ群:375151422 cocos2dx QQ群:280818155

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

unity交流群

QQ群
unity3d unity 游戏开发

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
您好!针对您的问题,这里提供一些相关的信息供您参考。 首先,Unity是一款跨平台的游戏开发引擎,可以支持在多个平台上进行游戏开发。而Android和iOS是其中比较常见的移动平台。 在Android和iOS平台上嵌入Unity开发示例,可以参考官方文档中的相关内容。具体步骤如下: 1. 创建Unity项目并设置相关参数,例如选择Android或iOS平台,设置屏幕分辨率等。 2. 在Unity项目中添加所需的场景和游戏对象,并设置相应的脚本和材质等。 3. 将Unity项目导出为Android或iOS平台所需的文件格式,例如APK或IPA文件。 4. 在Android或iOS项目中嵌入Unity文件,并通过相关代码进行调用和交互。 需要注意的是,Android和iOS平台上嵌入Unity的具体实现方式可能会有所不同,需要根据具体情况进行调整和修改。 另外,关于在Unity开发Android或iOS应用的示例,也可以参考官方文档中的相关内容。具体步骤如下: 1. 创建Unity项目并设置相关参数,例如选择Android或iOS平台,设置屏幕分辨率等。 2. 在Unity项目中添加所需的场景和游戏对象,并设置相应的脚本和材质等。 3. 使用Unity提供的Android或iOS插件进行开发,例如调用相机或GPS等硬件功能,或者进行网络通信等。 4. 将Unity项目导出为Android或iOS应用程序,并进行相关的测试和调试。 需要注意的是,开发Android或iOS应用程序需要遵循相应的规范和标准,例如Android应用程序需要满足Google Play的要求,iOS应用程序需要通过苹果的审核。因此,在开发过程中需要谨慎处理相关问题。 希望以上信息对您有所帮助,如有任何疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值