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

Question about javascript bridge differences on osx and ios #199

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

Question about javascript bridge differences on osx and ios #199

whimsica opened this issue Jun 16, 2017 · 6 comments

Comments

@whimsica
Copy link

I'm sending some information back and forth using the javascript bridge. On osx the information is sent back and forth immediately. For example if I send a command to javascript and a return command during FixedUpdate it is sent back before FixedUpdate finishes. Same with Update and Late Update.

On IOS if I send a command to javascript during FixedUpdate it doesn't return until fixed,update,late have occured, or a frame later.

Why is there a difference? It doesn't seem related to the framerate.
Is there any way to get the javascript bridge to return immediately on ios rather than a frame later?

@KojiNakamaru
Copy link
Member

The bridge is based on location or iframe:

// NOTE: depending on the situation, you might prefer
// the 'iframe' approach.
// cf. https://github.com/gree/unity-webview/issues/189
#if true
webViewObject.EvaluateJS(@"
window.Unity = {
call: function(msg) {
window.location = 'unity:' + msg;
}
}
");
#else
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;
}
}
");
#endif

Especially the location approach may cause a delay, but even the iframe approach still have some delay. If you only need to support WKWebView, you can utilize window.webkit.messageHandlers.unityControl.postMessage() which is much efficient (cf. #189).

After the Objective-C code receiving a message from JavaScript, however, it utilizes UnitySendMessage() to send a event to C#. It is safe but asynchronous so may cause another delay. If you also need to avoid this delay, you could try MonoPInvokeCallback:

https://forum.unity3d.com/threads/monopinvokecallback-in-unity.132510/

@whimsica
Copy link
Author

Thank you very much for the information about what might cause delays. I tried the wkwebview version also and it had the same issues. I will do some experiments with location vs iframe and UnitySendMessage vs MonoPInvokeCallback.

@whimsica
Copy link
Author

I built a test project to see the problems more clearly. It fires off calls to javascript functions that call unity back during FixedUpdate, Update, and LateUpdate and documents when they return.

I tried getting rid of the iframe methods, and also using setTimeouts to delay the iframes. I also did some more experiments with wkwebview. In the experiments the return calls from javascript always seem to happen after LateUpdate no matter when they are called. Also they return at random times after LateUpdate. According to documentation on UnitySendMessage it has a one frame delay so this may be the problem. I also downloaded a few more projects for unity including http://uniwebview.onevcat.com/ and it has the same frame delays. If it's possible to fix it would be very powerful to get immediate synchronous returns from javascript. I've been reading up on MonoPInvokeCallback, but unfortunately it's beyond my expertise to put together an example to see if it solves the problem. If you have time could you provide some example code that I could try with my test system to see if it's possible to solve the problem? Thank you.

@KojiNakamaru
Copy link
Member

I've implemented CallMethod based on MonoPInvokeCallback, so please check it:
https://github.com/gree/unity-webview/tree/experimental/monopinvokecallback
Please note it is experimental and works only for osx and ios.

@whimsica
Copy link
Author

Great. I'll run some experiments and get back to you.

@whimsica
Copy link
Author

Success! There are now no frame delays. I am sending EvaluateJS with return calls consecutively in FixedUpdate, Update, and LateUpdate. The returns are immediate and everything stays in sync.

@whimsica whimsica closed this as completed Jul 3, 2017
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