Search Unity

Open url

Discussion in 'Web' started by danielesuppo, Jul 4, 2017.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    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
    http://va.lent.in/opening-links-in-a-unity-webgl-project/
    for me, on Chrome, it just download the webpage, and on IE it simply do nothing like with Chrome on mobile.

    Any help and tip is much appreciated!
    Thank-you!

    Daniele
     
  2. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    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?
     
  3. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    c# (in your code)
    Code (CSharp):
    1.     [DllImport("__Internal")]
    2.     public static extern void OpenMyUrl(string str);
    3. //....
    4. void myvoid()
    5. {
    6.     OpenMyUrl("google.com");
    7. }
    8.  
    OpenMyUrl.jslib: (in Assets/Plugins folder)
    Code (JavaScript):
    1. var OpenMyUrlPlugin = {
    2.   OpenMyUrl: function(str) {
    3.       var url = Pointer_stringify(str);
    4.       window.open(url, '_blank');
    5.   }
    6. };
    7. mergeInto(LibraryManager.library, OpenMyUrlPlugin);
     
  4. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    Yes it should be do-able like that but if you do that in response to a UI click (even pointer up) the browser just thinks you created a popup & blocks
     
  5. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    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.

    Code (csharp):
    1.     OpenUrl: function(urlPtr)
    2.     {
    3.         var url = Pointer_stringify(urlPtr);
    4.         document.onmouseup = function()
    5.         {
    6.             window.open(url, "_blank", "");
    7.             document.onkeyup = null;
    8.             document.onmouseup = null;
    9.         }
    10.         document.onkeyup = function()
    11.         {
    12.             window.open(url, "_blank", "");
    13.             document.onkeyup = null;
    14.             document.onmouseup = null;
    15.         }      
    16.     },
     
  6. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    Well that works - but only after second click if I call it on unity button click, so still not quite there for me!
     
  7. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    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)

    Code (JavaScript):
    1. var URLPlugin = {
    2.    
    3.     InitURLOpening: function() {
    4.         url = "none";
    5.         document.onmouseup = function()
    6.         {
    7.             if (url != "none") window.open(url, "_blank", "");
    8.             url = "none";
    9.         }
    10.     },
    11.  
    12.     SetOpenURL: function(str)
    13.     {
    14.         url = Pointer_stringify(str);
    15.     },
    16.    
    17. };
    18. mergeInto(LibraryManager.library, URLPlugin);
     
  8. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
    Why set URL on mouse down? why not call OpenURL on mouse down with url inside parameter?
    On mouse up it will run anyway correct
     
  9. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    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!
     
  10. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
    I implemented
    Code (CSharp):
    1. var OpenMyUrlPlugin = {
    2.    OpenUrl: function(urlPtr)
    3.     {
    4.         var url = Pointer_stringify(urlPtr);
    5.         document.onmouseup = function()
    6.         {
    7.             window.open(url, "_blank", "");
    8.             document.onmouseup = null;
    9.         }
    10.     },
    11. };
    12. mergeInto(LibraryManager.library, OpenMyUrlPlugin);
    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.
     
    GerDavid and andyz like this.
  11. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    yes my js was not the best - that's the way to do it
     
    luniac likes this.
  12. GerDavid

    GerDavid

    Joined:
    Aug 15, 2019
    Posts:
    6
    This implementation actually works, but for mobile devices not working when press button nothing happens until you click in somewere of the browser window
     
  13. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Instead of the above code, please use the following code:

    Code (JavaScript):
    1. var OpenMyUrlPlugin = {
    2.    OpenUrl: function(urlPtr)
    3.     {
    4.         var url = UTF8ToString(urlPtr);
    5.         document.onmouseup = function()
    6.         {
    7.             window.open(url, "_blank", "");
    8.             document.onmouseup = null;
    9.         }
    10.     },
    11. };
    12. mergeInto(LibraryManager.library, OpenMyUrlPlugin);
    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?
     
    GerDavid likes this.
  14. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
  15. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    Code (JavaScript):
    1. window.open(url);
    without parameters also open a new tab btw.
     
  16. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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)
     
    ROBYER1 likes this.
  17. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    @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.
     
    sumpfkraut likes this.
  18. GerDavid

    GerDavid

    Joined:
    Aug 15, 2019
    Posts:
    6
    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

    Code (JavaScript):
    1. var OpenWindowPlugin = {
    2.   openWindow: function(link)
    3.   {
    4.       var url = UTF8ToString(link);
    5.       document.onmouseup = function()
    6.       {
    7.           window.open(url);
    8.           document.onmouseup = null;
    9.       }
    10.       var url = UTF8ToString(link);
    11.       document.ontouchend = function()
    12.       {
    13.           window.open(url);
    14.           document.ontouchend = null;
    15.       }
    16.   }
    17. };
    18. mergeInto(LibraryManager.library, OpenWindowPlugin);
     
  19. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    This would make sense to us too as it's a good use case for WebGL and the current workaround feels hacky

    For a while I was using this code in a file called OpenURL.jslib in Plugins > WebGL marked for WebGL and it worked fine for a while:

    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2. OpenURLInTab: function (url) {
    3. window.open(url, '_blank').focus();
    4. }
    5. });
    Until one day, it randomly stopped working, perhaps a Unity bug that it even worked in the first place.
    So now we are using:

    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2. OpenURLInTab: function (url) {
    3. var fullUrl = Pointer_stringify(url);
    4. console.log(fullUrl);
    5. window.open(fullUrl, '_blank').focus();
    6. }
    7. });
     
  20. TioTizo

    TioTizo

    Joined:
    Feb 27, 2021
    Posts:
    1