Hello!
Does anyone understood how to simply open an url in a new tab (or at least in the same tab)?
I’m looking for a solution by 1 year, and none of the solutions I’ve found on the web seem to work.
This solution
for me, on Chrome, it just download the webpage, and on IE it simply do nothing like with Chrome on mobile.
Let me bump this again as I can’t believe this is not covered in a simple way by Unity.
Any reason there is not an OpenURL equivalent for webgl or at least an example? (the docs fail to mention webgl for Application.OpenURL BTW).
Used to be ExternEval to call JS in page but depreceated - why and what replacement?
I use this little snippet to open URLs, it’s similar to the above but is not treated as a popup because it fires on a user input event. Allows you to open a URL either by key press or mouse press. Of course you’d have to clean it up if you’ve already got events registered for onmouseup or onkeyup.
This is best solution I have come up with as a variant on the above - keep an onmouseup function active but set relevant url on mouse down on a Unity button to have it actually go somewhere (when button released)
The way I understand it (not being a js web dev) is most browsers block opening new URLs without it being in response to a click or mouse event. Combined with Unity’s UI message timing it means you can not just make something open right away, you have to delay it to be in response to something e.g. mouse up after an init on unity mouse down.
If a unity dev could confirm (& perhaps also let us know why no easy solution built in to easily handle such things - opening URLs, file dialogs etc) it would be good!
And in Unity i call the OpenUrl function using Unity Event OnPointerDown on a UI button.
So the onmouseup gets activated on pressing down, and will open the url on mouse up.
Its not perfect because after pressing down on a button, the user can move the mouse around without lifting finger off the mouse, but that’s a nonissue really.
This implementation actually works, but for mobile devices not working when press button nothing happens until you click in somewere of the browser window
That is because in Emscripten the function Pointer_stringify() is deprecated and will be removed in the future. (UTF8ToString() is a drop-in replacement)
@GerDavid For mobile devices, try out if instead of “onmouseup”, one of “ontouchstart” or “ontouchmove” or “ontouchend” events would work better?
Given that people are looking for a workaround, I understand that the Unity function Application.OpenURL() is not currently working for WebGL? If that is the case, we should indeed fix it. Can someone confirm - and raise a bug report?
@jukka_j Application.OpenURL is working just fine on WebGL. But it always opens on the same tab. It would be nice if there was an alternative version to open on new tabs.
Added a task on the board to look into whether the current Application.OpenURL() API would make more sense to always open in a new tab, or whether we should offer a new web-specific Application.OpenURLInNewTab() API for this. (Or maybe a web-specific Application.NavigateToURL() API to navigate the current page, and Application.OpenURL() API would open in a new tab)
@jukka_j Opening on a new tab is what makes sense I think. You always want to go back to the experience. If the user wants to exit the game, he can close the tab himself, but visiting a link should never stop the game.
Thank you for the info! don’t know that about Pointer_stringify(), and for Application.OpenURL() actually works but its deprecated and i supposed will be removed in the future?.
And for mobile devices yes!, i tried it using “ontouchend” and for pc “onmouseup” and works nicely!, if this helps someone else only add an event trigger OnPointerDown and call this function in your UI.Button