Fading Coder

An Old Coder’s Final Dance

You are here: Home > Tech > Content

Creating and Using Unity Plugins with Android Studio

Tech 3

Setting Up the Android Studio Project

  1. Create a new project:

    • In Android Studio, navigate to File > New > New Project.
    • Enter a suitable Application Name and Company Domain, ensuring the Package Name matches the Unity project's Bundle Identifier.
    • Click Next, selecting "Phone and Tablet" and specifying the Minimum SDK.
    • Choose "Empty Activity" and use default values for the Activity and Layout Names.
    • Click Finish to generate the project.
  2. Add Unity's classes.jar:

    • Copy the Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar file from the Unity installation directory.
    • Place it in the libs directory of your Android Studio project.
    • Right-click on Project, select Open Module Settings, and add classes.jar as a dependency.

Developnig the Android Code

Edit the main activity to include the necessary Unity integration:

import android.os.Bundle;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void ShowToast(final String message){
        runOnUiThread(() -> Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show());
    }

    public static String GetInformation() {
        return "This is a Plugin's content!";
    }
}

Modifying the Build Configuration

Adjust build.gradle to configure the project for AAR export:

  1. Replace apply plugin: 'com.android.application' with apply plugin: 'com.android.library'.
  2. Remove the applicationId entry.
  3. The modified build.gradle:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation files('libs/classes.jar')
}

Finalizing the Android Manifest File

Ensure the manifest file contains the appropriate <meta-data> information for the main activity. Remove unnecessary resources such as the styles.xml file.

Exporting and Integrating the Plugin with Unity

  1. Build the AAR file by selecting Build > Build APK.
  2. Remove the classes.jar file from the generated AAR, as Unity includes its copy during APK generation.

Importing the Plugin into Unity

  1. Create a Unity project.
  2. Import the generated AAR file:
    • Create the Plugins/Android directory in your Unity project.
    • Place the AAR file and Android Manifest in the Android folder.

Implementing the Unity Code

Create a script to interface with the Android functionality:

using UnityEngine;

public class Call : MonoBehaviour {
    string information_ = null;

    void OnGUI() {
        if(GUI.Button(new Rect(0, 0, 200, 20), "Show Toast - Hello World!")) {
            using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) {
                    jo.Call("ShowToast", "Hello World!");
                }
            }
        }

        if (GUI.Button(new Rect(0, 40, 200, 20), "Get Plugin's Information")) {
            using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) {
                    information_ = jo.CallStatic<string>("GetInformation");
                }
            }
        }

        GUI.Label(new Rect(220, 40, Screen.width - 220, 20), information_);
    }
}

Set the Unity project's Bundle Identifier and Minimum API Level to match the plugin's configuration. Build the Unity APK and test the functionality.

Tags: Unity

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.