集成Unity3d查看到Android的活动 [英] Integrate Unity3d view into Android activity

查看:264
本文介绍了集成Unity3d查看到Android的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Android的小AR的应用程序,现在面临整合Unity3d到活动的问题。这些要求表明,我需要能够以present一些Android的用户界面 - 例如,菜单和操作栏 - 和一个摄像头视图,将显示在Unity3d创建了一个模型检测到目标时

I'm currently working on a small AR app for Android and am facing the problem of integrating Unity3d into an activity. The requirements indicate that I need to be able to present some Android UI - e.g. menus and an action bar - and a camera view that will display a model created in Unity3d when the target is detected.

我发现,帮助了我很多的链接:<一href="http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View/page4?p=1477830&viewfull=1#post1477830"相对=nofollow> Unity3d论坛的。其中的一个用户那里问同样的问题,但现在从来没有我有任何正确的答案 - 这就是为什么我在这里发帖。

I found a link that helped me a lot: Unity3d forums. 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.

问题:

我有一个小的Unity3d项目,本质上是一个白色立方体,我试图在我的Andr​​oid的活动之一,显示它。该模型看起来很好,当活动不具有的setContentView()的onCreate()方法,但我可以'牛逼指定在一个XML文件我的布局。

I got a small Unity3d project that is essentially a white cube and am trying to display it in one of my Android activities. 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.

当我添加的setContentView()的方法,我可以看到的立方体,但它是非常小的,似乎没有任何办法实际上使得它的变化它的大小。

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.

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>

活动实施第1版:

    public class HomeActivity extends UnityPlayerActivity {

        UnityPlayer unityPlayer;

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

和所产生的截图:

And the resulting screenshot:

活动实施第2版:

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

    }
}

和所产生的截图:

任何人都可以向我解释这是为什么,以及如何解决它?

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.

而不是简单地使用的setContentView()的onCreate(),延长 onResume( )并在该方法递归翻阅所有可用的意见,找到UnityPlayer对象的父视图。一旦这样发现,布局和其他视图可以膨胀并添加到父视图。

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. Once that's found, layouts and other views can be inflated and added to that parent view.

下面是用code例如链接 - 我用这个做我的应用程序的工作:<一href="https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse" rel="nofollow">https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse

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

编辑:这里有一个code片段展示我的解决方案

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

        }
    }
}

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

这篇关于集成Unity3d查看到Android的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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