Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity.call overrides previous Unity.call #189

Closed
whimsica opened this issue Jun 3, 2017 · 6 comments
Closed

Unity.call overrides previous Unity.call #189

whimsica opened this issue Jun 3, 2017 · 6 comments

Comments

@whimsica
Copy link

whimsica commented Jun 3, 2017

In javascript:

Unity.call("message1");
Unity.call("message2);

only message2 is processed.
How can I get both messages to be processed?

I'm using mac editor and wkwebkit

@KojiNakamaru
Copy link
Member

I've modified the definition of Unity.call() in #149 (comment) . The new definition solves the issue discussed but it has the side effect you described. If the following original definition based on iframe works well for you, please utilize it:

                webViewObject.EvaluateJS(@"
                  window.Unity = {
                    call: function(msg) {
                      var iframe = document.createElement('IFRAME');
                      iframe.setAttribute('src', 'unity:' + msg);
                      document.documentElement.appendChild(iframe);
                      iframe.parentNode.removeChild(iframe);
                      iframe = null;
                    }
                  }
                ");

If you only need to support WKWebView and not UIWebView, you can also utilize the WKWebView's scriptMessageHandler, which is more reliable. Instead of Unity.call() you can call window.webkit.messageHandlers.unityControl.postMessage().

@whimsica
Copy link
Author

whimsica commented Jun 5, 2017

Thanks the iframe method seems to work fine. I only need to support WKWebView. I would be interested in the MessageHandlers to see if there is any speed difference. Is it currently implemented? I tried window.webkit.messageHandlers.unityControl.postMessage()
but window.webkit is undefined.

@KojiNakamaru
Copy link
Member

Yes, it is implemented. Please note that even if you specify enableWKWebView: true it might become undefined because the plugin falls back to UIWebView if the target iOS doesn't support WKWebView.

@whimsica
Copy link
Author

whimsica commented Jun 5, 2017

I'm trying it in the unity editor on a mac osx sierra. Is that why it would be undefined?

If I call
window.webkit.messageHandlers.unityControl.postMessage('someMessage');
do I have to create a function in the sampleWebView named unityControl to receive the string?

@KojiNakamaru
Copy link
Member

Ah, sorry, it is only implemented for the iOS version. It should work on iOS devices.

@whimsica
Copy link
Author

whimsica commented Jun 5, 2017

The webkit method is much faster in my tests. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants