android - 在android studio中将Unity场景显示为 subview

标签 android unity-game-engine

我对在 android studio 项目中显示 3D 模型和图形有点困惑。

我一直在与 Rajawali 合作,这是一个 Android OpenGL ES 2.0/3.0 引擎。

感谢 Rajawali,我能够显示 3D 模型、执行简单的动画并移动相机。

现在我想执行越来越复杂的 Action 和动画,也许还可以创建自定义 Material 和纹理,但是 Rajawali wiki 已经过时了,而且这个库有一些限制(在我看来)。

所以现在我想知道是否有一种方法可以在Unity3D中创建场景或其他东西,例如我可以用手指旋转的动画3D模型 View ,然后将其转换为Android Java类或CustomView。

谢谢。

编辑:

这是我目前得到的:

enter image description here

这是我的Android Studio APP的主屏幕。在背景中,我有一个用 Rajawali“加载”的 3D 模型 View 。

如何使用 Unity 获得相同的结果?

最佳答案

您正在寻找的是如何将 Unity 场景显示为 subview 。

如下所示:

enter image description here

这是描述的here在 Unity 的论坛上。以及加载Unity场景的代码:

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);
    }
}

您可以export将 Unity 项目转换为 Android Android 项目,然后使用上面的代码,或者您可以编写该 Java 代码,然后将其编译为 jar 插件,并通过修改 Unity 中的 Android Manifest 来让 Unity 加载它。两种方法都应该有效。

最后,您可以使用UnityPlayer.SendMessage从Java调用Unity端的C#函数。

list 文件:

<?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>

编辑:

如果你想从Java调用Unity的函数,使用

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

您可以找到有关此内容以及如何将其导入 Android Studio 的更多信息 here .

关于android - 在android studio中将Unity场景显示为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44758520/

相关文章:

ios - 为应用程序商店构建时出现 XCode 错误

c# - 一次分配多个变量

unity-game-engine - Google Cardboard SDK for UNITY - 检测设备兼容性

android - 无法为锁定文件创建父目录

android - 如何将 android 库项目导出为 jar 文件,该文件将包含它自己引用的库项目的 jar

android - Nativescript RadSideDrawer 未定义

java - 如何将 TextInputLayout 与 Theme.material 一起使用?

android - 为图像按钮添加边框

optimization - Unity3d 绘制调用计数因平台而异

c# - 如何整理出一个RandomRangeInt只能从Unity的主线程调用?