iOS集成UnityAR项目

2019-02-22  本文已影响0人  原味丿丿咖啡Vitas

最近公司需要开发AR项目,在网上找来Unity项目研究了一下,用Unity生成iOS项目后需要导入到自己已有的项目中,步骤如下:

导入Unity项目文件

我们之前生成的Unity项目是这样的,我们需要导入的文件如下图标注


项目目录 Classes和Libraries文件以及Vuforia.framework 导入方式 Data文件导入方式 Vuforia
       #import <UIKit/UIKit.h>
       #import "AppDelegate.h"
       #include "RegisterMonoModules.h"
       #include "RegisterFeatures.h"
       #include <csignal>

       static const int constsection = 0;

       void UnityInitTrampoline();

      // WARNING: this MUST be c decl (NSString ctor will be called after       +load, so we cant really change its value)
       const char* AppControllerClassName = "ViewController";

       int main(int argc, char * argv[]) {
          UnityInitStartupTime();
          @autoreleasepool {
    
              UnityInitTrampoline();
    
              UnityInitRuntime(argc, argv);
              RegisterMonoModules();
              //        NSLog(@"-> registered mono modules %p\n", &constsection);
              RegisterFeatures();
              // iOS terminates open sockets when an application enters background mode.
              // The next write to any of such socket causes SIGPIPE signal being raised,
              // even if the request has been done from scripting side. This disables the
              // signal and allows Mono to throw a proper C# exception.
              std::signal(SIGPIPE, SIG_IGN);
    
            //        return UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
           return UIApplicationMain(argc, argv, nil,   NSStringFromClass([AppDelegate class]));
    }
}
other Link Flags
-weak_framework
CoreMotion
-weak-lSystem
Header Search Paths
//具体根据文件目录而定
"$(SRCROOT)/BuddhaARDemo"  
"$(SRCROOT)/BuddhaARDemo/Classes"
"$(SRCROOT)/BuddhaARDemo/Classes/Native"
"$(SRCROOT)/BuddhaARDemo/Libraries"
"$(SRCROOT)/BuddhaARDemo/Libraries/libil2cpp/include"
Other C Flags
-DINIT_SCRIPTING_BACKEND=1
-fno-strict-overflow
-DRUNTIME_IL2CPP=1
pch文件 0CD804D8ED94E500BF2F72EC8388B67E.jpg
GCC_THUMB_SUPPORT:NO 
GCC_USE_INDIRECT_FUNCTION_CALLS:NO 
UNITY_RUNTIME_VERSION:2018.2.9f1  <--这个值根据自己unity-demo来 
UNITY_SCRIPTING_BACKEND: il2cpp

效果如下

image.png 相关依赖库

AppDelegate.h:

#import <UIKit/UIKit.h>
@class VTUnityController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic , strong)UIWindow *unityWindow;
@property (nonatomic, strong) VTUnityController *unityController;
- (void)showUnityWindow;
- (void)hideUnityWindow;

@end

AppDelegate.mm:

#import "AppDelegate.h"
#import "VTUnityController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if (_unityController == nil) {
        _unityController = [[VTUnityController alloc]init];
    }
    return YES;
}


- (UIWindow *)unityWindow
{
    if (!_unityWindow) {
        if (!UnityGetMainWindow()) {
            _unityWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        }else{
            _unityWindow = UnityGetMainWindow();
        }
    }
    return _unityWindow;
}
- (void)showUnityWindow
{
    [self.unityWindow makeKeyAndVisible];
}

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

@end

VTUnityController.h

#import "UnityAppController.h"

NS_ASSUME_NONNULL_BEGIN

@interface VTUnityController : UnityAppController
+ (instancetype)instance;

- (void)initUnity;

- (void)pauseUnity;

- (void)startUnity1;

- (BOOL)isPaused;
@end

NS_ASSUME_NONNULL_END

VTUnityController.m

#import "VTUnityController.h"
//#import "UnityAppController.h"
#import "UnityAppController+ViewHandling.h"
#import "UnityAppController+Rendering.h"

#import "DisplayManager.h"
#import "UnityView.h"

#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
//#include <csignal>
#import "AppDelegate.h"

@interface VTUnityController()

@property (nonatomic, assign) BOOL isInitUnity;

@end

@implementation VTUnityController
+ (instancetype)instance {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return 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];
        
    }
    return self;
}

void UnityInitTrampoline();

- (void)initUnity {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (!self.isInitUnity) {
        
        [super applicationDidBecomeActive:[UIApplication sharedApplication]];
        
        UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath] UTF8String]);
        [self selectRenderingAPI];
        [UnityRenderingView InitializeForAPI: self.renderingAPI];
        
        _window = delegate.unityWindow;
        _unityView      = [self createUnityView];
        
        [DisplayManager Initialize];
        _mainDisplay    = [DisplayManager Instance].mainDisplay;
        [_mainDisplay createWithWindow: _window andView: _unityView];
        
        [self createUI];
        [self preStartUnity];
        self.isInitUnity = YES;
        
        _unityView.back = ^{
            [delegate hideUnityWindow];
        };
        
    }else{
        [self startUnity1];
    }
    [delegate showUnityWindow];
}

- (void)pauseUnity {
    UnitySendMessage("ARCamera", "Exit", "");  // 调Unity方法 退出模型 (与unity交互)
    UnityPause(1);
}

- (void)startUnity1 {
    UnityPause(0);
}

- (BOOL)isPaused {
    if (UnityIsPaused() == 1) {
        return YES;
    }
    else {
        return NO;
    }
}

-(void)applicationDidFinishLaunching:(UIApplication *)application{
    
}

- (void)appWillEnterForeground:(NSNotification *)notification {
    [super applicationWillEnterForeground:[UIApplication sharedApplication]];
}

- (void)appDidBecomeActive:(NSNotification *)notification {
    if (nil == self.unityView) {
        return;
    }
    [super applicationDidBecomeActive:[UIApplication sharedApplication]];
}

- (void)appWillResignActive:(NSNotification *)notification {
    [super applicationWillResignActive:[UIApplication sharedApplication]];
}

- (void)appWillTerminate:(NSNotification *)notification {
    [super applicationWillTerminate:[UIApplication sharedApplication]];
}

- (void)appDidReceiveMemoryWarning:(NSNotification *)notification {
    [super applicationDidReceiveMemoryWarning:[UIApplication sharedApplication]];
}

@end
- (void)startAR {
  
    VTUnityController *vc = [VTUnityController instance];
    [vc initUnity];
}

效果如下

效果图

如遇到image not found,请参考我的另一篇文章

https://www.jianshu.com/p/cadd1dc95cf5

上一篇 下一篇

猜你喜欢

热点阅读