最近在作一些前端的尝试,须要用到unity3d调用android原生开发打包好的一些功能,因而就在网上搜索一些方法,发现网上的一些方法多多少少有一点错误!这里本身写一个随笔记录一下。前端
1、首先建立android开发好的arr包(android studio开发环境)android
1.建立android工程c#
这里的sdk选择比较重要,要与调试机以及等下unity工程里面的minmun sdk 版本同样才能够(有些低版本的unity支持的sdk版本较低),否则会在unity编译打包的时候报错误!app
二、修改android工程build.gradle(android studio环境须要),让工程变成libraryide
三、将unity的开发包jar导入到android工程中测试
找到unity的开发包classes.jar,路径在unity的安装路径中(D:\Program Files\Unity 2017.1.0b1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes\classes.jar)中,gradle
右键复制classes.jar,直接黏贴到android studio工程libs文件夹中ui
右键app,弹出下拉菜单,选择Open module setting,在弹出的窗口中切换到Dependencies选项卡,将classes.jar包含到工程里面this
4.修改class main ,Mainacity代码让他继承与UnityPlayerActivity类spa
下面是个人代码,写了两个方法,一个是用来测试接口返回值,一个是用来测试调用弹窗的
package com.example.hsh.testun; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity { Context m_context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); m_context = this; } public String ShowDialog(final String _title, final String _content){ return "Java return"; } public void dialog2(){ Log.d("dialog2","ffffffffffff"); } public void dialog1(){ AlertDialog.Builder mBuilder=new AlertDialog.Builder(m_context); mBuilder.setTitle("testtttt") .setMessage("xxxxxxxxxxxxxxxxx") .setPositiveButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Whatever... } }); AlertDialog dialog=mBuilder.create(); dialog.show(); } }
五、编译成arr
2、建立unity工程
一、拉入Buttion跟Text组件用于测试
二、建立Plugins/Android路径(必须是同样名字的)
将刚才android工程建立的arr包中classes.jar,放到unity工程的Plugins/Android路径中,并建立AndroidManifest.xml文件
AndroidManifest.xml文件内容以下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hsh.testun" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true"/> <uses-permission android:name="android.permission.INTERNET"/> <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> </manifest>
其中<activity android:name=".MainActivity">,属性名要和本身android jar中的activity名字同样,个人就是MainActivity,因此不用更改
三、建立c#测试脚本
using UnityEngine; using System.Collections; using UnityEngine.UI; public class TestBtnScript: MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void MyShowDialog() { // Android的Java接口 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); //AndroidJavaClass jc = new AndroidJavaClass("com.example.hsh.testun.MainActivity"); //AndroidJavaObject jo = new AndroidJavaObject("com.example.hsh.testun.MainActivity"); // 参数 string[] mObject = new string[2]; mObject[0] = "Dialog title"; mObject[1] = "Dialog text is here!!"; try { jo.Call("dialog1");
string ret = jo.Call<string>("ShowDialog", mObject); Text text4 = GameObject.Find("Canvas/Text").GetComponent<Text>(); text4.text = ret; } catch(System.Exception e) { Text text2 = GameObject.Find("Canvas/Text").GetComponent<Text>(); text2.text = e.ToString(); } // 调用方法 } }
将MyShowDialog方法绑定到button的点击事件,就能够了。
注意,C#代码中,有注释的//AndroidJavaObject jo = new AndroidJavaObject("com.example.hsh.testun.MainActivity");
若是用这个jo直接调用call去调用android中的ShowDialog方法,是能够拿到string的返回值的,可是调用dialog1去弹窗的状况,会出现安卓报错nullpointer的错误。(具体空指针位置在代码AlertDialog.Builder mBuilder=new AlertDialog.Builder(m_context);这一行中,具体缘由不太明白,但愿有理解安卓机制的大牛告知)。