2

my question is for Unity3D developers.

I have a project in Unity almost finished, now i need to setup some objects to open URL (www) links to a web browser. I need a script that i can attach to any gameobject to open any URL.

I really appreciate your help. Will be nice if this script can work in all platforms. thanks.

rebit
  • 33
  • 2
  • 5
  • Can you provide more specific details? For example, can you provide code that you have already tried, with the minimum code you need to express your problem? – M. K. Hunter Dec 07 '14 at 03:04
  • Hi M.K., i didnt tried any code, i just want to click in a Cube for example and open a URL in a default operating system browser. – rebit Dec 07 '14 at 03:08

1 Answers1

2

Use Application.OpenUrl I believe it works on all devices even though it only mentions editor and PC in the description. Make sure your url has http:// in it or it wont work. A common mistake.

http://docs.unity3d.com/ScriptReference/Application.OpenURL.html

If you want to be able to type the url in in unity you can make this small modification, this also does it on click on a game object like you wanted.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

    public string url;
    void OnMouseOver() {
        if (Input.GetMouseButtonDown(0) ||  if(Input.touchCount >  0))
            Application.OpenURL(url);
    }
}
marsh
  • 2,592
  • 5
  • 29
  • 53
  • Its just create a script.cs, put this code inside and attach to a object ? – rebit Dec 07 '14 at 02:58
  • The unity says: "can´t add component because its doenst exists" – rebit Dec 07 '14 at 03:01
  • Yes exactly then when that object is created the page will load. You can modify it to do it at other times but you will have to know at least a bit of coding knowledge. It is impossible to post all the code for you without having a bit of knowledge about how you want it setup. I doubt many users would be able to do that anyway, – marsh Dec 07 '14 at 03:01
  • Remove .cs from your file name it does that automatically. Also make sure the name of your script is the same as the class in the script. In this case ExampleClass. – marsh Dec 07 '14 at 03:02
  • If i remove the extension the script cannot be attached to a gameobject. I think its simple but i dont know Coding. I just need open a simple url when click in a object. The script can show the option to write the URL in a field on Unity Editor. – rebit Dec 07 '14 at 03:07
  • Can you share in some cloud storage for me ? Btw Thanks your help. – rebit Dec 07 '14 at 03:07
  • Wow, its working , but when i hit play the URL automatic load, there a way to only open when i click ? – rebit Dec 07 '14 at 03:14
  • I think is missing a behavior like, onmouse hover or something like that ?? no ? – rebit Dec 07 '14 at 03:15
  • Yes i saw, but as i said, when i hit PLAY the URL is automatic opened, i need when i click on the object. I attached the script to the object with sucessfull. This is almost i need ! :D – rebit Dec 07 '14 at 03:23
  • Thank you i saw now ! on mousedown, i will test. – rebit Dec 07 '14 at 03:24
  • /Scripts/openurl.cs(9,23): error CS1525: Unexpected symbol `Application' – rebit Dec 07 '14 at 03:27
  • openurl.cs(8,18): error CS0117: `UnityEngine.Input' does not contain a definition for `GetMouseDown' – rebit Dec 07 '14 at 03:30
  • Try now. I think the method name was a bit off. – marsh Dec 07 '14 at 03:33
  • Yes !!! Working, now i need to test on Android too. I think will not work because android = Touchscreen (no mouse input)... – rebit Dec 07 '14 at 03:35
  • On android and iOS use if(Input.touchCount > 1) instead of Input.GetMouseButtonDown(). Please checkmark the answer if it helped you. – marsh Dec 07 '14 at 03:40
  • You press the up box if you liked the answer. Then the green checkmark below it if it answered your question. – marsh Dec 07 '14 at 03:44
  • Thanks! :) Good luck on your project. – marsh Dec 07 '14 at 03:44