gree / unity-webview

zlib License
2.25k stars 686 forks source link

Close Webview from Unity Webgl build (webview inside another unity project) #769

Open kadirmenz opened 2 years ago

kadirmenz commented 2 years ago

Actually I wanna close webview via this webgl build, window.close() doesnt work so I think one way which is redirect to about:blank and check coming url from webview's onStarted method if it is about:blank should close webview or Is there any possibility to send message from unitywebgl to webview which is inside main unity app, for close webview? if you have any good idea for it I would love to hear.

KojiNakamaru commented 2 years ago

As the javascript function Unity.call() is available, you can define and use MyUnityCall() as below:

mergeInto(LibraryManager.library, {
    MyLoadURL: function(href) {
        window.location.href = Pointer_stringify(href);
    },
    MyUnityCall: function(msg) {
        Unity.call(Pointer_stringify(msg));
    }
});
    [DllImport("__Internal")]
    private static extern void MyUnityCall(string msg);

    public void OnClick()
    {
        MyUnityCall("close");
    }

then, the host app can receive the "close" message with cb: and destroy webViewObject.gameObject.

        webViewObject.Init(
            cb: (msg) =>
            {
                Debug.Log(string.Format("CallFromJS[{0}]", msg));
                if (msg == "close") {
                    Destroy(webViewObject.gameObject);
                }
            },
kadirmenz commented 2 years ago

As the javascript function Unity.call() is available, you can define and use MyUnityCall() as below:

mergeInto(LibraryManager.library, {
    MyLoadURL: function(href) {
        window.location.href = Pointer_stringify(href);
    },
    MyUnityCall: function(msg) {
        Unity.call(Pointer_stringify(msg));
    }
});
    [DllImport("__Internal")]
    private static extern void MyUnityCall(string msg);

    public void OnClick()
    {
        MyUnityCall("close");
    }

then, the host app can receive the "close" message with cb: and destroy webViewObject.gameObject.

        webViewObject.Init(
            cb: (msg) =>
            {
                Debug.Log(string.Format("CallFromJS[{0}]", msg));
                if (msg == "close") {
                    Destroy(webViewObject.gameObject);
                }
            },

Thanks a lot 🙏💕

kadirmenz commented 2 years ago

Sir, It works on mobile phones in webview but when I open my webgl build on pc web browser it says "Unity is not defined". So is there any way to make it like that: if its work in Unity app with webview call Unity.call, if its not call window.close(). I tried that but its not work ```


UnityCall: function(msg) {
    if(Unity!=null)
    {
        Unity.call(Pointer_stringify(msg));
    }else
    {
        window.open('','_self').close();
    }
}
KojiNakamaru commented 2 years ago

You can use the following code to check whether Unity is defined. However, window.open('', '_self').close() does not work for latest browsers.

    UnityCall: function(msg) {
        if (window.Unity != undefined) {
            window.Unity.call(Pointer_stringify(msg));
        } else {
            window.open('', '_self').close();
        }
    }
hZorah commented 1 year ago

Any updates on this? Any way to send messages from webgl build? I am getting Unity is not defined too.

KojiNakamaru commented 1 year ago

@hZorah I've confirmed the sample app works correctly with 2020.3.34f1. As there are several steps to run it correctly, please re-check https://github.com/gree/unity-webview/tree/7d3eb1a7cf5549a8f63cd7a8fa0ef67879e279ba#webgl .

hZorah commented 1 year ago

I was able to do it in 2022.3.5f1. I now have some other problems, but I'll open another issue. Thank you for the reply :)