Essay - Published: 2020.04.15 | csharp | unity |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
I want a button in my Unity app - coffeedash - to open a URL when a button is clicked. How can I tell Unity to switch over to a url? How can I set that as an action on button click?
Basically we have two different things we're trying to accomplish:
Here's a GameObject Monobehaviour that does both:
public class Example : Monobehaviour {
public Button MyButton;
void Awake() {
MyButton.onClick.AddListener(OnMyButtonClick);
}
void OnMyButtonClick() {
Application.OpenURL("https://hamy.xyz/blog/coffeedash");
}
}
This example adds an onClick listener to our button (which means it will get called on button click), pointing it to the OnMyButtonClick function. Then we have the OnMyButtonClick function call Application.OpenURL which opens a browser to the url of your choice.
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.