PopupWindow cannot display Unity3D views in Android, but Dialog does?

207 views Asked by At

Here is some brief codes of my project, in which I'd like to display a Unity3D model to do something.

public class MainActivity extends FragmentActivity{
protected UnityPlayer mUnityPlayer;
@Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
    getWindow().setFormat(PixelFormat.RGBX_8888); 
    mUnityPlayer = new UnityPlayer(this);
    ...
    mTestImageButton = ***;
    mTestImageButton.setOnClickListener(mTestImageButtonClickListener);
}
    private ImageButton mTestImageButton;
    private View.OnClickListener mTestImageButtonClickListener = new 
    View.OnClickListener() {
        @Override
        public void onClick(View v) {
          showUnity3D_dialog(v);
          //showUnity3D_popupwindow(v);
        }
    };
    private void showUnity3D_dialog(View v) {
      Dialog dialog = new Dialog(v.getContext(), R.style.move_dialog);
      dialog.setContentView(mUnityPlayer);
      Window window = dialog.getWindow();
      window.setWindowAnimations(R.style.move_dialog);
      WindowManager.LayoutParams lp = window.getAttributes();
      window.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
      lp.width = 1179; 
      lp.height = WindowManager.LayoutParams.MATCH_PARENT;
      window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
      window.setAttributes(lp);
      dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                    }
                });
      dialog.setCanceledOnTouchOutside(true);
      dialog.show();
      mUnityPlayer.resume();
    }
    private void showUnity3D_popupwindow(View v) {
      PopupWindow pop = new PopupWindow(mUnityPlayer, 1000, 800);
      pop.setBackgroundDrawable(new BitmapDrawable()); 
      pop.setFocusable(true);
      pop.setOutsideTouchable(false);
      pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {

                    }
                });
      pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
      mUnityPlayer.resume();
    }
}

As codes above, I tried to display a Unity3D-model in a PopupWindow, but the result makes me a bit confused, since there is nothing on screen when I called showUnity3D_popupwindow(v).

But when I called showUnity3D_dialog(v), the model by Unity3D shows.

I really can't understand what caused such scenario, and how can I display my model in a PopupWindow in android? Or, is it an impossible task?

I have my project under Android 4.3 & Unity 5.4.0.

Thanks a lot.

1

There are 1 answers

0
Programmer On

Looks like you are currently using the PopupWindow(View contentView, int width, int height) overload function. I've seen instances where functions that take View, Context or Activity does not work in Unity and you have to try other overloads.

You've tried the one takes View, now try the Context overload:

PopupWindow(Context context)

then set the width and height later on with the setWidth and setHeight function. You can get the current Context with UnityPlayer.currentActivity.getApplicationContext().

private void showUnity3D_popupwindow(View v) 
{
    Context mContext = UnityPlayer.currentActivity.getApplicationContext();
    PopupWindow pop = new PopupWindow(mContext);
    pop.setWidth(1000);
    pop.setHeight(800);

    pop.setBackgroundDrawable(new BitmapDrawable());
    pop.setFocusable(true);
    pop.setOutsideTouchable(false);
    pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {

    }
    });
    pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    mUnityPlayer.resume();
}