- Home /
Answer by Vagonn · Jan 27, 2016 at 06:56 AM
This is now a dead link. Can you summarize the solution?
Use onpointerup for touch input compatibility
mergeInto(LibraryManager.library, {
OpenWindow: function(link) {
var url = Pointer_stringify(link);
document.onpointerup = function() { //Use onpointerup for touch input compatibility
window.open(url);
document.onpointerup = null;
}
},
});
Answer by dppc · Jan 27, 2016 at 05:48 AM
Like this:
Application.ExternalEval("window.open(\"http://www.unity3d.com\")");
See http://docs.unity3d.com/ScriptReference/Application.ExternalEval.html for more info.
Be aware. Open link in new tab is blocked by chrome.
Application.ExternalEval
is deprecated check my answer: http://answers.unity.com/answers/1817839/view.html
Answer by Teadaddy · Jan 26, 2016 at 11:06 PM
this has been needed for some time apparently, I am looking for some sort of workaround as well - please vote for this topic to be addressed by Unity, now that webGL is working, this needs to be implemented.
http://answers.unity3d.com/questions/32531/applicationopenurl-on-webplayer-to-open-in-a-new-t.html
Here is the correct answer to this question, posted beneath by Vagonn
http://va.lent.in/opening-links-in-a-unity-webgl-project/
Thanks! works like a charm
Answer by d2clon · Feb 26, 2021 at 08:42 AM
Application.ExternalEval is deprecated, the new way is using a .jslib file with description of the js functions you want to use in your C# code:
For our case we need:
// Assets/Pluging/JSUtils.jslib
mergeInto(LibraryManager.library, {
OpenURLInExternalWindow: function (url) {
window.open(Pointer_stringify(url), "_blank");
}
});
And:
# Scripts/MyControler.cs
using UnityEngine;
using System.Runtime.InteropServices;
public class MyController : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void OpenURLInExternalWindow(string url);
public void OpenMyUrl()
{
OpenURLInExternalWindow("https://myurl.com");
}
}
Answer by GilesDMiddleton · Apr 22, 2017 at 05:56 PM
dppc was nearly right. The actual answer to open in a new window is to call with a 2nd parameter, replacing _blank with the name of the window you want to repeatedly target (or keep it as _blank for a new window each time).
Application.ExternalEval("window.open(\"http://www.unity3d.com\",\"_blank\")");
However, you'll need to call something different if you want to open a URL when not in WebGL. Also, 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" );
}
}