How to communicate between Android and Unity? - android

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.

Related

Reusable components with Flutter

The problem
My idea is to create some simple components using Flutter so that they can be used in multiple native iOS and Android projects in our firm.
For example, consider a generic login view. This view can be coded once in Flutter and then can be included later on into projects that have both iOS and Android parts(native) . So such common components can be written once using Flutter instead of twice as native iOS and Android components.
What I have tried so far
I have followed the steps here https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps and it works, but it seems very restricted in functionality.
My questions are
How can I add multiple flutter projects like this, with each project representing a reusable component? Is that even the way to go?
Can I have smaller components- for example in iOS, UIViews instead of FlutterViewController without native hacking ?
How do I handle passing data between the native and Flutter part and also the navigation?
What do you mean by "it seems very restricted in functionality" ?
https://flutter.dev/docs/development/add-to-app lists these limitations:
Running multiple Flutter instances or running in partial screen views may have undefined behavior.
Using Flutter in background mode is still a WIP.
Packing a Flutter library into another sharable library or packing multiple Flutter libraries into an application isn’t supported.
Your 1st question is answered by the 3rd limitation. If you were considering splitting your simple components up into a separate Flutter library each, then no. You would need to package all your components into a single Flutter library, then let the native app pick from the pool.
For your 2nd question, can you be more specific what you mean by "without native hacking" ? Is there a reason you don't/can't use FlutterViewController?
3rd question about passing data between native and Flutter will involve https://flutter.dev/docs/development/platform-integration/platform-channels
From Flutter to native, here's an Android example https://stackoverflow.com/a/49308193/6668797, passing the value "text":
// flutter side, send the data
final String result = await platform.invokeMethod('getBatteryLevel', {"text":text});
// native side, retrieve it
String text = call.argument("text");
From native to Flutter, you use result.success(batteryLevel); to send it back to final String result.

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.

Is PascalScript compatible with Delphi XE5 to create Android/iOS/Windows applications?

I want to create an application that will draw simple forms based on JSON data. I need to have a script to put intelligence on that.
Is Pascal Script from RemObjects a solution for that?
Is is compatible to generate application with the same source code for Android, iOS and Windows?
EDIT: Extending to better understand the need: I have a delphi program that draws a form (pretty much like a dfm file, as a descriptor) and fill it with data from a database. However there is a need for inteligence to validate the data entered. Since the form and data and the validation rules are done by the user there is no way to be a "regular" pascal source code. For that an script language would be perfect, and since I need the same form and same script works in each platform that I could delploy my application it would be also perfect.
This script language should be able to work with exposed objects and deal with database operations (via exposed objects should work also), it is not only a simple IF statement, but there is not need of classes definition. Basically the same concept of what is possible to be done inside a delphi event when attributed by the IDE on OnClick for a TButton for example. It can validate fields, make a calculation of totals, save to a database.
EDIT: I have found that http://www.paxcompiler.com/ PaxCompiler is capable of implement scripting on all target platforms of XE5. I have contacted all the other scripting around this is the only one up to now.
If you want to draw shapes based on JSON data, then just interpret the data using Delphi code and have Delphi to draw the shapes or create the controls.
However I think it is a better idea to create some dummy code and ask RemObject support.
See: http://www.remobjects.com/support/
You can write them an email with some source/pseudo code of what you're trying to do and see what they say.

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

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

Categories

Resources