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

Iam试图添加unityview作为子视图在android布局。 我在android xml文件中创build相对布局,并将unityview添加到relativelayout。 但是统一观在相对布局的左下方呈现为小图像。

黑色部分是我的相对布局,但统一的观点不占用相对布局。

代码如下:

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

Solutions Collecting From Web of "如何在android布局中添加unityplayer作为子视图?"

最后我得到了解决办法。

  • MainActivity应该扩展为Activity ,而不是UnityPlayerActivity
  • 将这两行添加到Android清单中的活动
 <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" /> 

工作代码是:

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

结果:

UnityCorrectedSubview