Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/unity/Assets/Plugins/iOS/NativeCallProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ __attribute__ ((visibility("default")))
@interface FrameworkLibAPI : NSObject

+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi;
+(void) unregisterAPIforNativeCalls;

@end
9 changes: 8 additions & 1 deletion example/unity/Assets/Plugins/iOS/NativeCallProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi
api = aApi;
}

+(void) unregisterAPIforNativeCalls
{
api = NULL;
}

@end

extern "C"
{
void sendMessageToMobileApp(const char* message)
{
return [api sendMessageToMobileApp:[NSString stringWithUTF8String:message]];
if (api != NULL) {
[api sendMessageToMobileApp:[NSString stringWithUTF8String:message]];
}
}
}
4 changes: 2 additions & 2 deletions ios/RNUnityView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy) RCTBubblingEventBlock _Nullable onPlayerQuit;

- (void)unloadUnity;
- (void)pauseUnity:(BOOL * _Nonnull)pause;
- (void)pauseUnity:(BOOL)pause;
- (void)postMessage:(NSString* _Nonnull )gameObject methodName:(NSString* _Nonnull)methodName message:(NSString* _Nonnull) message;

@end
Expand All @@ -54,7 +54,7 @@ NS_ASSUME_NONNULL_END
@property (nonatomic, copy) RCTBubblingEventBlock _Nullable onPlayerQuit;

- (void)unloadUnity;
- (void)pauseUnity:(BOOL * _Nonnull)pause;
- (void)pauseUnity:(BOOL)pause;
- (void)postMessage:(NSString* _Nonnull )gameObject methodName:(NSString* _Nonnull)methodName message:(NSString* _Nonnull) message;


Expand Down
59 changes: 44 additions & 15 deletions ios/RNUnityView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ - (void)initUnityModule {
array[count] = NULL;

[[self ufw] runEmbeddedWithArgc: gArgc argv: array appLaunchOpts: appLaunchOpts];

// Free the strdup'd strings and the malloc'd array
for (unsigned i = 0; i < count; i++)
{
free(array[i]);
}
free(array);

[[self ufw] appController].quitHandler = ^(){ NSLog(@"AppController.quitHandler called"); };
[self.ufw.appController.rootView removeFromSuperview];

Expand Down Expand Up @@ -90,7 +98,7 @@ - (void)layoutSubviews {
}
}

- (void)pauseUnity:(BOOL * _Nonnull)pause {
- (void)pauseUnity:(BOOL)pause {
if([self unityIsInitialized]) {
[[self ufw] pause:pause];
}
Expand All @@ -102,6 +110,7 @@ - (void)unloadUnity {
[main makeKeyAndVisible];

if([self unityIsInitialized]) {
[NSClassFromString(@"FrameworkLibAPI") unregisterAPIforNativeCalls];
[[self ufw] unloadApplication];
}
}
Expand All @@ -123,7 +132,7 @@ - (void)unityDidUnload:(NSNotification*)notification {
[self setUfw: nil];

if (self.onPlayerUnload) {
self.onPlayerUnload(nil);
self.onPlayerUnload(@{@"message": @"unloaded"});
}
}
}
Expand All @@ -134,7 +143,7 @@ - (void)unityDidQuit:(NSNotification*)notification {
[self setUfw: nil];

if (self.onPlayerQuit) {
self.onPlayerQuit(nil);
self.onPlayerQuit(@{@"message": @"quit"});
}
}
}
Expand All @@ -157,15 +166,11 @@ - (void)postMessage:(NSString *)gameObject methodName:(NSString*)methodName mess
- (void)prepareForRecycle {
[super prepareForRecycle];

if ([self unityIsInitialized]) {
[[self ufw] unloadApplication];

NSArray *viewsToRemove = self.subviews;
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}

[self setUfw:nil];
// Only detach the Unity view from this component — do NOT unload the
// singleton Unity runtime, as that would break it for any future views.
NSArray *viewsToRemove = self.subviews;
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
}

Expand All @@ -178,15 +183,39 @@ - (instancetype)initWithFrame:(CGRect)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);
__weak RNUnityView *weakSelf = self;
self.onUnityMessage = ^(NSDictionary* data) {
RNUnityView *strongSelf = weakSelf;
if (strongSelf && strongSelf->_eventEmitter != nil) {
auto gridViewEventEmitter = std::static_pointer_cast<RNUnityViewEventEmitter const>(strongSelf->_eventEmitter);
facebook::react::RNUnityViewEventEmitter::OnUnityMessage event = {
.message=[[data valueForKey:@"message"] UTF8String]
};
gridViewEventEmitter->onUnityMessage(event);
}
};

self.onPlayerUnload = ^(NSDictionary* data) {
RNUnityView *strongSelf = weakSelf;
if (strongSelf && strongSelf->_eventEmitter != nil) {
auto eventEmitter = std::static_pointer_cast<RNUnityViewEventEmitter const>(strongSelf->_eventEmitter);
facebook::react::RNUnityViewEventEmitter::OnPlayerUnload event = {
.message=[[data valueForKey:@"message"] UTF8String]
};
eventEmitter->onPlayerUnload(event);
}
};

self.onPlayerQuit = ^(NSDictionary* data) {
RNUnityView *strongSelf = weakSelf;
if (strongSelf && strongSelf->_eventEmitter != nil) {
auto eventEmitter = std::static_pointer_cast<RNUnityViewEventEmitter const>(strongSelf->_eventEmitter);
facebook::react::RNUnityViewEventEmitter::OnPlayerQuit event = {
.message=[[data valueForKey:@"message"] UTF8String]
};
eventEmitter->onPlayerQuit(event);
}
};
}

return self;
Expand Down
14 changes: 6 additions & 8 deletions ios/RNUnityViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ @implementation RNUnityViewManager
RCT_EXPORT_VIEW_PROPERTY(onPlayerUnload, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPlayerQuit, RCTBubblingEventBlock)

RNUnityView *unity;

- (UIView *)view {
unity = [[RNUnityView alloc] init];
RNUnityView *unity = [[RNUnityView alloc] init];
UIWindow * main = [[[UIApplication sharedApplication] delegate] window];

if(main != nil) {
Expand All @@ -41,18 +39,18 @@ + (BOOL)requiresMainQueueSetup {
RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
return;
}
[unity postMessage:(NSString *)gameObject methodName:(NSString *)methodName message:(NSString *)message];
[view postMessage:(NSString *)gameObject methodName:(NSString *)methodName message:(NSString *)message];
}];
}

RCT_EXPORT_METHOD(pauseUnity:(nonnull NSNumber*) reactTag pause:(BOOL * _Nonnull)pause) {
RCT_EXPORT_METHOD(pauseUnity:(nonnull NSNumber*) reactTag pause:(BOOL)pause) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
RNUnityView *view = (RNUnityView*) viewRegistry[reactTag];
if (!view || ![view isKindOfClass:[RNUnityView class]]) {
RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
return;
}
[unity pauseUnity:(BOOL * _Nonnull)pause];
[view pauseUnity:pause];
}];
}

Expand All @@ -63,7 +61,7 @@ + (BOOL)requiresMainQueueSetup {
RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
return;
}
[unity pauseUnity:(BOOL * _Nonnull)false];
[view pauseUnity:NO];
}];
}

Expand All @@ -74,7 +72,7 @@ + (BOOL)requiresMainQueueSetup {
RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
return;
}
[unity unloadUnity];
[view unloadUnity];
}];
}

Expand Down
2 changes: 1 addition & 1 deletion src/UnityView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class UnityView extends React.Component<RNUnityViewProps> {
}

componentWillUnmount() {
if (this.ref.current) {
if (this.ref.current && !this.props.androidKeepPlayerMounted) {
Commands.unloadUnity(this.ref.current);
}
}
Expand Down
1 change: 1 addition & 0 deletions unity/Assets/Plugins/iOS/NativeCallProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ __attribute__ ((visibility("default")))
@interface FrameworkLibAPI : NSObject

+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi;
+(void) unregisterAPIforNativeCalls;

@end
9 changes: 8 additions & 1 deletion unity/Assets/Plugins/iOS/NativeCallProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi
api = aApi;
}

+(void) unregisterAPIforNativeCalls
{
api = NULL;
}

@end

extern "C"
{
void sendMessageToMobileApp(const char* message)
{
return [api sendMessageToMobileApp:[NSString stringWithUTF8String:message]];
if (api != NULL) {
[api sendMessageToMobileApp:[NSString stringWithUTF8String:message]];
}
}
}