Once you’ve got passed the popup blocker (if that’s the issue)- be aware of the 2nd parameter which allows you to open in a new, or named window, so you don’t open many windows for the same content.
Replacing _blank with the name of the window you want to repeatedly target (or keep it as _blank for a new window each time).
Note:you’ll need to call something different if you want to open a URL when not in WebGL.
And, if you’re trying to open social links things get more complicated, as you might want to call the native version.
public void OpenTwitter()
{
if( Application.platform==RuntimePlatform.WebGLPlayer )
{
Application.ExternalEval("window.open(\"https://www.twitter.com/GilesDMiddleton\",\"_blank\")");
return;
}
float startTime = Time.timeSinceLevelLoad;
//open the twitter app
Application.OpenURL( "twitter:///user?screen_name=GilesDMiddleton" );
if (Time.timeSinceLevelLoad - startTime <= 1f)
{
// typically a user takes a second to open twitter and decide what to do
// so if the function took less than a second, it's likely it failed
// try using normal URL opening
Application.OpenURL( "https://www.twitter.com/GilesDMiddleton" );
}
}
We are changing the behavior of the existing Application.OpenURL() function to work just like requested, i.e. to always open up the page in a new tab. This matches the expected behavior better, as navigating the current tab away does not seem to be desired practically ever. This should appear in a point release in the near future.
Just use javascript, no issue with popup blockers or anything… add a new file to Plugins folder, for this example let’s say it’s WebGL.jslib
mergeInto(LibraryManager.library, {
OpenInNewTab: function (url) {
window.open(Pointer_stringify(url));
},
OpenInSameTab: function (url) {
window.location = Pointer_stringify(url);
// or window.location.replace(Pointer_stringify(url));
}
});
Then create a class somewhere like this…
using System.Runtime.InteropServices;
public class JavaScript
{
[DllImport("__Internal")]
public static extern void OpenInNewTab(string url);
[DllImport("__Internal")]
public static extern void OpenInSameTab(string url);
};
And then to use it, you can do this (let’s say from a button being clicked),
In the above implementation, if the build is for WebGL, it’ll use JavaScript to open the link. For any other platforms, it’ll use Application.OpenURL.
Edit: I personally haven’t had any issue with popup blockers - though it’s not much different than OP approach aside from being via jslib, so it’s possible there still would be.
Edit 2: @jukka_j I’ll be switching to Application.OpenURL - that’s what I get for skimming before posting
@LilGames I made a free plugin that can deal with this. I didn’t test on Unity 2018.x, but it should work. It’s the same idea from what was posted above ^.
Hello! +1
I’ve managed to solve the issue with @Marks4 asset. However there is another bug: if you close the new tab and switch to the webgl app nothing is clickable anymore. But returning back by the left arrow from the new tab back to webgl works fine.