Import the unity project into android studio

This is a summary of my recent learning experience in importing the unity project model into Android.

1. Unity export Android project

To import the unity project to Android, first export an Android project file in unity

Under normal circumstances, just check the Export Project, and then export it. If there are other requirements or configurations, you can configure them in the Player Settings in the lower left corner. I don't have a particularly in-depth study of unity, you can search for information.

(I am using the unity project of the previous example on the official website of unity. This official example has a blogger's video teaching on station B. I put the resources below:

Use Unity as a library in an Android application
GitHub address: https://github.com/Unity-Technologies/uaal-example
Forum address: https://forum.unity.com/threads/integration-unity-as-a- library-in-native-android-app-version-2.751712/ )

The exported unity project file is roughly like this: 

2. Android creation, unity import

(1) First create a new Android project

The package name is set to be consistent with the package name of unity, and the package name of unity is generally com.unity3d.player. If the package names are inconsistent, I have tried it and it can be achieved, but it is easy to confuse when calling the package, and there may be other problems, which I am not very clear about. The recommendation is to stay consistent and avoid trouble. The name of the Android project Name is optional.

(2) Import the unity project to Android as a Module.

(3) Select unityLibrary to import. Click Finish.

(4) After importing, add a reference to unityLibrary for Android.

File in the upper left corner——>Project Structure...

Select Dependencies --> app, then click the plus sign + on the right, and select the third Moudule Dependency

Check the Unity you just imported and click OK. Then click OK in the picture above.

(5) Configure Android and unity build.gradle files.

Configure the SDK so that the current Android version can run. The SDKs of Android and Unity must be the same, otherwise an error will be reported, such as this minsdk. Build is correct even if the import is complete!

3. Android starts running unity

(1) AndroidMainfest.xml file in unity

Delete or comment out <intent-filter>-->, if you keep it, there will be two icons when we run the program on a mobile phone or an emulator.

The second is to add this line of code in <activity> to achieve multi-threading, so as to avoid ending the Android interface when returning to Android from unity.

android:process=":raadidcard"

In the app's AndroidMainfest.xml file, add these two lines of code in the position shown in the figure:

xmlns:tools="http://schemas.android.com/tools"
tools:replace="android:icon,android:theme,android:allowBackup"

 

(2) Then add this line of code in the app's build.gradle.

 ndk {
            abiFilters 'armeabi-v7a'
        }

Next, add this line of code in app's main->res->values->strings.xml.

<string name="game_view_content_description">Game view</string>

The reason for these two steps is that when I run it to the mobile phone, it shows that the hardware is not supported or crashes. After adding the above two codes, unity can be started normally.

(3) Click the button to start unity

        Add a button to the activity_main.xml file of the main project. Add a startup event to MainActivity.java. If the layout is marked red here, just move the mouse under the layout and create a layout. I analyze that it is a problem with the main project, and this has little impact.

4. Start and run

Conclusion: There are many ways to import unity into Android, and this is just one of them. Hope this article is helpful to you.

Guess you like

Origin blog.csdn.net/qq_58451437/article/details/122912174