IOS 集成unity(爬坑篇,完整版)

<b>一.配置问题<b>

<b>1.将unity工程中的这几个文件夹copy到ios工程中的根目录下面</b>


文件夹截图.png

<b>2.在工程中引入,class,libraries,data,MapFileParse.sh。并且删除class-native-所有.h文件</b>


data引入与其他不一样.png

<b>3.在工程中添加需要的库</b>


除系统外的库为第三方库.png

<b>4.build setting中更改配置选项</b>

1.enable Bitcode NO

2
3
4.可以略过,如果冲突
5
6
10
7
8
9
11

<b>二.代码部分</b>

//AppDelegate.h
#import "AppDelegate.h"

inline UnityAppController*  GetAppController()
{
    AppDelegate*delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return delegate.unityController;
}
//pch文件
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
#endif

#include "Preprocessor.h"
#include "UnityTrampolineConfigure.h"
#include "UnityInterface.h"

#ifndef __OBJC__
#if USE_IL2CPP_PCH
#include "il2cpp_precompiled_header.h"
#endif
#endif

#ifndef TARGET_IPHONE_SIMULATOR
#define TARGET_IPHONE_SIMULATOR 0
#endif

#define printf_console printf

//删除unity的 main.mm  复制到ios工程的main.m并改为main.mm

//修改的地方
#import "AppDelegate.h"
UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

//AppDelegate.h添加部分 
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;

@property (strong, nonatomic) UnityAppController *unityController;

- (void)showUnityWindow;
- (void)hideUnityWindow;


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


-(UIWindow *)unityWindow{
    return  UnityGetMainWindow();
}

-(void)showUnityWindow{
    [self.unityWindow makeKeyAndVisible];
}

-(void)hideUnityWindow{
    [self.window makeKeyAndVisible];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    self.unityController = [[UnityAppController alloc]init];
    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
    
    [self showUnityWindow];
    //    [[DownloadNetWorkForUnity sharedInstance] downLoadMode:@"https://api-871ar.eschervr.com/resource/get_list?device_type=ios"];
    return YES;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    [self.unityController 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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.unityController 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 {
    [self.unityController applicationWillEnterForeground:application];
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self.unityController 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 {
    [self.unityController applicationWillTerminate:application];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


</b>
</b>
<b>三.常见坑<b>
1.clang: error: no such file or directory: 'CoreMotion'
位置不能改变

-weak_framework
CoreMotion
-weak-lSystem
-licucore

2.main.mm
3.UnityAppController

AppDelegate*delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
return delegate.unityController;

4.相机权限

Privacy - Camera Usage Description
Camera access required for target detection and tracking 

</b>
<h3>三.互相调用代码整理</h3>

<b>参考<b>:
http://blog.csdn.net/dylan_lwb_/article/details/51452470
//www.greatytc.com/p/a941230b626d
http://www.cnblogs.com/Erma-king/p/5544502.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,930评论 25 708
  • 前言 2000年,伊利诺伊大学厄巴纳-香槟分校(University of Illinois at Urbana-...
    星光社的戴铭阅读 15,942评论 8 180
  • unity版本 2017.1.1f1 Xcode版本8.2.1 (8C1002) 第一步 导出unity的Xco...
    野生塔塔酱阅读 3,881评论 5 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,853评论 18 139
  • 2011-06-16 行走 以前在家乡的时候,总是向往出发,向往离开,觉得远方的风景总比身边的美,现在回过头看,我...
    ss234阅读 430评论 0 1