- Home /
OnClick open URL
I am using C# and I would like my button to open a URL once it is clicked. How do I write this?
using UnityEngine; using System.Collections;
public class OGSButton : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Application.OpenURL("http://unity3d.com/");
}
}
Answer by GAZEREAPER · Dec 24, 2015 at 09:05 AM
Is it a unity UI button? If so you have to make a public void in your script, which opens the url, just like you did above. If you want to activate the void on a button press you have to: 1. go to the button. 2. scroll to the bottom. 3. where you see the onclick stuff, add the object that contains the object with your OGSButton and select the void that needs to be called.
Detailed tutorial here: Unity - UI Button
Hello,
Thank you for your reply. I was ably to find a video that explained it after a while of searching. For those who are having the same issue...
On the script as mentioned above,
void yourFunction () { Application.OpenURL("http://unity3d.com/"); }
$$anonymous$$ake it it's own function.
I attached the script to the parent Canvas, the on the button scroll to the On Click () component and click + and pull the Canvas to the item slot under Runtime. Then click the "No Function" list menu, select yourScript from the list and then select yourFunction.
Hope others who have the same issue were able to solve it as well!