/
UnityNativeManager.mm
156 lines (113 loc) · 4.29 KB
/
UnityNativeManager.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// UnityNativeManager.mm
// Unity-iPhone
//
// Created by Mike on 8/7/10.
// Copyright 2010 Prime31 Studios. All rights reserved.
//
#import "UnityNativeManager.h"
void UnityPause( bool pause );
@implementation UnityNativeManager
@synthesize navigationControler = _navigationControler, animationType = _animationType,
animationSubtype = _animationSubtype, animationTimingFunction = _animationTimingFunction,
animationDuration = _animationDuration;
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark NSObject
+ (UnityNativeManager*)sharedManager
{
static UnityNativeManager *sharedManager = nil;
if( !sharedManager )
sharedManager = [[UnityNativeManager alloc] init];
return sharedManager;
}
- (id)init
{
if( ( self = [super init] ) )
{
// Default to fade animation and reasonable duration
self.animationType = kCATransitionFade;
self.animationDuration = 0.5;
self.animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Private
- (NSString*)oppositeAnimationSubtype
{
if( _animationSubtype == kCATransitionFromRight )
return kCATransitionFromLeft;
if( _animationSubtype == kCATransitionFromLeft )
return kCATransitionFromRight;
if( _animationSubtype == kCATransitionFromTop )
return kCATransitionFromBottom;
if( _animationSubtype == kCATransitionFromBottom )
return kCATransitionFromTop;
return nil;
}
- (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag
{
UnityPause( false );
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Public
- (void)setAnimationType:(NSString*)animationType subtype:(NSString*)animationSubtype
{
self.animationType = animationType;
self.animationSubtype = animationSubtype;
}
- (void)showViewControllerWithName:(NSString*)name
{
// Grab the controller from the given class name. Early out if we dont have it available
Class controllerClass = NSClassFromString( name );
if( !controllerClass )
return;
// Pause Unity
UnityPause( true );
// Instantiate the controller and wrap it in a UINavigationController
UIViewController *controller = [[controllerClass alloc] initWithNibName:nil bundle:nil];
_navigationControler = [[UINavigationController alloc] initWithRootViewController:controller];
_navigationControler.navigationBarHidden = YES;
_navigationControler.view.backgroundColor = [UIColor orangeColor];
// If we are landscape, manually rotate our view
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft )
controller.view.transform = CGAffineTransformMakeRotation( -1.5708 );
else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
controller.view.transform = CGAffineTransformMakeRotation( 1.5708 );
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// Set up the fade-in animation
CATransition *animation = [CATransition animation];
[animation setType:_animationType];
[animation setSubtype:_animationSubtype];
[animation setDuration:_animationDuration];
[animation setTimingFunction:_animationTimingFunction];
[window.layer addAnimation:animation forKey:@"layerAnimation"];
[window addSubview:_navigationControler.view];
[controller release];
}
- (void)hideViewControllerAndRestartUnity
{
// Set up the fade-out animation
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CATransition *animation = [CATransition animation];
[animation setType:_animationType];
[animation setDuration:_animationDuration];
[animation setTimingFunction:_animationTimingFunction];
[animation setDelegate:self];
// Reverse the animationSubtype if we have one
if( _animationSubtype )
[animation setSubtype:[self oppositeAnimationSubtype]];
[window.layer addAnimation:animation forKey:@"layerAnimation"];
_navigationControler.viewControllers = nil;
[_navigationControler.view removeFromSuperview];
[_navigationControler release];
_navigationControler = nil;
}
- (void)pauseUnity:(BOOL)shouldPause
{
if( shouldPause )
UnityPause( true );
else
UnityPause( false );
}
@end