Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return from unity activity to MainActivity in android studio

Tags:

I have made an app in android studio and a game in Unity. Now I want to combint these two together. Starting is no problem in android calling the unityNativePlayer. But when I want to go back from the unity activity to the MainActivity.java it comes to this: I have an exit button in the unity game calling Application.exit(); and nothing happens when I press the button. Is there a mistake?

like image 412
Emanuel Graf Avatar asked May 13 '16 00:05

Emanuel Graf


People also ask

How do I export from Unity to Android?

Export to Android Studio In Unity open File | Build Settings... , check Google Android Project and click Export. Open Android Studio and click Import Project (Eclipse ADT, Gradle, etc.) Within the file selection dialog, navigate to the folder where you exported the project and select the folder named after your app.

What is MainActivity this in Android?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

What is MainActivity Java in Android Studio?

The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application − package com.

What will you use to connect activities in Android Studio?

Using an Intent The simplest way to start an activity using an Intent object is to pass it as an argument to the startActivity() method. startActivity(intent); The startActivity() method is generally used only when you want to switch from one activity to another.


1 Answers

Application.exit() does not exist in Unity API.

For the Editor, use UnityEditor.EditorApplication.isPlaying = false;

Use Application.Quit() for your builds.

It is better to use Editor pre-processor to do this or you will have problem building your code that contains UnityEditor.EditorApplication.isPlaying = false; line of code for other platforms.

The code below should solved that problem:

#if UNITY_EDITOR
     UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif

Please understand that you should not have have any exit UI Button for mobile devices. It will work but it is a bad design. This is what the physical Home(iOS) or back(Android) buttons are used for.

EDIT:

You just want to quit Unity Activity from Java. The function should do it.

public void quitUnityActivity ()
{
    UnityPlayer.currentActivity.runOnUiThread(new Runnable(){
        public void run()
        {
            Log.v("Unity", "Unity Activity Exited!" );
            this.mUnityPlayer.quit();
            UnityPlayer.currentActivity.finish();
        }
    });
}

If that doesn't work, try

public void quitUnityActivity()
{
    Log.v("Unity", "Unity Activity Exited!");
    this.mUnityPlayer.quit();
    UnityPlayer.currentActivity.finish();
}

To call the Java function from Unity using C#.

void exitUnity()
{
    AndroidJavaClass myClass = new AndroidJavaClass("com.companyName.productName.MyClass");
    myClass.Call("quitUnityActivity");
}

Make sure to replace com.companyName.productName.MyClass with whatever you named your class in your Android Studio.

like image 167
Programmer Avatar answered Sep 28 '22 03:09

Programmer