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
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