Unity调用android的方法网上有许多,这里就不在赘述了,然而我们调用AndroidSDK时,一般都是继承UnityPlayerActivity,然后我们从MainActivity中启动我们的SDK。
那么问题来了,假如在一个已经添加了Unity的SDK,并且其已经继承UnityPlayerActivity时,我们该如何在不修改器Java文件的情况下执行我们的SDK呢。
思路:在C#中获取UnityPlayerActivity类,并且将其作为参数传入方法。
1 Unity: 2 ``` 3 AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 4 AndroidJavaObject unityActivity = unity.GetStatic<AndroidJavaObject>("currentActivity"); 5 using (AndroidJavaObject jo = new AndroidJavaObject("com.klondike.solitaire.spider.Classic.SDKBridge")) 6 { 7 jo.Call("PluginCall", unityActivity); 8 Debug.Log("调用安卓方法"); 9 } 10 Java: 11 public void PluginCall(Activity activity) 12 { 13 Log.d("PluginCall","CALLALLLLL"); 14 } 15 16 通过这种方法,可以不必在MainActivity修改,直接启动相关SDK 17 ``` 18 安卓端 19 ``` 20 MainActivity.java 21 public void ShowMsg(Activity activity,String msg) 22 { 23 Intent intent = new Intent(activity.getBaseContext(),HelloWorld.class); 24 intent.putExtra("type", msg); 25 activity.startActivity(intent); 26 } 27 28 HelloWorld.java 29 public class HelloWorld extends Activity 30 { 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 // setContentView(R.layout.activity_main); 35 // TextView showMsg = (TextView) findViewById(R.id.android_show); 36 String type = this.getIntent().getStringExtra("type"); 37 // showMsg.setText(type); 38 } 39 } 40 ```