10
votes

I'm a bit confused about displaying 3D models and graphics in android studio projects.

I've been working a lot with Rajawali, which is a Android OpenGL ES 2.0/3.0 Engine.

Thanks to Rajawali i was able to display 3D models, perform simple animation and move the camera around.

Now I'd like to perform more and more comlicated movements and animation, and maybe create custom materials and textures, but Rajawali wiki is very Out dated and this library has some limits (in my opinion).

So now I wonder if there is a way to create a scene or something in Unity3D, for example an animated 3D model view which i can rotate with the finger, and then turn it into an Android Java class, or a CustomView.

Thanks.

EDIT:

Here's what i got currently:

enter image description here

This is the home screen of my Android Studio APP. In the Backgroud I've got a 3D model view "loaded" with Rajawali.

How can I obtain the same result using Unity?

1
Do you know that unity is able to export your game for android as well?B001ᛦ
I know, but i don't want to develop a game. I want to embedd a 3D model View inside my AndroidStudio App.L. Gangemi
@Kardux Not a duplicate. The answer on that question explains how to export Unity question but not how to show Unity scene as subview.Programmer
@Programmer Agreed this is not entirely the same question but the main part (about Android project export) is still valid. Plus how to change Unity scene from an activity to a SubView isn't really related to Unity but to Android itself (also I didn't knew this was the goal when I posted comment ^^). Anyway I deleted that flag :) To OP: make sure to accept provided answer if it fits your needs.Kardux

1 Answers

16
votes

What you are looking for is how to display Unity Scene as a subview.

Something like below:

enter image description here

This is described here on Unity's forum. And the code to load Unity scene:

package com.unity3d.viewexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;

import com.unity3d.player.UnityPlayer;


public class JavaCubeViewActivity extends Activity {
    private UnityPlayer m_UnityPlayer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the UnityPlayer
        m_UnityPlayer = new UnityPlayer(this);
        int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
        boolean trueColor8888 = false;
        m_UnityPlayer.init(glesMode, trueColor8888);

        setContentView(R.layout.main);

        // Add the Unity view
        FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);    
        LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        layout.addView(m_UnityPlayer.getView(), 0, lp);
    }
}

You can export the Unity project to Android Android project then use that code above or you can write that Java code then compile it as a jar plugin and make Unity load it by modifying the Android Manifest in Unity. Both method should work.

Finally, you can call C# function on the Unity side from Java with UnityPlayer.SendMessage.

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.unity3d.unity" android:versionName="1.0" android:versionCode="1">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    </activity>
    <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
      <meta-data android:name="android.app.lib_name" android:value="unity" />
      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    </activity>
    <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    </activity>
  </application>
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" />
</manifest>

EDIT:

If you want to call Unity's function from Java, use

UnityPlayer.UnitySendMessage("GameObjectName", "MethodName", "parameter to send");

You can find more information on this and how to import it into Android Studio here.