S: ButtonOpenURL, Setup Buttons to Open an URL from Unity

With this easy to use ButtonOpenURL Script you can define quickly Butttons that lead to an URL inside the World Wide Web

Drag the ButtonOpenURL Script on a Unity UI Button, define the Url to Open and the Script will automatic attach a onClick Event that performs the Application.OpenURL Action to Open a Unity URL

ButtonOpenURL.cs

				
					using UnityEngine;

/// <summary>
/// Attached on a Unity UI Button the Click on it with Call the URL to Open
/// </summary>
public class ButtonOpenURL : MonoBehaviour
{
    [SerializeField] string urlToOpen = "https://www.stussegames.com";

    void OnEnable() =>
        GetComponent<UnityEngine.UI.Button>().onClick.AddListener(OpenURL);

    void OpenURL() => 
        Application.OpenURL(urlToOpen);

    void OnDisable() => 
        GetComponent<UnityEngine.UI.Button>().onClick.RemoveListener(OpenURL);
}
				
			

The ButtonOpenURL Script  can be also changed and a Button variable can be attached and assigned inside the Inspector to remove the GetComponent Part  from t he OnEnable and OnDisable Function.

Its also Possible to make the OpenURL Method Public and call it  from another Script  or  Assign  it inside the Unity Inspector  on the Button onClick Events.

Happy Development <3

Leave a Reply

Your email address will not be published.