我想在
android布局中添加unityview作为子视图.
我在android xml文件中创建相对布局,并将unityview添加到relativelayout.
但是统一视图在相对布局的左下方显示为小图像.
我在android xml文件中创建相对布局,并将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(),lp); } }
解决方法
最后我得到了解决方案.
> 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,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); } }
结果: