在一个现有的Android项目中嵌入unity3d项目
1.将unity3d项目导出android工程
2.将第一步导出的Android工程中assets文件夹和libs文件夹下的所有内容复制到Android项目中对应的assets和libs文件夹下
3.Android调转到unity
package com.jxkj.testandroid; import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button startGameBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startGameBtn = (Button) findViewById(R.id.StartGameButton); startGameBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { GotoGameActivity(); } }); } void GotoGameActivity(){ Intent intent = new Intent(this,GameActivity.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
package com.jxkj.testandroid; import com.unity3d.player.UnityPlayer; import android.app.Activity; import android.os.Bundle; public class GameActivity extends Activity{ UnityPlayer mUnityPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUnityPlayer = new UnityPlayer(this); setContentView(mUnityPlayer); //setContentView(R.layout.activity_main); } // Resume Unity @Override protected void onResume() { super.onResume(); mUnityPlayer.resume(); } // Notify Unity of the focus change. @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); } @Override public void onBackPressed() { runOnUiThread(new Runnable() { @Override public void run() { mUnityPlayer.quit(); } }); super.onBackPressed(); } }