Search code examples
unity-game-engineopenurl

Little script for Unity3D to open URL in a default external browser


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.


Solution

  • 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);
        }
    }