[JAVA] Notes on creating android plugins for Unity

Working in Android Studio

android plugin creation

Create a project with "android studio" Create "android libraly" in "android studio"

Unity program folder to work with Unity? "Classes.jar" in Copy to "libs" folder of "android library" If "classes.jar" is included in aar and compiled, it will fail when building with Unity. Exclude "classes.jar" from being included in aar

Change (gradle) implementation fileTree(dir: 'libs', include: ['*.jar']) ↓ compileOnly fileTree(dir: 'libs', include: ['classes.jar'])

Addendum (gradle) android.libraryVariants.all{ variant-> variant.outputs.each{output-> output.packageLibrary.exclude('libs/classes.jar') } }

Script creation Import Unity classes so that you can throw messages to Unity

import static com.unity3d.player.UnityPlayer.UnitySendMessage;

Put "callbackTarget" and "callbackMethod" in String format in the argument of the class UnitySendMessage allows you to exchange String format strings UnitySendMessage(callbackTarget, callbackMethod, "onReadyForSpeech");

Working in Unity

AndroidJavaClass TestClass = new AndroidJavaClass ("package name.class name"); AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject context = unityPlayer.GetStatic("currentActivity");

context.Call("runOnUiThread", new AndroidJavaRunnable(() => { TestClass.CallStatic( "Function name defined in TestClass", context, gameObject.name, "CallbackMethod" ); } ));

private void CallbackMethod(string message) { Debug.Log(message); }

This allows you to execute the function defined in aar and pass the return value to the function defined in Unity.

Recommended Posts

Notes on creating android plugins for Unity
[Android] Notes on xml
Notes for Android application development beginners
Notes on calling Installer on Android App
Notes on Android (java) thread processing
Android application development using Unity + ARCore on Ubuntu
[Android] Detailed notes
I made a library for displaying tutorials on Android.
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Notes on Protocol Buffers
[Android] Creating more buttons
Notes on multiple inheritance
[Android / Kotlin] Detailed notes 2
Notes for AST analysis
Notes on regular expressions
Notes in Android studio
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~