Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created August 28, 2015 15:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save haxpor/4c8e457d2f000bcd442b to your computer and use it in GitHub Desktop.
//
// UnityNativeInterface.m
// Unity-iPhone
//
// Created by Sam Izzo on 30/05/14.
//
//
#import "UnityInterface.h"
#import "UnityNativeInterface.h"
#import "NativeInterface.h"
#import "UnityViewControllerBase.h"
#import "UnityAppController.h"
#ifndef __cplusplus
//extern void UnityPause(bool pause);
#endif
@interface CustomAppController : UnityAppController
{
UIViewController* m_unityViewController;
NativeInterface* m_nativeInterface;
bool m_inNative;
}
- (void) switchToNative;
- (void) switchToUnity;
//- (void) createViewHierarchyImpl;
@end
@implementation CustomAppController
//- (void) createViewHierarchyImpl;
- (void)willStartWithViewController:(UIViewController*)controller;
{
_rootController = [[UnityDefaultViewController alloc] init];
_rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
m_nativeInterface = [[NativeInterface alloc] init];
m_unityViewController = [[UIViewController alloc] init];
m_unityViewController.view = _unityView;
_rootController.view = _rootView;
[_rootView addSubview:m_unityViewController.view];
m_inNative = false;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToUnity) name:@"SwitchToUnityRequested" object:nil];
}
- (void) switchToNative;
{
if (!m_inNative)
{
NSLog(@"Switching to native");
[m_unityViewController.view removeFromSuperview];
[_rootView addSubview:m_nativeInterface.ViewController.view];
m_inNative = true;
UnityPause(true);
}
else
{
NSLog(@"Already in native, not switching");
}
}
- (void) switchToUnity;
{
if (m_inNative)
{
m_inNative = false;
[m_nativeInterface removeView];
[_rootView addSubview:m_unityViewController.view];
UnityPause(false);
}
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(CustomAppController)
#ifdef __cplusplus
extern "C"
{
#endif
void SwitchToNative()
{
CustomAppController* controller = (CustomAppController*)[UIApplication sharedApplication].delegate;
[controller switchToNative];
}
#ifdef __cplusplus
}
#endif
@haxpor
Copy link
Author

haxpor commented Aug 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment