This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

👉 About the author

As we all know, life is a long process of overcoming difficulties, reflecting and moving forward. In this process, there will be a lot of questions and thinking about life, so I decided to share my own thinking, experience and story, in order to find resonance!! Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)

👉 Soon to learn

From head to toe, from ignorant to running, perfect joint development of Android and Unity. The article is divided into two parts.

👉 background

🙈 xiao Kong and 🙎 Xiao Zhi: This is a very long story!

👉 Practice Process

The opening

RXlung/Android-Unity source address: RXlung/Android-Unity– Android/Unity Brawl — complete two-way integration interactive guide (github.com)

The purpose of this article is to integrate Unity into Android, which you can do after you complete this article

  1. Android Arbitrary layout loads Unity 3D scenes
  2. Any manipulation of buttons /3D items in the layout (zoom, rotate, etc.)
  3. Exchange messages (you hold me, I hold you)
  4. Feel free to switch scenes in Unity
  5. Dynamic loading of mobile SD card 3D resources, once developed everywhere.
  6. You’re acting like a pussy in front of white

The story to the chase

The first task is to export the Unity project (3D processing is done, communication and dynamic loading are described below).

Click on the blackboard and check Export Project. Then click Export at the bottom and wait for the Project to be exported.

The exported structure feels familiar, just like the Android Studio project structure (PS: it is). Open Studio and import the Android project in the form of a moudle. The first time you might slow down a bit and have a cup of tea. After success comes the important operation. Open the build.gradle file you just imported. The first thing to do is to change the gradle version to match your studio version.

Dependencies {classpath 'com. Android. View the build: gradle: 3.2.0'}Copy the code

Mine is Android Studio 3.2, which is recommended no lower.

Go down and you can find it

apply plugin: 'com.android.application'
Copy the code

Instead of

apply plugin: 'com.android.library'
Copy the code

Since we will be integrating as a library, the next step down is the familiar sdkVsersion, consistent with your AnroID project. There’s also an applicationId. Get rid of that.

Some projects encounter the unityads. aar file, which is not affected. Other modifications and some of the pits encountered will not repeat, there are questions can leave a message. Mine is as follows:

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN buildscript { repositories { Google () jcenter dependencies ()} {the classpath 'com. Android. View the build: gradle: 3.2.0'}} allprojects {repositories { google() jcenter() flatDir { dirs 'libs' } } } apply plugin: 'com.android.library' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) } android { compileSdkVersion 29 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { minSdkVersion 21 targetSdkVersion 29 ndk { abiFilters 'armeabi-v7a', } lintOptions {abortOnError false} aaptOptions {noCompress = ['.unity3d', '.ress', '.resource', '.obb'] } buildTypes { debug { minifyEnabled false useProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt' jniDebuggable true } release { minifyEnabled false useProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt' signingConfig signingConfigs.debug } } packagingOptions { doNotStrip '*/armeabi-v7a/*.so' doNotStrip '*/x86/*.so' } }Copy the code

Next, open the Unity project manifest file androidManifest.xml, delete the application node, delete the Intent-filter node, and add the activity content process(solve the problem).

<application // delete other android:banner="@drawable/app_banner" android:isGame="true"> <application // Delete other Android :banner="@drawable/app_banner" Android :isGame="true" // Android :label="@string/app_name" // Android :launchMode="singleTask" // Add this line Android :process="e.unity3d"> // Delete intent-filter <! --<intent-filter>--> <! --<action android:name="android.intent.action.MAIN" />--> <! --<category android:name="android.intent.category.LAUNCHER" />--> <! --<category android:name="android.intent.category.LEANBACK_LAUNCHER" />--> <! --</intent-filter>--> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> ......Copy the code

At this point, the configuration is complete and you are ready to play.

But in order to play smoothly, we add a custom control (1. To solve the kill problem 2. Increase your scalability). Custom a Java file integrated with UnityPlayer(core key class)

public class MUnityPlayer extends UnityPlayer { public MUnityPlayer(Context context) { super(context); } @Override protected void kill() { //super.kill(); // By default, unity will kill the process, overwrite the kill method, remove super.kill, do not allow it to kill}}Copy the code

After the configuration is complete, click Build, Rebuild Project in Unity’s Lib Project to generate the AAR file

Unity export project configuration is complete, configure your own Android project, first will be a few lib…… So copy to your own project jniLibs.

Then in the build file of the project that you need to integrate

implementation(name: 'XingFeiUnity', ext: 'aar')
Copy the code

Then copy aar to your project libs file, remember to rename delete the following “-debug”, otherwise there will be a class can not find the problem.

A wave of SAO operation done, running up has been basically ok. But is this the end? No way, we can’t stop when we start, please see next chapter for details!

👉 other

📢 by xiao Kong and xiao Kong in Xiao Zhi 📢. 📢 Welcome to like 👍 save 🌟 leave 📝