Import Unity project into Android Studio (Unity, AS mixed development)

environment

  • Unity 2019.4.8
  • Android Studio 4.0.1

1. Configure the environment of the Unity project

insert image description here
Create the OverrideUnityActivity.java file and place it in the Unity project. The directory structure is as shown above and the
file content is as follows

package com.company.product;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.unity3d.player.UnityPlayerActivity;

public abstract class OverrideUnityActivity extends UnityPlayerActivity
{
    
    
    public static OverrideUnityActivity instance = null;

    abstract protected void showMainActivity(String setToColor);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    
    
        super.onCreate(savedInstanceState);
        instance = this;
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        instance = null;
    }
}

You will know the function of this file as follows

2. Unity export Android project

insert image description here
The exported directory structure is as follows
insert image description here

3. Import Android Studio

Create an empty Android project with no restrictions on the package name and API Level
Import the unityLibrary folder under the unity export file directory into Android Studio as a module
insert image description here

4. Modify build.gradle(module:app)

insert image description here
insert image description here

5. Modify build.gradle (Module: NativeAndroidApp)

insert image description here

6. Modify strings.xml (Module: app)

insert image description here
This step is very important, although I don't know what it does

7. Delete part of the code in AndroidManifest ( module:unityLibrary )

insert image description here
Delete the code specified in the AndroidManifest, this part of the code will cause two icons to appear after the packaged APK is installed

8. Create the MainUnityActivity class (Module: app)

insert image description here
This class inherits from OverrideUnityActivity

9. Modify AndroidManifest (Module: app)

insert image description here

<activity android:name=".MainUnityActivity"
            android:screenOrientation="fullSensor"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
            android:hardwareAccelerated="false"
            android:process=":Unity"
            android:label="UnityProject">
 </activity>

10. Call up the Unity scene

At this point, you can see some phased results
insert image description here

References

Unity official case
https://github.com/Unity-Technologies/uaal-example
The unknown master
https://blog.csdn.net/u014361280/article/details/91888091#æ¡ ˆä¾‹æµ‹è¯•çŽ¯å¢ƒï¼š[
https://www.cnblogs.com/zhxmdefj/p/13273560.html

Special thanks to Brother Zhang, who gave me a lot of guidance in Android Studio.

Guess you like

Origin blog.csdn.net/qq_40028144/article/details/108058407