Home » Android » java – How to integrate Unity into an Android Activity

java – How to integrate Unity into an Android Activity

Posted by: admin June 16, 2020 Leave a comment

Questions:

This is my first time using Unity, and I want to integrate it into an already existing Android app.

From what I’ve read, you need to package your Android project into a jar file. Since you can’t include the resource folder in the jar, how do I handle all of the resources for my app? The app also uses the ActionBarSherlock and ViewPagerIndicator libraries which have resources of their own as well.

I basically just need a way to click a button on Android and pop up an Activity containing the Unity stuff. I want the rest of the app to be native Android code. Thanks for the help!

How to&Answers:

I figured it out. I was following these directions but kept getting various error messages including ClassNotFound exceptions.

It turns out the way that ADT 17 handles .jar files broke this method of integrating Unity into Eclipse. After hours of searching, here’s the fix I came up with:

Instead of adding classes.jar to your build path, you should just copy the classes.jar file into the /lib directory of your project, and everything should work properly.

Answer:

Hi Below are the steps to follow

  1. Create a unity project using android pro.

    1. Open unity . go to File-> build settings in Unity and create a build for project.
    2. go to your-project-location/temp/.
    3. There is a folder named Staging area.
    4. Copy this folder to other location. rename this folder. eg test-unity.
    5. Now go to eclipse create a new project from existing code. go to test-unity and select it. New project is created.
    6. Right click on project and select properties.
    7. select android from left tab list. and check is library on right panel. apply the changes.
    8. Now create new project in android name it test-android.
    9. The package name has to be the same as you have used in unity.
    10. Right click on project. go to android and add library. You will see the unity project name. select it. the unity project is added as library in test-android project.
    11. Now copy the assets folder form unity project to in eclipse to test android project.
    12. Also copy libs->armeabi-v7a to libs folder in unity.
    13. now go to unity installation dir. In my case( C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin) . Copy classes.jar to libs folder in test-android project.
      optional–. After copying classes.jar if you cannot see classes.jar in android-dependencies folder of test android project go to build of test-android and include this jar file.
    14. Now open main activity.class in test-android project and paste this line

    import com.unity3d.player.UnityPlayerActivity;

    1. Now replace Activity with UnityPlayerActivity as below

    public class MainActivity extends UnityPlayerActivity

    1. comment the line setContentview(R.layout.main).
    2. Now run your app.

Answer:

In some scenario developers using native platform technologies (like Android/Java & iOS/Objective C) want to include in their apps/games features powered by Unity (usually for 3D/2D Real Time Rendering), like AR experience, interaction with 3D models, 2D mini games.

You can now insert features powered by Unity (Starting with Unity 2019.3.a2) directly into your native
mobile applications. These features include, but aren’t limited to, 3D
or 2D real-time rendering functions such as augmented reality, 2D
mini-games or 3D models.

You can integrate the Unity runtime components and your content in a
native platform project. The Unity Runtime Library exposes controls to
manage when and how to load/activate/unload within the native
application.

Pre Requirements:

  • Android Studio 3.4.2+
  • Unity version 2019.3.0b4+

Limitations

While we tested many scenarios for Unity as a library hosted by a native app, Unity does not control anymore the lifecycle of the runtime, so we cannot guarantee it’ll work in all possible use cases.

For example:

  • Unity as a Library supports rendering only full screen, rendering on a part of the screen isn’t supported.
  • Loading more than one instance of the Unity runtime isn’t supported.
  • You may need to adapt 3rd party Plug-ins (native or managed) to work properly.

How it works

The build process overall is still the same, Unity creates the iOS Xcode and Android Gradle projects, but to enable this feature we changed the generated iOS Xcode and Android Gradle projects, they now have the following structure:

  • A library part (iOS framework and Android Archive (AAR) file) that includes all source & plugins
  • A thin launcher part that includes app representation data and runs library

Integration Unity as a library in native Android app
Step by step explanations on how to include the Android library part into your native application when needed.

Example: Integration Unity as a library in native Android app

Integration Unity as a library in native iOS app
Step by step explanations on how to include the iOS library part into your native application when needed.

Example: Integration Unity as a library in native iOS app

Sample Project Source Code