简体   繁体   English

将Unity3d视图集成到Android活动中

[英]Integrate Unity3d view into Android activity

I'm currently working on a small AR app for Android and am facing the problem of integrating Unity3d into an activity. 我目前正在开发一款适用于Android的小型AR应用程序,并且面临将Unity3d集成到活动中的问题。 The requirements indicate that I need to be able to present some Android UI - eg menus and an action bar - and a camera view that will display a model created in Unity3d when the target is detected. 这些要求表明我需要能够呈现一些Android UI - 例如菜单和操作栏 - 以及将在检测到目标时显示在Unity3d中创建的模型的摄像机视图。

I found a link that helped me a lot: Unity3d forums . 我找到了一个帮助很多的链接: Unity3d论坛 One of the users there asked the same question I have now but never got any proper answer -that's why I'm posting here. 其中一个用户问我现在有同样的问题,但从来没有得到任何正确的答案 - 这就是我在这里发帖的原因。

Problem: 问题:

I got a small Unity3d project that is essentially a white cube and am trying to display it in one of my Android activities. 我有一个小的Unity3d项目,它本质上是一个白色的立方体,我试图在我的一个Android活动中显示它。 The model looks fine when activity doesn't have setContentView() in its onCreate() method but then I can't specify my layout in an XML file. 当活动在其onCreate()方法中没有setContentView()但是我无法在XML文件中指定我的布局时,模型看起来很好。

When I do add the setContentView() method, I can see the cube but it's very small and there doesn't seem to be any way of actually making it change its size. 当我添加setContentView()方法时,我可以看到立方体,但它非常小,似乎没有任何方法可以实际改变它的大小。

The XML file: XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/unityView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</FrameLayout>

1st version of the activity implementation: 第一版活动实施:

    public class HomeActivity extends UnityPlayerActivity {

        UnityPlayer unityPlayer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }
    }

And the resulting screenshot: 结果截图: 在此输入图像描述

2nd version of the activity implementation: 活动实施的第二版:

public class HomeActivity extends UnityPlayerActivity {

        UnityPlayer unityPlayer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_home);
            unityPlayer = new UnityPlayer(this);
            int glesMode = unityPlayer.getSettings().getInt("gles_mode", 1);
            unityPlayer.init(glesMode, false);

            FrameLayout layout = (FrameLayout) findViewById(R.id.unityView);
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layout.addView(unityPlayer.getView(), 0, lp);
        }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);
        unityPlayer.windowFocusChanged(hasFocus);

    }
}

And the resulting screenshot: 结果截图:

在此输入图像描述

Could anyone explain to me why that is and how to fix it? 任何人都可以向我解释为什么会这样,以及如何解决它?

While I still don't know why it works like that, I've found a way of fixing it. 虽然我仍然不知道它为什么会这样,我找到了一种解决方法。

Instead of simply using setContentView() in onCreate() , extend onResume() and in that method recursively look through all the available views to find the parent view of the UnityPlayer object. 而不是简单地在onCreate()使用setContentView() ,扩展onResume()并在该方法中递归查看所有可用视图以查找UnityPlayer对象的父视图。 Once that's found, layouts and other views can be inflated and added to that parent view. 找到后,布局和其他视图可以膨胀并添加到该父视图中。

Here's the link with a code example - I've used this to make my app work: https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse 这是与代码示例的链接 - 我用它来使我的应用程序工作: https//developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views -日食

Edit: Here's a code snippet showing my solution. 编辑:这是显示我的解决方案的代码段。

@Override
public void onResume() {

    super.onResume();

    if (unityPlayer == null) {

        View rootView = findViewById(android.R.id.content);
        unityPlayer = findUnityPlayerView(rootView);

        if (unityPlayer != null) {

            ViewGroup unityPlayerParentView = (ViewGroup)(unityPlayer.getParent());

            View mainHomeView = getLayoutInflater().inflate(R.layout.activity_main, null);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            unityPlayerParentView.addView(mainHomeView, layoutParams);

        }
    }
}

and

private UnityPlayer findUnityPlayerView(View view) {

    if (view instanceof UnityPlayer) {

        return (UnityPlayer) view;
    }
    if (view instanceof ViewGroup) {

        ViewGroup childrenViews = (ViewGroup) view;

        for (int i = 0; i < childrenViews.getChildCount(); i++) {

            UnityPlayer foundView = findUnityPlayerView(childrenViews.getChildAt(i));

            if (foundView != null) {

                return foundView;
            }
        }
    }

    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM