1
\$\begingroup\$

I've exported an Unity app/game I have made to Android Studio. In Android Studio I made a layout and within it I have a framelayout. However, when I run the app the framelayout is completely black, why?

Here is my Java code:

public class UnityPlayerActivity extends Activity {
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

    FrameLayout fl_forUnity;

    Button bt_groen, bt_gul, bt_sort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.RGBX_8888); 

        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(R.layout.main);


        this.fl_forUnity = (FrameLayout) findViewById(R.id.fl_forUnity);

        this.fl_forUnity.addView(mUnityPlayer.getView(),
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);


        this.bt_groen = (Button) findViewById(R.id.bt_groen);
        this.bt_gul = (Button) findViewById(R.id.bt_gul);
        this.bt_sort = (Button) findViewById(R.id.bt_sort);

        this.bt_groen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", "G");
            }
        });

        this.bt_gul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", "Y");
            }
        });

        this.bt_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", "B");
            }
        });

        mUnityPlayer.requestFocus();
    }
}

Here is my 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="400dip"
        android:id="@+id/fl_forUnity"
        android:background="#00FFFF"
        android:layout_centerHorizontal="true">

    </FrameLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Grøn"
        android:id="@+id/bt_groen"
        android:layout_column="4"
        android:layout_row="2"
        android:layout_alignTop="@+id/bt_sort"
        android:layout_toRightOf="@+id/bt_sort"
        android:layout_toEndOf="@+id/bt_sort"
        android:layout_marginLeft="30dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gul"
        android:id="@+id/bt_gul"
        android:layout_column="9"
        android:layout_row="2"
        android:layout_alignTop="@+id/bt_sort"
        android:layout_toLeftOf="@+id/bt_sort"
        android:layout_toStartOf="@+id/bt_sort"
        android:layout_marginRight="30dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sort"
        android:id="@+id/bt_sort"
        android:layout_below="@+id/fl_forUnity"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dip" />

</RelativeLayout>
\$\endgroup\$
4
  • \$\begingroup\$ I had a similar issue a little while back. Have you preloaded any assets in your player settings? For me preloading assets is what caused the problem \$\endgroup\$ Commented Dec 13, 2016 at 16:58
  • \$\begingroup\$ What do you mean by preloaded? \$\endgroup\$ Commented Dec 13, 2016 at 16:58
  • \$\begingroup\$ In your player settings there's an option to preload assets, which I found caused my game to throw a black screen on Android. If you haven't found it then you're probably not using it though TBH. The manual page is here if you're interested :-) docs.unity3d.com/Manual/class-PlayerSettingsAndroid.html \$\endgroup\$ Commented Dec 13, 2016 at 17:04
  • \$\begingroup\$ Ok.... What could then be the problem? \$\endgroup\$ Commented Dec 13, 2016 at 17:23

1 Answer 1

-1
\$\begingroup\$

I found the answer: add android:process=":UnityKillsMe" to your unity library activity tag in manifest.

And also, we should implement onWindowfocusChangedListener for sure:

void OnWindowFocusChanged(){ m_UnityPlayer.windowFocusChanged(hasFocus);}

My code: It worked

public class MainActivity extends AppCompatActivity {

   private UnityPlayer m_UnityPlayer;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Create the UnityPlayer
       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.frameLayout);
       LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
       layout.addView(m_UnityPlayer.getView(), 0, lp);
       m_UnityPlayer.windowFocusChanged(true);
       m_UnityPlayer.resume();
   }
   @Override
   public void onWindowFocusChanged(boolean hasFocus)
   {
       super.onWindowFocusChanged(hasFocus);
       m_UnityPlayer.windowFocusChanged(hasFocus);
   }
   @Override
   public void onPause() {
       super.onPause();
       m_UnityPlayer.pause();
   }
   @Override
   public void onResume() {
       super.onResume();
       m_UnityPlayer.resume();
   }

   @Override
   protected void onDestroy() {
       super.onDestroy();
       m_UnityPlayer.quit();
   }
}
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.