Communicating between android studio and unity - android

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.

Related

Creating a Class Diagram from Kotlin code in Android Studio

I want to create a class diagram from all the Kotlin packages in Android Studio. Since the code structure is more on the complex side, I'd like a way to automatically generate said diagram. I tried researching and installing some plugins inside of Android Studio but none have worked so far. Most of them seem to be made for IntelliJ and primarily Java, perhaps that's why it's not working. Can you recommend something that would help me generate a Kotlin Class Diagram? Below is an example of the format I'd excpect. Thank you in advance!
Example of a preferred class diagram

Implement camera lib with Kotlin multiplatform

I'm trying to understand what structure should have a multiplatform library. Checking on the Internet I've seen a huge number of examples explaining how to make a log or a "hello world" but there's a lack of complex examples, even in the official documentation (important to note that I'm only interested in mobile platform, iOS and Android).
So I want to create an example that simply opens the camera (as a lib, not as a multiplatform app) just to have an idea of how to work with a real feature which, also, is native. Right now I have created a project following the official example, so it has a common module (using expect) and one for Android and one for iOS (using actual), and now these are my doubts:
I've seen that the iOS module is also in Kotlin, Kotlin/Native as I understand. Should my project have also an wrapper in Swift, or will the library have no Swift code? And if it should, where should it be in the project structure?
Also in the Android module I've noticed I cannot import the class "Activity" nor the "Intent", which I will need to open the camera, why? is this code restricted to Java without the Android libs? Should it also have a wrapper to Android? If so, how can I configure this wrappers?
I know I can use the "expect" key when creating classes but, as I understand, the common and the native modules will always be separated classes. I mean, if I create a class in the common module, can I define methods of this class using "expect" and define them later in the native?
Can my lib have a Manifest?
Finally, does anyone knows a real example that really explain a more complex situation?
Thanks
Okay, let's go through your questions one-by-one.
I would recommend you to have a look at this example
The
iOS module produces an Objective-C framework as a result. It can be utilized by the Xcode project the same way as any other framework with non-Kotlin origins.
It looks like the unavailability to use
Android SDK is the result of using jvm("android") target instead
of android() one. To use the android target, one has to apply the android Gradle plugin in addition to kotlin-multiplatform one.
I
think you want to do something like that: just ordinary class
declaration in the common and extension function for it with an
expect modifier. And then actualize it in the platform-specific
code.
I think so.
I'd also recommend you to have a look at
this and this, maybe these examples will be complex enough for you.😁

Use LibGdx libraries with existing android project?

I have a project that I have been working on which is a standard android application that does not include any other external libraries at this time. I am now looking at making a game at the end of the app(its a quiz so after the questions I want to make a mini game).
So what I am trying to find out is can I just import the libraries that it uses and connect them to my project rather than creating a LibGDX project and importing that?
What it comes down to is your personal preference. In answer to your question, yes you could just add in the jar files and such into your current project, however you are going to have to edit your manifest and some add in some other stuff besides jars using this Setup. In my personal opinion it would be easier to create a libgdx project using the tutorial found here, but in the end as I said before it all depends on what exactly you want to do.
Before deciding, ask yourself.
Do I want to have everything in one project, or can it be in more than one?
What would be better in the long run for managing my app.
Do I want ease now or later?
Cheers!

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

Using custom Android widget from one project in another

I'm attempting to use a custom Android vertical scrollbar widget that seems to work fine in its own example project, but I am having trouble trying to include it into my project. I've exported a .jar from it to use in my project, but it seems to be a minefield of problems, from duplicate resources, to my XML layouts not able to get at styles inside the library, and errors "inflating class" when reading my XML layout file.
How should I go about using the widget from another project in my own?
Quite new to Java development (know the language, but not much of the overarching project/package management), so if there's some good text regarding that it would be appreciated.
Create an Android Library Project for the widget.
As far as I can understand the problem should be related to the "R" class which is created in the same package as the main activity. When you create the new project this class is located in "the wrong place".
Giving up compatibility and require the use of the same package in all projects is not an option: market will not allow you to publish more than one application per namespace. Moreover, is is not a good practice for software engineering.
A much better solution is, as already pointed out by fhucho, to create a library project with your widget to be imported in the main application project.
If you are also looking for a book on android development and already know java I usually recommend "Hello Android" by Ed Burnette, it is quick to read but gives a nice overview with examples.

Categories

Resources