在Android Studio中将Unity场景显示为子视图 [英] Display Unity Scene as Sub View in android studio

查看:142
本文介绍了在Android Studio中将Unity场景显示为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

感谢Rajawali,我能够显示3D模型,执行简单的动画并移动摄像机.

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

现在,我想执行越来越复杂的动作和动画,并可能创建自定义的材质和纹理,但是Rajawali Wiki非常过时,并且该库有一些限制(在我看来).

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).

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

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.

谢谢.

这是我目前得到的:

这是我的 Android Studio应用的主屏幕.在Backgroud中,我已将Rajawali的3D模型视图加载".

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

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

推荐答案

您正在寻找的是如何将Unity Scene显示为子视图.

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

类似以下内容:

此处在Unity的论坛上.以及加载Unity场景的代码:

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

您可以将Unity项目导出到Android Android项目,然后在上面使用该代码,或者您可以编写该Java代码,然后将其编译为jar插件,并通过在Unity中修改Android Manifest使Unity加载它.两种方法都应该起作用.

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.

最后,您可以使用UnityPlayer.SendMessage.

清单文件:

<?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的函数,请使用

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

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

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

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

这篇关于在Android Studio中将Unity场景显示为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆