Interaction between Unity and Android

1. Goals

    1) Unity3D Callable Android Java Function (in. jar)

2) Java callable unity3d function

3) Unity3D callable android C function (in. so)

2. Test environment

    1) Unity5.0

    2) JDK: jdk-8u25-windows-i586

    3) Android SDK



4) Eclipse: adt-bundle-windows-x86

3. Creating Java Engineering

Step 1:

    

Step 2:


Step 3:


Step 4: Configure Laucher Icon. Don't add your Image File here without any modification. It will force the size of the Image File to be changed and then put it directly into res/drawable.

Step 5:

    

Step 6: Blank Activity uses default values.

Step 7: Delete the following red markers


Note: If the base class is not Activity, right-click on PlugInTest on the left and select Properties to enter the following interface:


    

4. Modifying the Android Project

4.1 Import Unity5.0 classes.jar

It defines UnityPlayerActivity <its base class is Activity>, and its source code location:

      C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer\com\unity3d\player)

2) classes.jar is located in: C: Program Files Unity Editor Data Playback Engines Android player release bin. Its import method is as follows:

      

4.2 Replace Launcher Icon

Overlay ic_launcher.png in the res/drawable-xxx directory.

4.3 Modify MainActivity.java

  1. package com.example.plugintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. import com.unity3d.player.UnityPlayerActivity;  
  7.   
  8. public class MainActivity extends UnityPlayerActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.     }  
  14.   
  15.     // only for test, C# in unity will call it  
  16.     public int GetInt(){  
  17.         return 1000;  
  18.     }  
  19.   
  20. }  


4.4 Export Android content to Unity 5.0

4.4.1 Generate plugintest.jar

1) Enter the Properties of PlugInTest and check "Is Library"

      2) Project->Clean

      3) Project->Build Project

4) See bin directory for generated plugintest.jar

4.4.2 Put PlugInTest content into Unity 5.0

Put the following in the AssetsPluginsAndroid directory of Unity Project.

1) plugintest.jar in bin directory

2) Android Manifest. XML in bin directory (additional Activities or permissions can be added as needed)

3) entire res directory

The results in Unity Project are as follows:

    

5. Modify Unity Project

5.1 Modify Android [Player Settings]



5.2 Access Java Interface

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class AndroidTest : MonoBehaviour {  
  5.   
  6.     // Use this for initialization  
  7.     void Start () {  
  8.   
  9.     }  
  10.       
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.         AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");  
  14.         AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");  
  15.         Debug.Log("*********JerryTest*********" + jo.Call<int>("GetInt"));      
  16.     }  
  17. }  


UnityEngine. Android JavaClass corresponds to java.lang.Class and its subclasses.

UnityEngine. Android JavaObject corresponds to java.lang.Object and its subclasses.




5.3 Export apk

      File->Build Run

When exporting an apk, you may encounter the following problems:

         Unable to find unity activity in manifest. You need to make sure orientation attribute is set to fullSensor manually.

Add a line to Android Manifest:

  1. <meta-data android:name="unityplayer.UnityActivity" android:value="true" />  

All Android Manifests are as follows:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.toltech.game.doubleboats"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <supports-screens  
  7.         android:smallScreens="true"  
  8.         android:normalScreens="true"  
  9.         android:largeScreens="true"  
  10.         android:xlargeScreens="true"  
  11.         android:anyDensity="true"/>  
  12.     <uses-sdk  
  13.         android:minSdkVersion="14"  
  14.         android:targetSdkVersion="21" />  
  15.   
  16.     <application  
  17.         android:allowBackup="true"  
  18.         android:icon="@drawable/ic_launcher"  
  19.         android:label="@string/app_name"  
  20.         android:theme="@android:style/Theme.NoTitleBar">  
  21.         <activity  
  22.             android:name="com.example.plguintest.MainActivity"  
  23.             android:label="@string/app_name" >  
  24.             <intent-filter>  
  25.                 <action android:name="android.intent.action.MAIN" />  
  26.   
  27.                 <category android:name="android.intent.category.LAUNCHER" />  
  28.             </intent-filter>  
  29.             <meta-data android:name="unityplayer.UnityActivity" android:value="true" />  
  30.         </activity>  
  31.     </application>  
  32.   
  33. </manifest>  

6. Android Native Plug

For Active Plugin of Android Platform (armv7,x86), lib*.so should be placed in the following directory:

1) X86 Platform: Assets/Plugins/Android/libs/x86/
2) armv7 platform: Assets/Plugins/Android/libs/armeabi-v7a/


Keywords: Android Unity Java JDK

Added by sheila on Fri, 07 Jun 2019 03:48:29 +0300