Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unityplayer as subview in android layout?

Tags:

android

Iam trying to add unityview as subview in android layout. I create relative layout in android xml file and added unityview to relativelayout. But the unityview is appearing as small image at left bottom of relative layout.

The black portion is my relativelayout but unity view not occupying enter relative layout.

The code is as follows:

  public class MainActivity extends UnityPlayerActivity {

            public static Context mContext;
            private Handler handler=new Handler();
            private UnityPlayer m_UnityPlayer;

            @Override
                protected void onCreate(Bundle bundle) {
                    super.onCreate(bundle);


                            mContext = this;
                            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);

                            //This is my relative layout
                            RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativelayout2);    
//Relativelayout width is 750dp and height is 640dp.

                            LayoutParams lp = new LayoutParams (750, 640);

//Iam adding unityview to layout here

                            layout.addView(m_UnityPlayer.getView(), 0, lp);


            }
            }

Unitysubview

like image 557
user1065434 Avatar asked Feb 18 '14 10:02

user1065434


1 Answers

Finally I got the solution.

  • MainActivity should be extend as Activity, not UnityPlayerActivity.
  • Add these two lines to activity in android manifest
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />

The working code is:

    public class MainActivity extends Activity implements OnTouchListener {
        public static Context mContext;
        private Handler handler=new Handler();
        protected UnityPlayer mUnityPlayer; 



        protected void onCreate(Bundle savedInstanceState) {


        requestWindowFeature(Window.FEATURE_NO_TITLE);
                super.onCreate(savedInstanceState);
                mContext = this;
                    getWindow().takeSurface(null);
                setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
                getWindow().setFormat(PixelFormat.RGB_565);

                mUnityPlayer = new UnityPlayer(this);
                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);

                View playerView = mUnityPlayer.getView();


                setContentView(R.layout.activity_main);

                FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);     

                LayoutParams lp = new LayoutParams (750, 630);

                layout.addView(playerView, 0, lp);

    }

    protected void onDestroy ()
    {
        mUnityPlayer.quit();
        super.onDestroy();
    }

    // onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
    protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }
    protected void onResume()
    {
        super.onResume();
        mUnityPlayer.resume();
    }
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mUnityPlayer.configurationChanged(newConfig);
    }
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        mUnityPlayer.windowFocusChanged(hasFocus);
    }
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
            return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);
        return super.dispatchKeyEvent(event);
    }

}

The result:

UnityCorrectedSubview

like image 65
user1065434 Avatar answered Oct 05 '22 07:10

user1065434