【原】unity调Android(二)
unity调android 本人目前发现android主activity必须继承UnityPlayerActivity,unity才能通过
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.call("函数名")
或者
AndroidJavaClass jc = new AndroidJavaClass("包名+.主Activity");
jc.CallStatic("函数名");
unity调Android(一)中unity没法调用android 本文主要解决调用问题:
实现思路是通过activity切换调用的方式。受益于 雨凇momo教程 。让主activity继承UnityPlayerActivity,然后在主activity里通过
Intent intent = new Intent(mContext,"子activity"+.class);
intent.putExtra("Activity", "Activity");
this.startActivity(intent);
以下主要实现代码:
public class SDKDemo extends UnityPlayerActivity{ Context mContext=null; JoyActivity joyActivity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext=this; Intent intent = new Intent(mContext,JoyActivity.class); intent.putExtra("JoyActivity", "JoyActivity"); this.startActivity(intent); } public void nativeInit() { Log.e("nativeInit","nativeInit Enter"); joyActivity=new JoyActivity(); joyActivity.nativeInit(); Log.e("nativeInit","nativeInit End"); } public class JoyActivity extends Activity { UnityPlayer unityPlayer; // UnityPlayer.init() should be called before attaching the view to a layout. // UnityPlayer.quit() should be the last thing called; it will terminate the process and not return. protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); requestWindowFeature(Window.FEATURE_NO_TITLE); 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(playerView); playerView.requestFocus(); } 以上代码 也可以参考 unity调android(一)中的方式 public void nativeInit () { Log.e("nativeInit", "##########################"); } unity代码: void Start () { jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("nativeInit"); }