DMC
DMC

Reputation: 1194

Add button to Unity View

I am trying to add a button to my Unity View in Eclipse. I have imported the Unity project and I have changed the onCreate method so that my setContentView is setting my own xml layout as opposed to the Unity View. Then I add the Unity view to this layout. This works but instead of the button sitting on top of my Unity View it is at the side of my screen. Please see my code below. This is my onCreate method:

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

This is my xml layout 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>

Is there a way that I can add a button on top of the Unity View instead of to the side of it?

Upvotes: 0

Views: 1657

Answers (1)

DMC
DMC

Reputation: 1194

I worked out how to do this myself. Instead of add the Unity View to your own layout you can inflate your own RelativeLayout and add this to the Unity View and then set the Unity View as the setContentView.

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



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

To set up OnTouchListener for the Unity View you use:

playerView.setOnTouchListener(this);

Upvotes: 1

Related Questions