Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Develop Unity Augmented reality vuforia application and integrate with native android application

I have an e-commerence application with complex UI and argument reality features like model rendering and image recognition. Application required to support image recognition feature to low end devices as well which is not supported by ARCORE yet. So only option is Unity. Due to no experience in unity, want to develop application in android java (all UI components, add to cart and payment feature) and AR module in unity vuforia. Then integrate unity AR module code in android application and call it from java code. I have seen few tutorials in which java and unity code integrated but those are sample projects with little back forth communication.

So Is this a good approach to develop UI native and AR rendering in unity and then integrate it together? If yes then how much difficulty can be caused by this approach ?

like image 260
Hassan Munir Avatar asked May 29 '18 10:05

Hassan Munir


2 Answers

Android to Unity communication is very simple. I'm using it from last 3 years and its working like a charm :)

IMP: When developing a Unity Android application, it is possible to extend the standard UnityPlayerActivity class (the primary Java class for the Unity Player on Android, similar to AppController.mm on Unity iOS). An application can override any and all of the basic interaction between the Android OS and the Unity Android application.

  • You can write simple Wrapper in your C# unity code and Export the Android Studio source Code from Unity.
  • Unity C# scripts allow you to access features like OS calls and third-party code libraries that would otherwise not be available to Unity.

First Create Code inside Unity and call the android function from unity

1. First Create Build in Unity Editor:

You need to call a Native Android method from Unity or vice-versa : Eg:

 public void OpenActivity()
    {
        var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
        // Accessing the class to call a static method on it
        var jc = new AndroidJavaClass("com.xyz.abc.StartActivity");
        // Calling a Call method to which the current activity is passed
        jc.CallStatic("Call", jo);
    }

}

Replace it by your activity and package name

var jc = new AndroidJavaClass("com.xyz.abc.StartActivity");

In C# (Caller)

jc.CallJavaFunc( "javaTestFunc", "UnityJavaJarTest" );

In Android (Callee)

public void javaTestFunc(String strFromUnity) { 
    /* ... Do something here with java native api ... */ 
} 

2. Then Export the code as development build and import the same in Android studio:

In Android Studio Extend/Modify the UnityPlayerActivity. The UnityPlayerActivity.java file

In Android (Caller)

UnityPlayer.UnitySendMessage("AndroidManager", "SetJavaLog", strFromUnity + "HelloWorld"); 

In C# (Callee)

void SetJavaLog(string strJavaLog)
{
    strLog = strJavaLog;
    _text.text = strLog;
}

Then Create a new Android Manifest to set the new activity as the entry point of your application.

VERY VERY IMP: Though this method support string , still for relible output use pointer/IntPtr


Here is Complete Example Code:

Java:

public final class MyPlugin { //Make class static variable so that the callback function is sent to one instance of this class. public static MyPlugin testInstance;

 public static MyPlugin instance()
 {
     if(testInstance == null)
     {
         testInstance = new MyPlugin();
     }
     return testInstance;
 }

string result = "";


public string UnitySendMessageExtension(string gameObject, string functionName, string funcParam)
{
    UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
    string tempResult = result;
    return tempResult;
}

//Receives result from C# and saves it to result  variable
void receiveResult(string value)
{
    result = "";//Clear old data
    result = value; //Get new one
}}

C#:

class TestScript: MonoBehaviour
{
    //Sends the data from PlayerPrefs to the receiveResult function in Java
    void sendResultToJava(float value)
    {
        using(AndroidJavaClass javaPlugin = new AndroidJavaClass("com.company.product.MyPlugin"))
        {
             AndroidJavaObject pluginInstance = javaPlugin.CallStatic("instance");
             pluginInstance.Call("receiveResult",value.ToString());
        }
    }

    //Called from Java to get the saved PlayerPrefs
    void testFunction(string key)
    {
        float value = PlayerPrefs.GetFloat(key) //Get the saved value from key
        sendResultToJava(value); //Send the value to Java
    }}

Usage from Java:

String str = UnitySendMessageExtension("ProfileLoad", "testFunction","highScore"); 
like image 195
Sanket Prabhu Avatar answered Oct 16 '22 14:10

Sanket Prabhu


I would create the mobile app in UI in unity itself. Unity has many modules for working in mobile AR that i highly recommend you look at. If you know java, then working in c# is not difficult at all They are practically the same. You can do all the UI elements using Vuforia, I do not see why that is an issue. In fact, Vuforia does allow you to use android studio with it, so maybe that route is the best for you.

like image 37
Tayyab Hussain Avatar answered Oct 16 '22 13:10

Tayyab Hussain