Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android布局中将unityplayer添加为子视图?_Android - Fatal编程技术网

如何在android布局中将unityplayer添加为子视图?

如何在android布局中将unityplayer添加为子视图?,android,Android,我正在尝试在android布局中将unityview添加为子视图。 我在android xml文件中创建相对布局,并将unityview添加到relativelayout。 但unityview在相对布局的左下角显示为小图像 黑色部分是我的relativelayout,但unity视图没有占据enter relative布局 代码如下: public class MainActivity extends UnityPlayerActivity { public st

我正在尝试在android布局中将unityview添加为子视图。 我在android xml文件中创建相对布局,并将unityview添加到relativelayout。 但unityview在相对布局的左下角显示为小图像

黑色部分是我的relativelayout,但unity视图没有占据enter relative布局

代码如下:

  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);


            }
            }

我终于找到了解决办法

  • MainActivity
    应扩展为
    Activity
    ,而不是
    UnityPlayerPractivity
  • 将这两行添加到android清单中的活动中

工作守则是:

    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);
    }

}
结果是:


啊,原来除了onCreate之外,我还缺少了所有的on*方法:facepalm:我想在杯子形状的图像中旋转更多的球,有可能吗?请参考此链接