[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!

performance:

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.

e367f4fd1f5df39297760f80ce1b3f7d

Solution guide after generation, as shown in Fig

f65f1170e2f6b704b9f2fd28df465697

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:

cc68a52d308614ea94c8018978984702

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

99fe7f3a5593edaa47c94fd540550bb4

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

fcb83d3326df26e3a98eca91b2b04938

6. Next jar bag everywhere

0e3b817d660f58b4b8f485a864589950

2d8e61dfa7a8a10cb695c78b19446f0b

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

a2af87dcec9b86c6a47c73faf1b27801

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

44f7651fc1dd3602f2cef083e9dab99c

notice:

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.

0512cf444745c104bad0e326efcd96a9

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

7ccbdaeb89e98fbaa3b3108b32750bc7

9809f383db810dc07fef139a27a5e918

3. Call the appropriate function

a0afcdd1f2f584ad17c6dfd954a6af83

(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