10

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?

Programmer
  • 121,791
  • 22
  • 236
  • 328
L. Gangemi
  • 3,110
  • 1
  • 22
  • 47
  • Do you know that unity is able to export your game for android as well? – B001ᛦ May 11 '17 at 14:34
  • 1
    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 May 11 '17 at 14:35
  • 2
    @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 May 11 '17 at 17:08
  • 1
    @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 May 12 '17 at 07:23

1 Answers1

16

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • That's what i was looking for! I'll give a try, thanks ;) – L. Gangemi May 12 '17 at 06:37
  • Could you only edit the link refering to the forum? Becouse not it refers to the image, thanks – L. Gangemi May 12 '17 at 06:38
  • Do you know any other discussion or example about that? Bocouse this one is from 2012. Anyway no problem, just asking. – L. Gangemi May 12 '17 at 07:36
  • `m_UnityPlayer.getView()` returns Unity's Android View Unity is running on. If you know other ways in Android to display another View other than `FrameLayout`, you can use that in conjunction with `m_UnityPlayer.getView()`. That should work too. – Programmer May 12 '17 at 07:49
  • You should take a look at the [`UnityPlayer.java`](https://github.com/eppz/Unity.Labs.Unity_Player_Android/blob/master/com/unity3d/player/UnityPlayer.java) class source code. It will show you many good variables you can use. Another variable from that class is the `private SurfaceView n`. If you can make that a public variable, you can absolutely use it to display Unity in your game as a sub-view but none of these are necessary. The `m_UnityPlayer.getView()` that returns `View` should be fine. – Programmer May 12 '17 at 07:55