- Home /
How to load android app when the button clicked in unity scene?
I have created android app and unity game. So I was able to load unity game from android app using button click event. And the next step is I need to come back to the android app (Specific activity) if the user want. How can I launch the android activity when the user press button. This will be a great help for me, I am tired with this problem. I am trying to solve this within 7 days but I unable to solve it.
Answer by Vandarthul · May 18, 2017 at 12:38 PM
Hey @ishafdo4
Try this:
public void LaunchApp(string package, string storeLink = null)
{
bool fail = false;
string bundleId = package; // your target bundle id
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
try
{
launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
}
catch (System.Exception e)
{
fail = true;
}
if (fail && storeLink != null)
{ //open app in store
Application.OpenURL(storeLink);
}
else //open the app
ca.Call("startActivity", launchIntent);
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
}
package is bundleIdentifier of app, such as "com.facebook.katana" and optional parameter is the store link of the app in case the app is not found in device, you can leave it as null and it will only try to open the app if it exists.
Thank you very much @Vandarthul I tried this. And I works successfully. You saved my time. again thank you very much...........!!!!!
How I can open the"play store"(google) in unity by this code? I tested but I could not open play store app.help me plaese
means open link my games in " google play store " Application
Answer by tanveertak · May 19, 2017 at 01:51 PM
1)if you have make the game as you mentioned then use
public void ExitGame()
{
Application.Quit();
}
on the back button it will exit the game 2)and also you have mentioned you also created android app then you would be familiar with activity life cycle on resume() function will be called and you can redirect the user to any activity you want using intents
Intent i=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(i);
setContentView(R.layout.activity_second);
second activity might be your home activity or what every activity you want .
this might not be you expecting the answer but as you mention you have spend 7 days .so it might help but it is alternate and might help you to open your mind for something you are missing till some one else answer
How I can open the"play store"(google) in unity by this code? I tested but I could not open play store app.help me plaese
Answer by shahin2002 · Feb 23, 2019 at 12:27 PM
How I can open the"play store"(google) in unity by this code? I tested but I could not open play store app.help me plaese
means open link my games in " google play store " Application
Answer by paresh_unity496 · Nov 05, 2019 at 11:18 AM
it's work then ok. other wise you should try this. . i have same scenario as you mention in question. this work for me. https://medium.com/@davidbeloosesky/embedded-unity-within-android-app-7061f4f473a