Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from Unity scene to Android Activity

Hello I am currently writing an Android app that uses Unity to display a 3D model. The user is capable of interacting with the model as selecting objects in the scene. What I am having trouble is passing data back to the Android activity.

I currently have the Unity scene executing as a subview, can anyone point me in the direction on how to pass data from the scene to Android?

Do I have to create a Java plugin, explained here? link text

If this is the case, does anyone have any tutorials on this? The original material is kind of lacking. Thanks in advance.

like image 740
user1561427 Avatar asked Jul 29 '12 19:07

user1561427


People also ask

Can Unity export to mobile?

For Unity to be able to export your game as an APK, you need to install the Android Build Support module on the Unity Editor that you are using to build your project. Under the Installs tab of Unity Hub, if you find the circled icon below your editor version, that means that Android Build Support is installed for it.

How does Unity compile to Android?

When Unity builds an Android app, it includes a . NET bytecode interpreter in native code, based on Mono. When you run the app, the interpreter is run to execute the bytecodes. That's how it can run on Android.


1 Answers

If you want to communicate from Unity to an Activity (Java code) you need to create a native plugin. Then in C# from Unity you can find the activity or the JavaObject and pass it parameters.

Check this out.

https://www.thepolyglotdeveloper.com/2014/06/creating-an-android-java-plugin-for-unity3d/

After you watch this page the summary would be:

  • Create a simple Unity Project.
  • Create an Android native plugin. Can be an activity or a simple class.
  • Export the .jar or .aar from Android Studio (i recommend the .aar since it's the android studio library. There you can manage the AndroidManifest.xml and then Unity will merge it with the final one)
  • Put the .aar into Unity project under Assets/Plugins/Android folder.
  • Create a c# script calling for your class method.
  • Build the .apk from Unity (Remember adding the company, product name and bundle id)
  • Run the apk in a device.
  • Test
like image 165
Martin Gonzalez Avatar answered Oct 20 '22 00:10

Martin Gonzalez