open URL/using *

ok guys I want to open a url (and yes it is The Unity Gallery for Everyone Related)

this is what I got

import java.net.*;
import java.io.*;

function OnGUI {
    if (GUILayout.Button("url"))
    {
         URL url = new URL("http://www.google.com/");
         InputStream in = url.openStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
     }
}

the error I get is

how do I make this work, this is supposed to open a link in a webbrowser

You are trying to use java libraries. Unity does not use Java. (You might be confusing javascript with java, but there is no relation between those two languages.)

Have you tried taking a look at the WWW class in the Unity script reference?

yes I have but hat I want is to open a tab in the browser or window and go to that url

[url="http://www.google.com"]Google[/url]

that is an html link I want a javascript link but this doesn’t work

window.open="http://www.google.com";

I get an error saying that it doesn’t know what window means when it turns the same color as OnGUI

The Unity plugin can tell the web browser to run some code by using Application.ExternalEval. Something like this should work:

Application.ExternalEval("window.open('http://www.google.com'); ");

… but will probably be blocked by a popup blocker.

If you are using the standalone player, or if it’s okay to open the link in the same window, you can use Application.OpenURL like this:

Application.OpenURL("http://www.google.com");

Here’s the function you probably want. Use it to tell the browser to open a new window, or redirect the current page.

thank you this is perfect
!
!
!
!
:lol:

ok here is my finished code that works for any one else who wants to open windows with-in unity (from)

in Unity

function OnGUI () {
if (GUILayout.Button("Google") {
Application.ExternalCall ("URLOpen", "http://www.google.com");
}
}

in html file

after body tag

<script language="JavaScript" type="text/javascript">
<!--
function URLOpen( arg )
{
winRef = window.open( arg );
}
-->
</script>

works perfectly! also isn’t blocked by pop-up blocker in ie6-7 safari firefox opera

How did you manage to “trick” the popup-blocker in FireFox? In IE it works great, but if I try to do that in FireFox, the pop-blocker will catch it. I’m currently on FF 3.0.6.

I didn’t it worked in my firefox with pop-up blocker on.

also you could try this

in stead of winRef…

window.location.href = arg;

that also works, it is a javascript redirect

I atleast think it does…

it should :slight_smile:

Hm, I’m apparently not so lucky :frowning: The “window.location.href = arg;” is a plain redirect and will take over the current frame/window/tab, right (correct me if I’m wrong, my javascript knowledge is very limited)? I sort of need the popup, unfortunately.

Thing is, in FireFox, it appears to me that the window.open works and avoids the popup-blocker if it is called as a result of an onclick-event in the page, but if it is called as a result from a Unity callback the popup-blocker will catch it (it is probably trying to be smart thinking that it is some automated way of opening windows).

You don’t happen to have a web-build of your application online? I’m just curious to see if the popup-blocker catches it on my computer :slight_smile:

then do this have the user click on a button in game to g to the website and that might fix it, and no I don’t I did this as a test a while back but haven’t used it.