How to pass data from Unity scene to Android Activity - android

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.

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

I'm not sure since I haven't worked with Unity before. But there are some ways to transfer data between activities, I hope it'll help you.
Intent
Parcelable

Related

Communicating between android studio and unity

I am developing an AR app. I am developing all the UI and menus in android studio and a scene in unity. I am trying to pass a string from my android studio project to my unity scene. Is there a built-in way to do this? I am trying to implement it with writing to files but it doesn't seem like a good way to do it.
Thank You in advance.
You can achieve this in many different ways:
Display your mUnityPlayer within a Fragment and then handle the scene with the FragmentManager
Extend an Activity from UnityPlayerActivity and communicate as the rest of activities
Use SharedPreferences and its listeners
You can think of many other ways to tackle this issue, hope any of them give you a hint
Get classes.jar from one of the sub-folders from
<UnityInstallationDirectory>\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono
or
<UnityInstallationDirectory>\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp
depending on if you are using mono or IL2CPP as Scripting Backend to build your Android Project.
Once you import the classes.jar module into your Android Java project, you can the use UnityPlayer.UnitySendMessage to call C# function from Java.
UnityPlayer.UnitySendMessage("Gameobject Name","Method","Message")
If you plan to extend UnityPlayerActivity then grab UnityPlayerActivity.java from
<UnityInstallationDirectory>\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player.
Finally, UnityPlayer.UnitySendMessage is what you are looking for. There are many tutorials out there on how to use it, if you are still confused.

Insert unity as in a subview in android and plug in

I have to build an android app in which I have to integrate unity player with an existing android application. I need to get values from a sensor and animate the gameObject in unity. Should I make a plugin using ndk or jni? Please help. I'm new to this. I have read the docs but I am confused.
You can choose to build an Android Studio project instead of an APK. To do this, select the "Google Android Project" checkbox from the android build settings.
This will give you a project with a main activity which is a UnityPlayerActivity. You can add/remove activities, views and do anything you want with the project. You should also be able to move the contents to another project and merge them. The important thing here is to keep all the libraries and resources Unity builds intact. This should all feel natural if you're familiar with Android Studio.
To pass data to/from Unity, you can use the AndroidJava* classes, which essentially allow you to call any Java code (from the OS libraries or your app package) as if you were using reflection API.

How to communicate between Android and Unity?

I can figure out how to embed a unity project inside an Android project from articles on internet and Unity's own Learning section http://unity3d.com/learn. What I am having trouble with is communicating between Unity and Android.
I need to send Unity some JSON data which will contain numbers and values to display and the URLs for a couple of images. From there on inside unity's scene I want to do some animation. Based on the selection made by the user, I need to send appropriate requests to our server and open different activities.
In normal Android development, I would pass information from one activity to another using Bundle, but I can't figure out how to do the same with Unity and Android. Getting user selection from Unity's scene to my application is also a problem that needs to be solved.
Unity game is not a normal Android app. It's more like a library integrated into an Android app.
Unity docs demonstrates that to call methods on Android native side from Unity you can use AndroidJavaClass (for Java class references and static calls) and AndroidJavaObject (for references to objects).
To interact the other way around you can use UnitySendMessage() in Java. The last parameter in UnitySendMessage() is an optional string argument that you can use to pass JSON to Unity.
In Unity you can either use JsonUtility.FromJson for deserialization or some 3rd party scripts for json parsing, there are a lot on github.

Cardboard SDK Unity - way to go native and share properties?

We are new to unity, and experimenting with a cardboard component. This is a separate project for now, but we would like to move this code over to our existing projects and have it called from non unity activities/controllers. But is there a way for the two technologies to share data? Based on some choices in the non-unity part of the app, it will change what URL the unity part needs to call. Can we use NSUserDefaults/SharedPreferences for Unity to get data from the other native side?
Unity can communicate with native code through the plugin architecture, documented here:
http://docs.unity3d.com/Manual/Plugins.html
This is way too big a subject for me to provide more than this frowned-upon link-only answer.

Using Unity player/engine to load 3d in existing Android application

Have created a Augment Reality app using Vuforia which would play video on recognition. Now, just need the ability to show 3D objects as well. I did follow the video tutorial from Vuforia on this and able to get the 3D models showing on targets using Unity engine. But, when I exported the project as android (eclipse) project I could locate only 3 activity classes. The challenge here is how to integrate these two projects together.
The component that loads 3D model using unity player is abstracted and not exposed. I was thinking I could just simply use the unity player API to load the 3D models within my existing project.
Is there a way the 3D model loading code be exposed to the Java (android) part? As I need to dynamically load the models and might need to download it from the server and load it.
Since we have done so much work on the UI part of our existing application it will be really helpful if we can just plug the Unity player and manage the model inside our app.
Thanks in advance.
You may find some tips here : https://developer.vuforia.com/forum/faq/unity-how-can-i-extend-unitys-android-activity
It was helpfull to me to override QCARPlayerActivity class, and it seems to work fine (actually, i'm stuck with another problem to finish() the activity...)

Categories

Resources