RSS

Step-by-Step guide for developing Android Plugin for Unity3D (II)

10 Aug

In previous blog, I have covered how to handshake Native Android code with Unity3D. The example shown therein is pretty simple: how to call a java function in C#!

In this blog, I am trying to step one more stride further: to create and use an Android View in Unity3D. The example shown here is an Android ImageView, however, this approach is generally applicable to all kinds of Android Views, ViewGroups and custom views.

imageAn Android ImageView

imageUse Android ImageView in Unity3D

First, create an Android Library project with Android Studio. I will not repeat this process and snapshots are available in my previous post.

Create a new class called “Utils”, add below code snippet:

imageimageimageimage

In this class, we defined a few static functions to add a FrameLayout, an ImageView etc to an Android Activity, together with a static function which add an image whose resource is defined as R.drawable.android, which is saved in the lib_project\src\main\res\drawable-xxhdpi folder.

Next, build the library project and we can get an aar file in the folder lib_project\build\outputs\aar\;

Copy this aar file to the Unity3D project, anywhere in the Assets folder or its subfolders;

Create a MonoBehaviour C# script in U3D project as shown below, attached this script to the Main Camera or any game object:

imageimage

In these functions, the Android Java function in the Utils class is called in the C# Start() function, specifically, the Utils.AddExampleImageView() function is called!

Note that in order to call this function, we need to get the Android Activity and pass it as the 1st argument. To implement this, we created a C# static function called GetAndroidActivity() as shown in the code snippet above.

Build the U3D project and run it on an Android device! Wow, it worked now!

For the source code, please clone this GitHub Repo. Don’t forget to star it!

Advertisements
 
10 Comments

Posted by on August 10, 2016 in Android, Programming, Unity 3d

 

Tags: , , , , , , ,

10 responses to “Step-by-Step guide for developing Android Plugin for Unity3D (II)

  1. dole

    September 3, 2016 at 1:32 am

    great tutorial,

    what about the other way: to use an object ( a 3D object for example) into android. Can you post a tutorial about this, or to guide me into the right direction?

    thank you!

     
  2. xinyustudio

    September 8, 2016 at 10:19 pm

    I might cover this in future tutorials.

     
  3. shishir

    September 27, 2016 at 7:50 pm

    Hi, I have followed this tutorial and the preceding one, Both work great. Thanks. However, I am not able to call a non-static method of java from unity c# file. I have checked everywhere online, none of the solutions worked for me. Can you pls help me in calling a non-static java method from unity?

     
  4. Shishir Sahoo

    September 27, 2016 at 7:51 pm

    Hi, I have followed this tutorial and the preceding one, Both work great. Thanks. However, I am not able to call a non-static method of java from unity c# file. I have checked everywhere online, none of the solutions worked for me. Can you pls help me in calling a non-static java method from unity?

     
  5. xinyustudio

    September 27, 2016 at 8:32 pm

    you have two options, refer to https://docs.unity3d.com/ScriptReference/AndroidJavaObject.html, just replace CallStatic with Call(); 2nd option: you wrap your instance method into a static function in Java, that is it!

     
  6. xinyustudio

    September 27, 2016 at 8:34 pm

    Please vote if you like it!

     
  7. grosalex

    November 28, 2016 at 11:20 pm

    thanks for this nice tutorial.
    I’ve just one question, how do you add fastly dependencies you have in your gradle to the unity project which is going to use it? Unity seems to not be able to find those classes?

    Regards,

     
  8. suren suren

    April 7, 2017 at 7:06 pm

    Is there any possibility to run android webservices from unity 3D?

     
  9. Reda

    May 27, 2017 at 5:19 am

    Hi i tried to try your code in many devices but not working The first one is working good the second one is not showing the image and i dont know why can you help me please ?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: