Balu Sangem
Aug 27, 2017 · 1 min read

Adding views on top of UnityPlayer in UnityPlayerActivity

Many developers now a days trying to integrate Unity with Android, and they are successful. But the problem is adding android views on top of Unity Player.

When we get android project from Unity , we will get UnityPlayerActivity.class, This class onCreate() looks like this,

UnityPlayer mUnityPlayer= new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();

In above code we can see , UnityPlayerset as a view.

Instead of setting UnityPlayer as a view, set Layout from resources setContentView(R.layout.activity_unity_player) .

Now onCreate() method looks like this

UnityPlayer mUnityPlayer= new UnityPlayer(this);
setContentView(R.layout.activity_unity_player)
mUnityPlayer.requestFocus();

Create layout R.layout.activity_unity_player and change layout as below,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">

<FrameLayout
android:id="@+id/unity_player_layout"
android:layout_width="match_parent"
android:background="@color/toolbar_color"
android:layout_height="match_parent"/>
<ImageButton
android:id="@+id/back_button"
android:src="@mipmap/done"
android:background="@null"
android:layout_margin="@dimen/dp_10"
android:layout_gravity="bottom|start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/done_button"
android:src="@mipmap/done"
android:layout_margin="@dimen/dp_10"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

We added layout for UnityPlayerActivity.class , to add Unity Player to that layout ,

change UnityPlayerActivity.class in onCreate() method:

        UnityPlayer mUnityPlayer = new UnityPlayer(this);
setContentView(R.layout.unity_player_activity_layout);
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
mUnityPlayer.requestFocus();

Now we have added Unity Player and two custom Image Buttons on top of that Unity Player .

Happy coding , Good luck :)

NOSORT

For Developers by Developers

)

Balu Sangem

Written by

Software Engineer @Everest.Engineering , Software design learner.

NOSORT

NOSORT

For Developers by Developers

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade