Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android與fragment交互,fragment顯示unity3d視圖。

android與fragment交互,fragment顯示unity3d視圖。

編輯:關於Android編程

這兩天剛剛接觸Unity3d,之前一直是做android開發,對於Unity3d的開發有專門的人才,我主要涉及在Unity3d與android的交互,經過兩天是實驗終於完成了下面的效果:\ 本來想寫幾篇博客,來詳細說明下Unity3d導出android Project然後再進行二次開發的過程,後來發現幾篇博客,過程和我的類似,對於重復的過程我就不寫了,大家參考他的博客就可以了。而且,包括了Unity3d調用android腳本,這一塊我暫時還沒有涉及。 Unity3D游戲開發之Unity與Android交互調用研究
Unity3D游戲開發之在Android視圖中嵌入Unity視圖
Unity3D游戲開發之從Unity3D到Eclipse
在此,道一聲博主辛苦,對於上面的三篇博客大家看完之後,肯定已經可以從Unity3d導出android project,然後再倒入Eclipse了。我們可以看到MainActivity不是繼承自NativeActivity就是集成自UnityPlayerActivity(或者UnityPlayerNativeActivity),這樣就會給新手一個錯覺,就是要展示Unity3d的視圖,就必須要集成自這幾個接口,那麼這個想法,是錯誤的,具體大家可以參考一下這個博客: http://forum.unity3d.com/threads/using-unity-android-in-a-sub-view.98315/
這個帖子中,就是使用繼承自Activity的類來展示的U3d的視圖。 這樣的話,之前可能存在的一個問題,如果只能繼承自那幾個類,那麼supportV4包中的Fragment豈不是不能用了,我們都知道,android.app.Fragment只支持2.3以上的版本,而V4中可以支持到1.6,所以一般來說都推薦大家使用V4包中的Fragment。 好了,說完這個問題,我們就看上面那個效果,如果在android原生應用的話,這個是很常見也很簡單的應用,我們可以不停的切換中間的Fragment。 那麼換成Fragment來展示U3d,並且相互切換的話,應該怎麼樣書寫代碼呢?大家看看下面兩個代碼塊。
/**
 * 主界面
 * @author gavin
 *
 */
public class MainActivity extends FragmentActivity implements OnClickListener {

	private Button btn1;
	private Button btn2;
	private FragmentManager fm;
	View playerView;
	private Fragment currentFragment;
	private U3dFragment u3dFragment = new U3dFragment();
	private MenuOneFragment menuOneFragment = new MenuOneFragment();
	private MenuTwoFragment menuTwoFragment = new MenuTwoFragment();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Constants.mUnityPlayer = new UnityPlayer(this);
		int glesMode = Constants.mUnityPlayer.getSettings().getInt("gles_mode", 1);
		boolean trueColor8888 = false;
		Constants.mUnityPlayer.init(glesMode, trueColor8888);
		
		fm = getSupportFragmentManager();
		
		setContentView(R.layout.activity_main);
		initView();
		currentFragment = u3dFragment;
		changeFragment(currentFragment);
		
	}
	
	/**
	 * 初始化控件
	 */
	private void initView(){
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.btn1:
				Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show();
				if(currentFragment instanceof U3dFragment || currentFragment instanceof MenuTwoFragment){
					currentFragment = menuOneFragment;
					changeFragment(currentFragment);
				}else{
					currentFragment = u3dFragment;
					changeFragment(currentFragment);
				}
				break;
			case R.id.btn2:
				Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show();
				if(currentFragment instanceof U3dFragment || currentFragment instanceof MenuOneFragment){
					currentFragment = menuTwoFragment;
					changeFragment(currentFragment);
				}else{
					currentFragment = u3dFragment;
					changeFragment(currentFragment);
				}
				break;
		}
	}
	
	/**
	 * 方法用來改變界面
	 */
	private void changeFragment(Fragment fragment){
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.content, fragment);
		ft.commit();
	}
	
	protected void onDestroy ()
	{
		Constants.mUnityPlayer.quit();
		super.onDestroy();
	}

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

}
上面是Activity的寫法。
/**
 * 展示u3d的界面
 * @author gavin
 *
 */
public class U3dFragment extends Fragment {

	private Activity context;
	View playerView;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		context = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		playerView = Constants.mUnityPlayer.getView();
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		playerView.setLayoutParams(lp);
		if(playerView.getParent() != null){
			((ViewGroup)playerView.getParent()).removeAllViews();
		}
		return playerView;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
//		Constants.mUnityPlayer.quit();
		super.onDestroy();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Constants.mUnityPlayer.pause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Constants.mUnityPlayer.resume();
	}
	
}
上面是展示U3d視圖的Fragment的寫法。 要注意的有以下幾點: 1.UnityPlayer這個對象被定義為了靜態變量,在MainActivity中進行初始化,在Fragment中進行使用,用來獲取Unity3d的view。 2.在U3dFragment中沒有在onDestroy中,調用quit()方法。 3.在U3dFragment中的onCreateView()方法中,進行了下面的處理。
((ViewGroup)playerView.getParent()).removeAllViews();
對於上面三個問題,如果不處理會相應的引發下面三個問題。
1.展示U3d視圖的Fragment無法顯示Unity3d的動畫效果。
2.切換頁面的時候會報下面的錯誤:
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeVideoFrameCallback:VILII
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSoftInputClosed:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetTouchDeltaY:VF
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputString:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputCanceled:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetExtras:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetDefaultDisplay:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResume:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResize:VIIII
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequestedAA:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequested32bitDisplayBuffer:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRender:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRecreateGfxState:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativePause:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeKeysPressed:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInjectEvent:ZL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInitWWW:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFocusChanged:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFile:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDone:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeActivityIndicatorStyle:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.initJni:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.UnitySendMessage:VLLL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocationStatus:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocation:VFFFFDF
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeForwardEventsToDalvik:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDeviceOrientation:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodUnblockStreaming:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcess:IL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodInitJni:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodGetInfo:II
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodBlockStreaming:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcessMicData:ILI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyInvoke:LILL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyFinalize:VI 3.會報下面的錯誤: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
現在來說,功能是實現了,但是還有很多可以改進和不明白的地方,大家可以一起討論一下。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved