iOS开发 Unity如何嵌入UIKit

作者: iManuQiao | 来源:发表于2016-07-08 16:21 被阅读1752次

在开发iOS app的时候,有时候我们需要把一些功能交给Unity做,然后整合到native工程里面。

之前也是找了很多帖子,最后找到the nerd。他的方案很详细而且实测可用。下面我来精简一下步骤,并做一下翻译。另外,由于他用的是老版本的Unity,这篇文章适用于最新的5.3.5f1版本。

原文链接请点这里


7月14日更新:
已上传示例文件到Github
文件用法请见文末。


首先看下Unity上面的操作。

  1. 先让我们变出一个Unity工程。没有的话可以github上找一个示例工程
  2. 直接进入File->Build Settings或者快捷键shift+cmd+B。
  3. 出现一个小窗


    build settings.png
  4. Platform选择iOS。点击Add Open Scenes。Run in Xcode as Debug。
  5. 点击Build And Run。
    此时如果不是第一次导出,会询问是否要更新(append)或者替换(replace)原工程。都可以。


    replace or append.png
  6. 选择导出的工程位置。等待一杯咖啡的时间。
    不要急着打开工程所在文件夹。看任务栏里Unity的图标上会有一个进度饼,转完一圈才是真的结束。

然后进到Xcode进行操作。

  1. 打开导出的工程。General设置一下Bundle Identifier。试着在真机上面运行一下。Unity工程不支持iOS模拟器运行。

  2. 然后新建一个自定义View Controller,用来把Unity视图放进去。随便在界面上弄点什么。
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

        // 为了体现这个是自定义视图及控制器,随便在view上加点内容
        [self.view setBackgroundColor:[UIColor greenColor]];
        UILabel *label = [UILabel new];
        [label setText:@"this is container view"];
        [label setFrame:CGRectMake(0, 0, 0, 0)];
        [label sizeToFit];
        [self.view addSubview:label];
    }
    
  3. 接着新建一个.mm文件。我们就沿袭the nerd原文里的文件名也无妨。TNAppController.mm。然后复制以下代码

    #import <UIKit/UIKit.h>
    #import "UnityAppController.h"
    #import "UI/UnityView.h"
    #import "UI/UnityViewControllerBase.h"
    #import "ViewController.h"
    
    @interface TNAppController : UnityAppController
    
    @end
    
    @implementation TNAppController
    
    - (void)willStartWithViewController:(UIViewController*)controller {
        // 新建自定义视图控制器。
        ViewController *viewController = [[ViewController alloc] init];
    
        // 把Unity的内容视图作为子视图放到我们自定义的视图里面。
        [viewController.view addSubview:_unityView];
        [_unityView setFrame:CGRectMake(100, 100, 100, 100)];
    
        // 把根视图和控制器全部换成我们自定义的内容。
        _rootController = viewController;
        _rootView = _rootController.view;
    }
    
    @end
    
     //注意这一句,非常重要。如果你要对UnityAppController写子类,这句不能省略。
     IMPL_APP_CONTROLLER_SUBCLASS(TNAppController)
    

这里说几句。老版本的unity重写的方法不一样,可以看到源码里面多了三个Assert。

   - (id)init {
       if( (self = [super init]) )
       {
           // due to clang issues with generating warning for overriding deprecated methods
           // we will simply assert if deprecated methods are present
           // NB: methods table is initied at load (before this call), so it is ok to check for override
           NSAssert(![self respondsToSelector:@selector(createUnityViewImpl)], 
               @"createUnityViewImpl is deprecated and will not be called. Override createUnityView"
           );
           NSAssert(![self respondsToSelector:@selector(createViewHierarchyImpl)], 
               @"createViewHierarchyImpl is deprecated and will not be called. Override willStartWithViewController"
           );
           NSAssert(![self respondsToSelector:@selector(createViewHierarchy)], 
               @"createViewHierarchy is deprecated and will not be implemented. Use createUI"
           );
       }
       return self;
   }

对了,the nerd的帖子里就是重写的createViewHierarchyImpl。但是现在新版Unity会报错了。不过总体思路没变,就是把_rootViewController替换成自定义的View Controller。Unity显示的内容直接从_unityView成员访问就行了,把它作为subview加入自定义的视图里。


7月14日更新:
Github工程上获取示例文件的用法:

  1. 为避免与原Unity工程的文件混淆,把Github工程里的文件放在一个文件夹里。


    文件夹.png
  2. 把文件夹拖入工程根目录中。


    group.png
  3. 编译运行即可看到效果。

相关文章

网友评论

  • 8ee595aa79c0:请教下,为什么您提供的这个demo,我运行之后加了个uibutton,不响应点击事件?
    b0958d5eac00:我也遇见uibutton不响应了,请问你解决了吗
  • Cc_1987:求Unity添加到View的demo
  • 睿少:那我想将unityView添加到一个有Nav的controller上怎么做啊?
    睿少:@iManuQiao 我想的是从一个Controller,push到放有unityView的controller上,我不知道怎么为第一个Controller创建navigationController,我知道应该在UnityAppController里面设置,但总设置不对!大神!!!help me!!
    iManuQiao:加到controller.view上面,然后把controller push到navController里面
  • 睿少:是不是相当于unity与iOS的交互,代码实现全在unity中,在iOS段就只需将它导入程序就是了??
    睿少:@iManuQiao
  • 751fc49dcbfd:什么功能需要用到unity
    iManuQiao:不难。我找时间做个demo工程吧。
    751fc49dcbfd:@iManuQiao 难做吗 分享下链接:+1:
    iManuQiao:当时要用到一个漫画播放编辑器。这个模块经过我们讨论使用unity实现。

本文标题:iOS开发 Unity如何嵌入UIKit

本文链接:https://www.haomeiwen.com/subject/losljttx.html