The Application.OpenURL function cannot do this. I've done this before and willing to share. You can make a Java plugin to do this or you can use AndroidJavaClass
and C# to do it. I did mine in C# since this is a simple plugin.
To Open any App:
public bool openUrl(string packageName)
{
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject pManager = unityActivity.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject intent = null;
try
{
intent = pManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", packageName);
unityActivity.Call("startActivity", intent);
return true;
}
catch (Exception e)
{
Debug.LogWarning("Failed to Opeen App: " + e.Message);
//Open with Browser
string link = "https://play.google.com/store/apps/details?id=" + packageName + "&hl=en";
Application.OpenURL(link);
return false;
}
#endif
return false;
}
Usage:
bool success = openUrl("com.google.android.apps.plus");
To Open another App then pass in Url:
public bool openUrl(string packageName, string url)
{
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject pManager = unityActivity.Call<AndroidJavaObject>("getPackageManager");
//For accessing static strings(ACTION_VIEW) from android.content.Intent
AndroidJavaClass intentStaticClass = new AndroidJavaClass("android.content.Intent");
string actionView = intentStaticClass.GetStatic<string>("ACTION_VIEW");
//Create Uri
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", url);
//Psss ACTION_VIEW and Uri.parse to the intent
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionView, uriObject);
try
{
if (pManager.Call<AndroidJavaObject>("getPackageInfo", packageName, 0) != null)
{
intent.Call<AndroidJavaObject>("setPackage", packageName);
}
}
catch (Exception e)
{
Debug.LogWarning("Failed to Open App 1: " + e.Message);
return false;
}
try
{
unityActivity.Call("startActivity", intent);
return true;
}
catch (Exception e)
{
Debug.LogWarning("Failed to Open App 2: " + e.Message);
//Open with Browser
string link = "https://play.google.com/store/apps/details?id=" + packageName + "&hl=en";
Application.OpenURL(link);
return false;
}
#endif
return false;
}
Usage:
bool success = openUrl("com.google.android.apps.plus", "https://plus.google.com/+JewelMash");
You are looking for the second code. If you want to find the package name of another app use the image below:
This solution is for Android. You will need Object-C plugin to do this in iOS. I suggest you ask a new question about how to do this in iOS.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…