How to Start an Android Activity from a Unity Application

Introduction

This article will guide you through the process of launching an Android Activity from your Unity application. This can be useful for a variety of scenarios, such as accessing device features unavailable in Unity or integrating with external Android apps.

Prerequisites

  • Unity 2017.1 or later
  • Android Studio and the Android SDK
  • A basic understanding of Android development

Steps

1. Create an Android Activity

In Android Studio, create a new Android project. Within your project, create a new Activity class. For example, let’s name it “UnityActivity.java”. This Activity will be the target of your launch request from Unity.

package com.example.yourpackage;

import android.app.Activity;
import android.os.Bundle;

public class UnityActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unity); // Set the layout for the Activity
    }
}

2. Create a Java Plugin for Unity

We will create a Java class that will serve as a bridge between Unity and your Android Activity. In Android Studio, create a new Java class named “UnityBridge.java” in the same package as your Activity.

package com.example.yourpackage;

import android.content.Intent;

public class UnityBridge {
    public static void startActivity(android.app.Activity activity, Class activityClass) {
        Intent intent = new Intent(activity, activityClass);
        activity.startActivity(intent);
    }
}

3. Export the Java Plugin as a JAR

Build your Android project in Android Studio. The generated JAR file (located in the “app/build/intermediates/packaged-classes/debug/” directory) will be used in Unity.

4. Import the JAR into Unity

In Unity, navigate to “Assets/Plugins/Android”. If the “Plugins/Android” folder doesn’t exist, create it. Import the JAR file you exported earlier into this folder.

5. Implement the Launch Logic in Unity

Create a C# script in Unity, for instance, “Launcher.cs”. Use the following code to call the Java plugin and launch the Android Activity:

using UnityEngine;
using System.Collections;

public class Launcher : MonoBehaviour {

    public void StartActivity() {
        AndroidJavaClass pluginClass = new AndroidJavaClass("com.example.yourpackage.UnityBridge");
        AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
        pluginClass.CallStatic("startActivity", currentActivity, new AndroidJavaClass("com.example.yourpackage.UnityActivity"));
    }
}

6. Attach the Script and Trigger the Launch

Attach the “Launcher” script to a GameObject in your Unity scene. In your Unity game, add a button or a trigger to call the “StartActivity” function. When the user interacts with the button, the Android Activity will be launched.

Explanation

The code in the Java plugin provides a simple method “startActivity” which takes the current Activity and the Class of the Activity to launch. The C# script in Unity uses the “AndroidJavaClass” and “AndroidJavaObject” classes to interact with the Java plugin. It retrieves the current Activity context and passes it to the plugin, along with the Class of your Android Activity.

Important Notes

  • Replace “com.example.yourpackage” with your actual package name in the code.
  • Ensure that the Android Manifest file in your Android Studio project includes the “UnityActivity” in the “application” tag.
  • You can pass data between your Unity game and the Android Activity using Intent extras.


Leave a Reply

Your email address will not be published. Required fields are marked *