ios原生项目内嵌u3d工程

摘要:
其他标志->u3dFramework/LifeCycleListener.h>弱)UIView*播放视图/*显示Unity-(BOOL)isPaused的视图*/+(instancetype)实例;赋值)BOOLisInitUnity;

本文一反常态,目标是把u3d工程以framewWork形式 内嵌原生IOS项目

1、xcode中新建Cocoa Touch FrameWork。取名u3dFrameWork

2、把u3d导出的xcode中,Class、Library 拷贝到u3dFrameWork 中 采用

ios原生项目内嵌u3d工程第1张 方式引入

从library中移除掉libil2cpp

3 以下头文件移动到public 

ios原生项目内嵌u3d工程第2张

4、Build Phases 下头文件引用中 移除RegisterMonoModules.h   compile sources 中移除main.mm 和RegisterMonoModules.cpp  

5、配置修改

  1. Build Settings -> Header Search Paths: $(inherited) "$(SRCROOT)/Classes" "$(SRCROOT)" $(SRCROOT)/Classes/Native $(SRCROOT)/Libraries/bdwgc/include $(SRCROOT)/Libraries/libil2cpp/include
  2. Build Settings -> Library Search Paths: $(inherited) "$(SRCROOT)" "$(SRCROOT)/Libraries"
  3. Build Settings -> Prefix Header: Classes/Prefix.pch
  4. Build Settings -> Mismatched Return Type: Yes
  5. Build Settings -> Enable C++ Exceptions: Yes
  6. Build Settings -> Other Linker Flags: $(inherited) -weak_framework CoreMotion -weak-lSystem
  7. Build Settings -> Mach-O Type: Static Library
  8. Build Settings ->Other C Flags -> $(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DRUNTIME_IL2CPP=1
  9. Build Settings -> Build Avtive Achitecture Only: No

6 u3dFramework.h添加文件引用

  #import <u3dFramework/UnityAppController.h>

  #import <u3dFramework/UnityController.h>

  #import <u3dFramework/RenderPluginDelegate.h>

  #import <u3dFramework/UnityInterface.h>

  #import <u3dFramework/UnityRendering.h>

  #import <u3dFramework/RegisterFeatures.h>

  #import <u3dFramework/UnityForwardDecls.h>

  #import <u3dFramework/LifeCycleListener.h>

7、

ios原生项目内嵌u3d工程第3张

新建UnityController.h****************************

#import <Foundation/Foundation.h>

#import "UnityAppController.h"

@interface UnityController:UnityAppController

@property (nonatomic, readonly, weak) UIView *playView;  /* 展示Unity的view */

+ (instancetype)instance;

- (void)initUnity;

- (void)pauseUnity;

- (void)startUnity;

- (BOOL)isPaused;

@end

新建UnityController.mm************************

#import "UnityController.h"

#import "UnityAppController.h"

#import "DisplayManager.h"

#import "UnityView.h"

#import "UnityAppController+ViewHandling.h"

#import "UnityAppController+Rendering.h"

@interface UnityController()

@property (nonatomic, assign) BOOL isInitUnity;

@end

@implementation UnityController

+ (instancetype)instance {

    return (UnityController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];

}

- (instancetype)init

{

    self = [super init];

    if (self) {

        self.isInitUnity = NO;

        // 注册Unity的事件

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; 

    }

    returnself;

}

- (UIView *)playView {

    returnself.unityView;

}

staticconstint constsection = 0;

- (void)initUnity {

    

    if (!self.isInitUnity) {

        if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)

            [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

        

        UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath] UTF8String]);

        [self selectRenderingAPI];

        [UnityRenderingView InitializeForAPI: self.renderingAPI];

        _window = nil;

        _unityView      = [self createUnityView];

        

        

        [DisplayManager Initialize];

        _mainDisplay    = [DisplayManager Instance].mainDisplay;

        [_mainDisplay createWithWindow: _window andView: _unityView];

        [super applicationDidBecomeActive:[UIApplication sharedApplication]];

        self.isInitUnity = YES;

    }

}

- (void)pauseUnity {

    //[self applicationWillResignActive:[UIApplication sharedApplication]];

    UnityPause(1);

}

- (void)startUnity {

    

    //[self applicationDidBecomeActive:[UIApplication sharedApplication]];

    UnityPause(0);

}

- (BOOL)isPaused {

    if (UnityIsPaused() == 1) {

        returnYES;

    }

    else {

        returnNO;

    }

}

- (void)appWillEnterForeground:(NSNotification *)notification {

    [self applicationWillEnterForeground:[UIApplication sharedApplication]];

}

- (void)appDidBecomeActive:(NSNotification *)notification {

    if (nil == self.unityView) {

        return;

    }

    [self applicationDidBecomeActive:[UIApplication sharedApplication]];

}

- (void)appWillResignActive:(NSNotification *)notification {

    [self applicationWillResignActive:[UIApplication sharedApplication]];

}

- (void)appWillTerminate:(NSNotification *)notification {

    [self applicationWillTerminate:[UIApplication sharedApplication]];

}

- (void)appDidReceiveMemoryWarning:(NSNotification *)notification {

    [self applicationDidReceiveMemoryWarning:[UIApplication sharedApplication]];

}

@end

UnityAppController.h做如下修改******************************

inline UnityAppController* GetAppController()

{

    return (UnityAppController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];

}

二 新建ios原生工程

新建sdk文件夹。将上歩FrameWork和Libraries放入sdk中。Libraries中保留libiPhone-lib.a  libil2cpp.a  libil2cpp   引入sdk文件夹

拷贝u3d工程中Data文件夹 引入工程

Build Settings -> Header Search Paths:$(PROJECT_DIR)/sdk/Libraries/libil2cpp/include

Build Settings -> Library Search Paths: $(inherited) $(PROJECT_DIR)/sdk/Libraries

Build Settings -> Other Linker Flags:-force_load "$(PROJECT_DIR)/sdk/u3dFramework.framework/u3dFramework"

ios原生项目内嵌u3d工程第4张  

 AppDelegate.m做如下修改

#import "AppDelegate.h"

#import <u3dFramework/u3dFramework.h>

@interface AppDelegate ()

    @property(strong,nonatomic) UnityController *unityController;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    if (_unityController == nil)

        _unityController = [[UnityController alloc] init];

    returnYES;

}

ViewController.m做如下操作******************************

#import "ViewController.h"

#import <u3dFramework/u3dFramework.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(60, 60, 80, 40);

    [button setTitle:@"开启Unity" forState:UIControlStateNormal];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(clickHandler:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button1.frame = CGRectMake(160, 60, 80, 40);

    [button1 setTitle:@"暂停Unity" forState:UIControlStateNormal];

    [self.view addSubview:button1];

    [button1 addTarget:self action:@selector(clickHandler1:) forControlEvents:UIControlEventTouchUpInside];

    // 供Unity显示的View

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 300, 300)];

    [view setBackgroundColor:[UIColor grayColor]];

    [view setTag:22];

    [self.view addSubview:view];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (void) clickHandler:(id)sender

{

    [[UnityController instance] initUnity];

    [UnityController instance].playView.frame = [self.view viewWithTag:22].bounds;

    [[self.view viewWithTag:22] addSubview:[UnityController instance].playView];

}

- (void) clickHandler1:(id)sender

{

    if ([[UnityController instance] isPaused]) {

        [[UnityController instance] startUnity];

    }

    else {

        [[UnityController instance] pauseUnity];

    }

}

@end

 

ios原生项目内嵌u3d工程第5张

ios原生项目内嵌u3d工程第6张

免责声明:文章转载自《ios原生项目内嵌u3d工程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DevExpress GridView常用属性[转]mysql导出导入中文表解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

撩课-Web大前端每天5道面试题-Day4

1. 如何实现瀑布流? 瀑布流布局的原理: 1) 瀑布流布局要求要进行布置的元素等宽, 然后计算元素的宽度, 与浏览器宽度之比,得到需要布置的列数; 2) 创建一个数组,长度为列数, 里面的值为已布置元素的总高度(最开始为0); 3) 然后将未布置的元素依次布置到高度最小的那一列, 就得到了瀑布流布局; 4) 滚动加...

CentOS 7上的进程管理

一些杂乱的基础概念 程序是一种静态的文件,躺在磁盘上。而进程则是将程序运行起来放置于内存中。因此进程就是运行中的程序,是程序运行起来的一个实例。同一个程序可以运行为多个进程/实例。 进程之间有父子关系,即父进程与子进程之间的关系。父进程结束后,子进程也会随之结束。因此,当我们通过终端连接上之后,随即启用了一个与终端相关的shell进程(如bash),我们在...

IOS 特定于设备的开发:Core Motion基础

    Core Motion框架集中了运动数据处理。该框架是在IOS 4 SDK中引入的,用于取代accelerometer加速计访问。它提供了对3个关键的机载传感器的集中式监测。这些传感器有陀螺仪、磁力计和加速计组成,其中陀螺仪用于测量设备的旋转,磁力计提供了一种测量罗盘方位的方式,加速计用于监测沿着3根轴的重力变化。第四个入口点称为设备移动(devic...

前端入门flutter-06 ListView基础列表组件、水平列表组件、图标组件

  在日常开发中,经常看到的列表界面就是这集的内容: 垂直列表 垂直图文列表 横向列表 动态列表   ListView组件常用的参数: scrollDirection: Axis.horizontal 横向列表 Axis.vertical 垂直列表(默认垂直列表) padding : EdgeInsetsGeometry, 内边距   re...

springmvc总结(配置传递参数去除前后空格、参数绑定时处理日期)

1.属性为Integer时,前台表单不填,默认为null;属性为String,前台表单不填,默认为"";2.属性为Integer时,前台表单填空格,model封装为null;属性为String,前台表单填空格,model封装为"  ";3.属性为Integer,后台model封装时【去除】前后空格;属性为String,后台model封装时【不去除】前后空格...

ReactNative: 了解相机第三方库react-native-camera的使用

一、简介 在前一篇文章中,初步介绍了RN提供的关于相机功能CameraRoll的使用了。很多时候,这种最基础的API有时很难满足功能需求,此时,如果不想重复造轮子,我们可以选择一个完善好用的第三库。react-native-camera就是一个非常不错的关于相机功能的第三方库,使用这个框架基本能满足大多数的需求,现在来简单研究一下。 二、安装 1、同样地道...