0

I use Gradle to export unity project into Android Studio.

When I run project in phone, when I press back button, project exits immediately, but I write code to pause the game when I press back button. When I open the project again, game is in pause mode.

I don't want to exit project with back button.

It works correctly when I build project with unity, but because of 64K error, I need to export it. Any idea?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
BlackMB
  • 230
  • 6
  • 20
  • You don't have to export the project when you have that error. Read from **FIX WITHOUT EXPORTING THE PROJECT** in my [answer](https://stackoverflow.com/a/42583210/3785314). You must have Unity 5.5 and above to do the non export solution – Programmer Oct 13 '17 at 12:22
  • yeah, i use gradle inside unity too, but again, when i use back button, it minimized my game again and do what i write in code too... – BlackMB Oct 13 '17 at 12:36
  • I suspect that one of your plugins is doing this. – Programmer Oct 13 '17 at 12:43
  • i mean, when i use Input.GetKeyDown(KeyCode.Escape), this thing happens, because its not do anything in splash screen because i don't have this function there. – BlackMB Oct 13 '17 at 12:56

3 Answers3

0

In android, you can @Override below method on Main Activity to exit from the application. Here you can also show any popup/toast

@Override
public void onBackPressed() {
   //super.onBackPressed(); Enable if you want to exit from application
}
Alpesh Sorathiya
  • 510
  • 3
  • 13
0

Add this code in your Activity and then you can also add your own code in this method also.

With the following code, your activity back button has stopped working.

 @Override
public void onBackPressed() {

}
Akshay More
  • 359
  • 2
  • 13
  • if i insert this code into my Activity, what happens to my unity scripts that i write before? they override to this function too? – BlackMB Oct 13 '17 at 09:47
0

Make an empty game object in your Unity3D scene and add this script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class backButtonAndroid : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }


    void FixedUpdate()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }
        }
    }

}

follow the instructions here if you do not know how to export the project to Android : https://medium.com/@angelhiadefiesta/integrate-a-unity-module-to-a-native-android-app-87644fe899e0

then go to YOUR APP in Android Studio and put android:process=":UnityKillsMe" in your unity activity. It should look like this:

 <activity android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false" android:name="com.yourpackageinfo.UnityPlayerActivity"
            android:process=":UnityKillsMe">
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>

Ensure that android:launchMode="singleTask" is there too.

Tested on Android 8.1 with Unity 2018.1 on Nexus 5x device including also Vuforia libraries for Unity.

hf

Dimitrios Ververidis
  • 1,118
  • 1
  • 9
  • 33