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>


除系統(tǒng)外的庫為第三方庫.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
http://www.itdecent.cn/p/a941230b626d
http://www.cnblogs.com/Erma-king/p/5544502.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,922評論 25 709
  • 前言 2000年,伊利諾伊大學厄巴納-香檳分校(University of Illinois at Urbana-...
    星光社的戴銘閱讀 16,274評論 8 180
  • unity版本 2017.1.1f1 Xcode版本8.2.1 (8C1002) 第一步 導出unity的Xco...
    野生塔塔醬閱讀 4,006評論 5 4
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評論 19 139
  • 2011-06-16 行走 以前在家鄉(xiāng)的時候,總是向往出發(fā),向往離開,覺得遠方的風景總比身邊的美,現(xiàn)在回過頭看,我...
    ss234閱讀 476評論 0 1

友情鏈接更多精彩內容