[Unity3d] call the android interface

Short introduction

There are some mobile phone functions. The unit does not provide proper interfaces, such as vibrations, such as no screen, such as GP, such as …

Lots of special unit features, no interfaces. What we currently need to do is write the Java code with the Android Native ADT editor, then import the jar package into the Unity project, and then use a function that calls the Android interface provided by Unity to call a function in the JAR package.

Here we will introduce the use of Unity to achieve the effect of mobile phone vibration

1. Android provides JAR files, XML files, Res directory, and Directory Libs

2. Call Android function via AndroidJavaclass

In addition, these functions can only work on a real machine!


Android part

1. Download Androidsdk (You need to enable ADT Editor)

Official download connection: http://developer.android.com/sdk/index.html

2 Create an Android project

List position: File-> New-> Android Application Project

During the creation process, only the content of this interface needs to be changed.


Solution guide after generation, as shown in Fig


Among them is a java file that we must modify

3. There

JAR Reference: Module installation pathD:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer\development\binClasses. jar

Add addexternlarchivers as shown in the picture:


4. After the JAR is imported, you can start editing the related functions, open MainActivity


Add UnityActivity to make MainActivity inherit UnityPlayRactivity and remove the SetContentView function (otherwise you will only see one HelloWorm when you run the program on your mobile phone). here

5. At this point, the functions you need have been written, and then modify the XML file

The xml file is called androidmanifest.xml in the project files directory


6. Next jar bag everywhere



After clicking Export, select the JAR file in the Java directory. After choosing a good path, follow the default options for JAR files everywhere.

7. Currently the Android part is finished

part of the unit

1. Install the players corresponding to the android package


2. Add the plugins/android directory to the assets directory and put the Android JAR file, RES folder, LIBS folder and XML file in this directory into this directory



A: You do not have the Unity installation path under the Libs D:\Program Files\Unity\Editor\Presecbackengines\AndroidPlayer\Development\bin directory (to remove the category.


B: If the resource reassembly fails at creation time.

This problem is that the new theme uses the SDK version behind Android and we can include it in the SDK compilation. Here’s a simple and free way to create values, values-11, values-12 and AndroidMenifes related to the file’s related attribute topics. The definition and usage has been removed because the Android project we created here is used to communicate with the unit layer, and the definition and usage of the attribute has no meaning, so we can remove the SDK to compile it.

A: If the script component is a build-time filter, the script class can be found



3. Call the appropriate function


(Note: the initial understanding of the options here is populated with “com.unity3d.player.unityPlayer” and “CurrentActivity” because the MainActivity class was specified when the JAR files were created.)

Leave a Comment