asmadsen / react-native-unity-view

MIT License
204 stars 128 forks source link

onUnityMessage not working #19

Open bilal-arikan opened 4 years ago

bilal-arikan commented 4 years ago

Hi, I recently encountered this error,

reason of the problem At RNUnityViewManager.m,

RCT_EXPORT_MODULE (RNUnityView)
- (UIView *) view (
    self.currentView = [[RNUnityView alloc] init];
    if ([UnityUtils isUnityReady]) {
        [self.currentView setUnityView: [GetAppController () unityView]];
    } else {
        [UnityUtils createPlayer: ^ {
            [self.currentView setUnityView: [GetAppController () unityView]];
        }];
        (GetAppController () setUnityMessageHandler: ^ (const char * message) (
            [_bridge.eventDispatcher sendDeviceEventWithName: @ "onUnityMessage"
                                                            body: [NSString stringWithUTF8String: message]];
        }];
    }
    return self.currentView;
}

Doesn't run else section.

on UnityView.mm

- (void) createPlayer: (void (^) (void)) completed (
    if (_isUnityReady) (
        completed ();
        return;
    }
    [[NSNotificationCenter defaultCenter] addObserverForName: @ "UnityReady" object: nil queue: [NSOperationQueue mainQueue] usingBlock: ^ (NSNotification * _Nonnull note) {
        _isUnityReady = YES;
        completed ();
    }];
    if (UnityIsInited ()) (
        return;
    } ......

been calling _isUnityReady = YES before View method in this file

Currently for those who may encounter this error, If you create <UnityView> before call UnityModule.createUnity(), onUnityMessage works without any problems

klokie commented 3 years ago

hi @bilal-arikan, would you be willing to expand on your solution? I'm currently unable to get Xcode to compile the project due to this error which seems to be related:

/tmp/myproject/node_modules/@asmadsen/react-native-unity-view/ios/RNUnityViewManager.m:19:29: error: no visible @interface for 'UnityAppController' declares the selector 'setUnityMessageHandler:'
        [GetAppController() setUnityMessageHandler: ^(const char* message) {
         ~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~

Any suggestions would be much appreciated!

shhhiiiii commented 3 years ago

Hi @bilal-arikan, may i ask if your messaging is working after the unity is unload and loaded again??

Im also having a problem with the MessageHandler from react native to unity.

The scenario is : First open of unity view is perfectly working ( Message Handler for both side) then I used Application.Unload(); to unload unity and navigate to other react native screen, When navigate again to unity view, Unity will reload just fine and will work properly,

But the problem is that messaging is distorted. Unity still can send message and will be receive in react native side, But for react native part when attempt to send message to unity view. Unity will not be able to receive the message.

Im suspecting that react native still trying to send message from the previous view that has been unload.

Does anyone know how to fix this problem?

HELP PLEASE!! Thanks!