Custom Chrome Tab In Unity – How To Open Url In Custom Chrome Tab In Unity

Want to open a custom chrome tab in your unity application. Try this code. But remember that this code only work on Android device.

using UnityEngine;

public class CustomChromeTab : MonoBehaviour
{
public string url;

public void OpenChromeTab()
{
// Check if Chrome is installed on the device.
if (Application.platform == RuntimePlatform.Android)
{
AndroidJavaObject packageManager = new AndroidJavaObject("com.unity3d.player.UnityPlayer").GetStatic("currentActivity").Call("getPackageManager");
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", "android.intent.action.VIEW", AndroidJavaObject.CallStatic("parseUri", "http://" + url, 0), packageManager.GetStatic("MATCH_DEFAULT_ONLY"));
intent.Call("setPackage", "com.android.chrome");
AndroidJavaObject chooser = AndroidJavaObject.CallStatic("createChooser", intent, "Open in Chrome");
AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
currentActivity.Call("startActivity", chooser);
}
else
{
Application.OpenURL("http://" + url);
}
}
}

 

Now attach the script to the game object which is on scene. Again try below code to open a url in chrome tab.

CustomChromeTab chromeTab = GetComponent<CustomChromeTab>();
chromeTab.url = "w3teacher.net";
chromeTab.OpenChromeTab();

 

We hope you will like this script. Chrome tab have many benefits in applications. In unity it is difficult to find the code for custom chrome tab. You can open a url inside your game application.

Leave a Reply

Your email address will not be published. Required fields are marked *