-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathRNUnityView.mm
More file actions
233 lines (181 loc) · 6.08 KB
/
RNUnityView.mm
File metadata and controls
233 lines (181 loc) · 6.08 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#import "RNUnityView.h"
#ifdef DEBUG
#include <mach-o/ldsyms.h>
#endif
#ifdef RCT_NEW_ARCH_ENABLED
using namespace facebook::react;
#endif
NSString *bundlePathStr = @"/Frameworks/UnityFramework.framework";
int gArgc = 1;
UnityFramework* UnityFrameworkLoad() {
NSString* bundlePath = nil;
bundlePath = [[NSBundle mainBundle] bundlePath];
bundlePath = [bundlePath stringByAppendingString: bundlePathStr];
NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
if ([bundle isLoaded] == false) [bundle load];
UnityFramework* ufw = [bundle.principalClass getInstance];
if (![ufw appController])
{
#ifdef DEBUG
[ufw setExecuteHeader: &_mh_dylib_header];
#else
[ufw setExecuteHeader: &_mh_execute_header];
#endif
}
[ufw setDataBundleId: [bundle.bundleIdentifier cStringUsingEncoding:NSUTF8StringEncoding]];
return ufw;
}
@implementation RNUnityView
NSDictionary* appLaunchOpts;
static RNUnityView *sharedInstance;
- (bool)unityIsInitialized {
return [self ufw] && [[self ufw] appController];
}
- (void)initUnityModule {
@try {
if([self unityIsInitialized]) {
return;
}
[self setUfw: UnityFrameworkLoad()];
[[self ufw] registerFrameworkListener: self];
unsigned count = (int) [[[NSProcessInfo processInfo] arguments] count];
char **array = (char **)malloc((count + 1) * sizeof(char*));
for (unsigned i = 0; i < count; i++)
{
array[i] = strdup([[[[NSProcessInfo processInfo] arguments] objectAtIndex:i] UTF8String]);
}
array[count] = NULL;
[[self ufw] runEmbeddedWithArgc: gArgc argv: array appLaunchOpts: appLaunchOpts];
[[self ufw] appController].quitHandler = ^(){ NSLog(@"AppController.quitHandler called"); };
[self.ufw.appController.rootView removeFromSuperview];
if (@available(iOS 13.0, *)) {
[[[[self ufw] appController] window] setWindowScene: nil];
} else {
[[[[self ufw] appController] window] setScreen: nil];
}
[[[[self ufw] appController] window] addSubview: self.ufw.appController.rootView];
[[[[self ufw] appController] window] makeKeyAndVisible];
[[[[[[self ufw] appController] window] rootViewController] view] setNeedsLayout];
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
}
@catch (NSException *e) {
NSLog(@"%@",e);
}
}
- (void)layoutSubviews {
[super layoutSubviews];
if(![self unityIsInitialized]) {
[self initUnityModule];
}
if([self unityIsInitialized]) {
self.ufw.appController.rootView.frame = self.bounds;
[self addSubview:self.ufw.appController.rootView];
}
}
- (void)pauseUnity:(BOOL * _Nonnull)pause {
if([self unityIsInitialized]) {
[[self ufw] pause:pause];
}
}
- (void)unloadUnity {
UIWindow * main = [[[UIApplication sharedApplication] delegate] window];
if(main != nil) {
[main makeKeyAndVisible];
if([self unityIsInitialized]) {
[[self ufw] unloadApplication];
}
}
}
- (void)sendMessageToMobileApp:(NSString *)message {
if (self.onUnityMessage) {
NSDictionary* data = @{
@"message": message
};
self.onUnityMessage(data);
}
}
- (void)unityDidUnload:(NSNotification*)notification {
if([self unityIsInitialized]) {
[[self ufw] unregisterFrameworkListener:self];
[self setUfw: nil];
if (self.onPlayerUnload) {
self.onPlayerUnload(nil);
}
}
}
- (void)unityDidQuit:(NSNotification*)notification {
if([self unityIsInitialized]) {
[[self ufw] unregisterFrameworkListener:self];
[self setUfw: nil];
if (self.onPlayerQuit) {
self.onPlayerQuit(nil);
}
}
}
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"onUnityMessage", @"onPlayerUnload", @"onPlayerQuit"];
}
- (void)postMessage:(NSString *)gameObject methodName:(NSString*)methodName message:(NSString*) message {
dispatch_async(dispatch_get_main_queue(), ^{
[[self ufw] sendMessageToGOWithName:[gameObject UTF8String] functionName:[methodName UTF8String] message:[message UTF8String]];
});
}
#ifdef RCT_NEW_ARCH_ENABLED
- (void)prepareForRecycle {
[super prepareForRecycle];
if ([self unityIsInitialized]) {
[[self ufw] unloadApplication];
NSArray *viewsToRemove = self.subviews;
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
[self setUfw:nil];
}
}
+ (ComponentDescriptorProvider)componentDescriptorProvider {
return concreteComponentDescriptorProvider<RNUnityViewComponentDescriptor>();
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNUnityViewProps>();
_props = defaultProps;
self.onUnityMessage = [self](NSDictionary* data) {
if (_eventEmitter != nil) {
auto gridViewEventEmitter = std::static_pointer_cast<RNUnityViewEventEmitter const>(_eventEmitter);
facebook::react::RNUnityViewEventEmitter::OnUnityMessage event = {
.message=[[data valueForKey:@"message"] UTF8String]
};
gridViewEventEmitter->onUnityMessage(event);
}
};
}
return self;
}
- (void)updateEventEmitter:(EventEmitter::Shared const &)eventEmitter {
[super updateEventEmitter:eventEmitter];
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps {
if (![self unityIsInitialized]) {
[self initUnityModule];
}
[super updateProps:props oldProps:oldProps];
}
- (void)handleCommand:(nonnull const NSString *)commandName args:(nonnull const NSArray *)args {
RCTRNUnityViewHandleCommand(self, commandName, args);
}
Class<RCTComponentViewProtocol> RNUnityViewCls(void) {
return RNUnityView.class;
}
#else
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initUnityModule];
}
return self;
}
#endif
@end