向Unity View添加按钮

时间:2013-12-02 12:51:11

标签: c# android eclipse unity3d android-xml

我正在尝试在Eclipse中为Unity View添加一个按钮。我已经导入了Unity项目,并且我更改了onCreate方法,以便我的setContentView设置自己的xml布局而不是Unity视图。然后我将Unity视图添加到此布局。这是有效的,但不是坐在我的Unity视图顶部的按钮,它位于我的屏幕的一侧。请参阅下面的代码。这是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        mUnityPlayer = new UnityPlayer(this);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);

        getWindow().takeSurface(null);
        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
        getWindow().setFormat(PixelFormat.RGB_565);

        if (mUnityPlayer.getSettings().getBoolean("hide_status_bar", true))
            getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
        boolean trueColor8888 = false;
        mUnityPlayer.init(glesMode, trueColor8888);

        UnityPlayer.UnitySendMessage("Character_Mesh", "LoadScene", "CM_Unity_Elf");

        View playerView = mUnityPlayer.getView();

        detector = new GestureDetector(this, this);
        detector.setOnDoubleTapListener(this);

        setContentView(R.layout.menu_buttons);

        LinearLayout lLayout = (LinearLayout) findViewById(R.id.rlContainer);

        lLayout.addView(playerView);

        playerView.setOnTouchListener(this);
        playerView.requestFocus();
    }

这是我的xml布局menu_buttons:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/icon_call" />

</LinearLayout>

有没有办法可以在Unity View的顶部添加按钮而不是在它旁边?

1 个答案:

答案 0 :(得分:1)

我自己研究了如何做到这一点。您可以将Unity视图添加到Unity视图中,然后将Unity视图设置为setContentView,而不是将Unity视图添加到您自己的布局中。您可以将其添加到Unity视图中。

RelativeLayout layout = (RelativeLayout) View.inflate(this, R.layout.menu_buttons, null);



        mUnityPlayer.addView(layout);
        View playerView = mUnityPlayer.getView();
        setContentView(playerView);

要为您使用的Unity视图设置OnTouchListener:

playerView.setOnTouchListener(this);