Solution 1 :
you can achieve it using frame layout only,
MainActivity
should be extended as Activity
, not UnityPlayerActivity
. Add these two lines to activity in the AndroidManifest.xml
.
<meta-data android_name="unityplayer.UnityActivity" android_value="true" />
<meta-data android_name="unityplayer.ForwardNativeEventsToDalvik" android_value="false" />
The working code is:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
android_layout_width="match_parent" android_layout_height="match_parent"
android_rowCount="3"
android_columnCount="10">
<FrameLayout
android_layout_width="match_parent"
android_layout_height="250dip"
android_id="@+id/fl_forUnity"
android_background="#ff00ff15"
android_layout_column="4"
android_layout_row="0"
android_layout_alignParentTop="true"
android_layout_centerHorizontal="true">
</FrameLayout>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Rotate Right"
android_id="@+id/bt_rotRight"
android_layout_column="4"
android_layout_row="2"
android_layout_alignTop="@+id/bt_stop"
android_layout_toRightOf="@+id/bt_stop"
android_layout_toEndOf="@+id/bt_stop"
android_layout_marginLeft="30dp" />
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Rotate Left"
android_id="@+id/bt_rotLeft"
android_layout_column="9"
android_layout_row="2"
android_layout_alignTop="@+id/bt_stop"
android_layout_toLeftOf="@+id/bt_stop"
android_layout_toStartOf="@+id/bt_stop"
android_layout_marginRight="30dp" />
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Stop"
android_id="@+id/bt_stop"
android_layout_below="@+id/fl_forUnity"
android_layout_centerHorizontal="true"
android_layout_margin="10dip" />
</RelativeLayout>
UnityPlayerActivity.java
import android.widget.FrameLayout;
import android.widget.Button;
public class UnityPlayerActivity extends Activity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
//Declare a FrameLayout object
FrameLayout fl_forUnity;
//Declare the buttons
Button bt_rotLeft;
Button bt_rotRight;
Button bt_stop;
// Setup activity layout
@Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
{
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
//setContentView(mUnityPlayer);
//Set the content to main
setContentView(R.layout.main);
//Inflate the frame layout from XML
this.fl_forUnity = (FrameLayout)findViewById(R.id.fl_forUnity);
//Add the mUnityPlayer view to the FrameLayout, and set it to fill all the area available
this.fl_forUnity.addView(mUnityPlayer.getView(),
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//Initialize the buttons from the XML file
this.bt_rotLeft = (Button) findViewById(R.id.bt_rotLeft);
this.bt_rotRight = (Button) findViewById(R.id.bt_rotRight);
this.bt_stop = (Button) findViewById(R.id.bt_stop);
/*Send a brodcast a message to the 'Main Camera' game object, passing L, R according to the
pressed buttons.*/
this.bt_rotLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", "L");
}
});
this.bt_rotRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", "R");
}
});
this.bt_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", " ");
}
});
mUnityPlayer.requestFocus();
}
I hope the below reference URLs might help you.
Unity and Android CustomLayout and Unity and Android sub-view
Problem :
I’m using Unity with Android and it works when the view is in full screen, however when I make it into a subview, the screen doesn’t display anything.
It’s working as full screen:
Intent intent = new Intent(this, UnityPlayerActivity.class);
intent.putExtra("arguments", "data from android");
startActivity(intent);
These questions are also unanswered:
I’ve tried the solutions they have, but none seem to have worked.
Display Unity Scene as Sub View in android studio
https://gamedev.stackexchange.com/questions/134347/android-black-screen-with-unity-app-in-subview
Black screen unity subview in android
My current MainActivity/ Unity Manifest:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
setContentView(R.layout.activity_main);
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.addView(m_UnityPlayer.getView(), 0, lp);
}
<manifest xmlns_android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns_tools="http://schemas.android.com/tools">
<application>
<activity android_name="com.unity3d.player.UnityPlayerActivity" android_theme="@style/UnityThemeSelector" android_screenOrientation="landscape" android_launchMode="singleTask" android_configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android_hardwareAccelerated="false">
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android_name="unityplayer.UnityActivity" android_value="true" />
<meta-data android_name="android.notch_support" android_value="true" />
</activity>
<meta-data android_name="unity.splash-mode" android_value="0" />
<meta-data android_name="unity.splash-enable" android_value="True" />
<meta-data android_name="notch.config" android_value="portrait|landscape" />
<meta-data android_name="unity.build-id" android_value="46e01524-60a4-48fa-9d28-b231f5db3c5d" />
</application>
<uses-feature android_glEsVersion="0x00030000" />
<uses-feature android_name="android.hardware.vulkan.version" android_required="false" />
<uses-feature android_name="android.hardware.touchscreen" android_required="false" />
<uses-feature android_name="android.hardware.touchscreen.multitouch" android_required="false" />
<uses-feature android_name="android.hardware.touchscreen.multitouch.distinct" android_required="false" />
</manifest>
Comments
Comment posted by aalmigthy
What Unity-Version are you using? Unity reworked the whole Android-Integration in version 2019.3.0. The old answers will not work with the new integration.
Comment posted by DIRTY DAVE
I’m using 2019.3.0f6 so quite new. Ah that makes sense, but I’ve been searching and can’t seem to find a solution for this.
Comment posted by Hamid Yusifli
Please provide your full MainActivity class code.
Comment posted by LumbusterTick
According to unity blog on unity as a library , only fullscreen is supported atm.
Comment posted by forum.unity.com/threads/…
Here’s the link to the explanations stating only fullscreen is supported: