Display Unity Scene as Sub View in android studio

前端 未结 1 2073
盖世英雄少女心
盖世英雄少女心 2020-11-29 11:38

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

相关标签:
1条回答
  • 2020-11-29 12:00

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

    Something like below:

    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.

    0 讨论(0)
提交回复
热议问题