Michael A
Michael A

Reputation: 5840

Unity android plugin C# code to start activity

I am developing unity android plugin and was wondering if there is a way to start an android Activity class from c# code.

That is something that is equivalent to android code:

Intent intent = new Intent(context,some_activity.class);

Upvotes: 1

Views: 3132

Answers (1)

JcDenton86
JcDenton86

Reputation: 932

here is how I call my plugin for an AR application build with Unity for Android: firstly define your plugin:

private AndroidJavaClass plugin;

later on Start()

plugin = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

Later when you want to call your plugin:

ΑndroidJavaClass jc = new AndroidJavaClass("com.package.name.ActivityName");
AndroidJavaObject jo = plugin.GetStatic<AndroidJavaObject>("currentActivity");
string msg= "a msg to send to android activity";
jo.Call("buttonClicked", msg);

buttonClicked is the method in Java side. After getting to that method you can call any activity using Intents

You should create a jar of your plugin and add it inside the plugin folder of your Unity project.

For more details you can check here

Upvotes: 3

Related Questions