How do I open a URL in a separate window ?
There is no built-in function to do that. However, you can use Application.ExternalEval() to call some javascript to do that (whether this works or probably depends on the user’s popup blocking used, though).
good to know
could you give a sample javascript to do that
Here a (untested) javascript example that should work:
function Popup()
{
window.open("http://www.mywebsite.com", "mywindow", "location=1, status=1, scrollbars=1, width=640, height=480");
}
The above code must be included in the HTML file.
In Unity use the Application.ExternalCall(“Popup”) function to execute the script.
in the unity default web page where do I insert this ?
It should be in the HTML head element, like this:
<head>
<script type="text/javascript" language="javascript">
function Popup()
{
window.open("http://www.mywebsite.com", "", "location=1,
status=1, scrollbars=1, width=640, height=480");
}
</script>
</head>
It’s not opening a web browser
I’m doing a
if (Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
Application.ExternalCall(javascriptName);
else
Application.OpenURL(url);
The argument for the Application.ExternallCall is a functionName not a javascriptName, are you sure you’ve specified the correct argument? Check: Unity - Scripting API: Application.ExternalCall
Also, make sure the HTML is working correctly, I’ve attached working HTML popup code.
179313–6401–$popup_197.html (247 Bytes)
This is not working for me
Do I put popup.html in the same folder as banyan.html (my scene) or append the content somewhere inside the html ?
Here is my html for the player
<html>
<head>
<title>Unity Web Player - banyan</title>
<script language='VBScript'>
function DetectUnityWebPlayerActiveX
on error resume next
dim tControl, res, ua, re, matches, major
res = 0
set tControl = CreateObject("UnityWebPlayer.UnityWebPlayer.1")
if IsObject(tControl) then
if tControl.GetPluginVersion() = "2.5.0f5" then
' 2.5.0f5 on Vista and later has an auto-update issue
' on Internet Explorer. Detect Vista (6.0 or later)
' and in that case treat it as not installed
ua = Navigator.UserAgent
set re = new RegExp
re.Pattern = "Windows NT (\d+)\."
set matches = re.Execute(ua)
if matches.Count = 1 then
major = CInt(matches(0).SubMatches(0))
if major < 6 then
res = 1
end if
end if
else
res = 1
end if
end if
DetectUnityWebPlayerActiveX = res
end function
</script>
<script language="javascript1.1" type="text/javascript">
function GetUnity () {
if (navigator.appVersion.indexOf("MSIE") != -1 navigator.appVersion.toLowerCase().indexOf("win") != -1)
return document.getElementById("UnityObject");
else if (navigator.appVersion.toLowerCase().indexOf("safari") != -1)
return document.getElementById("UnityObject");
else
return document.getElementById("UnityEmbed");
}
function DetectUnityWebPlayer () {
var tInstalled = false;
if (navigator.appVersion.indexOf("MSIE") != -1 navigator.appVersion.toLowerCase().indexOf("win") != -1) {
tInstalled = DetectUnityWebPlayerActiveX();
}
else {
if (navigator.mimeTypes navigator.mimeTypes["application/vnd.unity"]) {
if (navigator.mimeTypes["application/vnd.unity"].enabledPlugin navigator.plugins navigator.plugins["Unity Player"]) {
tInstalled = true;
}
}
}
return tInstalled;
}
function GetInstallerPath () {
var tDownloadURL = "";
var hasXpi = navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1;
// Use standalone installer
if (1)
{
if (navigator.platform == "MacIntel")
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg";
else if (navigator.platform == "MacPPC")
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg";
else if (navigator.platform.toLowerCase().indexOf("win") != -1)
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe";
return tDownloadURL;
}
// Use XPI installer
else
{
if (navigator.platform == "MacIntel")
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
else if (navigator.platform == "MacPPC")
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
else if (navigator.platform.toLowerCase().indexOf("win") != -1)
tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerWin32.xpi";
return tDownloadURL;
}
}
function AutomaticReload () {
navigator.plugins.refresh();
if (DetectUnityWebPlayer())
window.location.reload();
setTimeout('AutomaticReload()', 500)
}
</script>
</head>
<body>
<center>
<h2>Unity Web Player - banyan</h2>
<script language="javascript1.1" type="text/javaScript">
if (DetectUnityWebPlayer()) {
document.write('<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="600" height="450"> \n');
document.write(' <param name="src" value="banyan.unity3d" /> \n');
document.write(' <embed id="UnityEmbed" src="banyan.unity3d" width="600" height="450" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" /> \n');
document.write('</object>');
}
else {
var installerPath = GetInstallerPath();
if (installerPath != "") {
// Place a link to the right installer depending on the platform we are on. The iframe is very important! Our goals are:
// 1. Don't have to popup new page
// 2. This page still remains active, so our automatic reload script will refresh the page when the plugin is installed
document.write('<div align="center" id="UnityPrompt"> \n');
document.write(' [url=""][img]http://webplayer.unity3d.com/installation/getunity.png[/img][/url] \n');
document.write('</div> \n');
// By default disable ActiveX cab installation, because we can't make a nice Install Now button
// if (navigator.appVersion.indexOf("MSIE") != -1 navigator.appVersion.toLowerCase().indexOf("win") != -1)
if (0)
{
document.write('<div id="InnerUnityPrompt">
Title</p>');
document.write('
Contents</p>');
document.write("</div>");
var innerUnityPrompt = document.getElementById("InnerUnityPrompt");
var innerHtmlDoc =
'<object id="UnityInstallerObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="320" height="50" codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">\n' +
'</object>';
innerUnityPrompt.innerHTML = innerHtmlDoc;
}
document.write('<iframe name="InstallerFrame" height="0" width="0" frameborder="0">\n');
}
else {
document.write('<div align="center" id="UnityPrompt"> \n');
document.write(' [url="javascript: window.open("][img]http://webplayer.unity3d.com/installation/getunity.png[/img][/url] \n');
document.write('</div> \n');
}
AutomaticReload();
}
</script>
<noscript>
<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="600" height="450" codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">
<param name="src" value="banyan.unity3d" />
<embed id="UnityEmbed" src="banyan.unity3d" width="600" height="450" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" />
<noembed>
<div align="center">
This content requires the Unity Web Player
[url="http://www.unity3d.com/unity-web-player-2.x"]Install the Unity Web Player today![/url]
</div>
</noembed>
</object>
</noscript>
<h5>[url="http://unity3d.com"][i]Created with Unity »[/i][/url]</h5>
</center>
</body>
</html>
Looking at the example, the code from popup html needs to be merged with your html file used for the player. Just put the Popup() function into your section. The point of the html file was just to isolate the code, so you can check it works.