• Unity 与 Android (Android Studio)的交互


    http://blog.csdn.net/kuerjinjin/article/details/50177633

    1.大体思路:

    Android Studio 中编译导出Jar库,提供函数供 Unity 调用;安卓端提供三个测试函数:弹出对话框弹出Toast手机振动;Unity端提供界面,添加三个按钮分别调用这三个安卓提供的函数

    2.编译环境

    Unity 4.3.4 + Android Studio 1.5( buildToolVersion:23.0.2)

    3.Android 部分

    1)新建工程

    新建一个 Android 工程例如叫 JarDemo ,然后依次点击:菜单栏中File -> New -> New Module -> 选择 Android Library -> 库的名称为 android2u3d,然后把layout下的activity_main.xml删掉,目前不需要这个布局文件。同时在MainActivity.java 的 onCreate 函数中将 setContentView(R.layout.activity_main); 删掉。

    2)修改配置

    Unity4.x安装目录EditorDataPlaybackEnginesandroidplayerin 下找到classes.jar 文件拷贝到JarDemoandroid2u3dlibs 目录下,然后依次点击菜单栏中File -> Project Structures -> 在左侧栏的Modules下点击 android2u3d ,右侧选择 Dependencies 标签页,点击右侧的加号按钮,选择File Dependency,然后在弹出的对话框中选择libs下classes.jar;或者直接修改这个库对应的build.gradle文件,在dependencies节点下添加一行:

    <code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">compile <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">files</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'libs/classes.jar'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

    3)添加代码

    现在开始需要修改 MainActivity.java 文件了,MainActivity需要继承自 UnityPlayerActivity

    <code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">UnityPlayerActivity</span> {</span>
    
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Context mContext = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;
    
        <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState);
            mContext = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>;
        }
    
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 定义一个显示对话框的方法,在Unity中调用此方法</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">ShowDialog</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String mTitle,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String mContent)
        {
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在UI线程下执行</span>
            runOnUiThread(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Runnable() {
                <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() {
                    AlertDialog.Builder mBuilder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> AlertDialog.Builder(MainActivity.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>);
                    mBuilder.setTitle(mTitle).setMessage(mContent).setPositiveButton(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Down"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>);
                    mBuilder.show();
                }
            });
        }
    
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 定义一个显示Toast的方法,在Unity中调用此方法</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">ShowToast</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String mStr2Show){
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 同样需要在UI线程下执行</span>
            runOnUiThread(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Runnable() {
                <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() {
                    Toast.makeText(getApplicationContext(),mStr2Show,Toast.LENGTH_LONG).show();
                }
            });
        }
    
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  定义一个手机振动的方法,在Unity中调用此方法</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">SetVibrator</span>(){
            Vibrator mVibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE);
            mVibrator.vibrate(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span>[]{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>}, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-1:表示不重复 0:循环的震动</span>
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li></ul>

    关于手机的振动,需要修改AndroidManifest.xml文件添加权限的:

    <code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.VIBRATE"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

    4)修改 build.gradle文件导出jar库

    打开 build.gradle(Module:android2u3d) 在文件后面追加以下代码:

    <code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">task makeJar(type: Copy) {
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">delete</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/libs/android2u3d.jar'</span>
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/intermediates/bundles/release/'</span>)
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">into</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/libs/'</span>)
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">include</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'classes.jar'</span>)
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">rename</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'classes.jar'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'android2u3d.jar'</span>)
    }
    
    makeJar.dependsOn(build)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

    然后在Terminal窗口中输入* gradlew makeJar* 来导出 android2u3d.jar;导出成功之后会有BUILD SUCCESSFUL 的提示! jar导出成功之后的物理位置在 JarDemoandroid2u3duildlibs

    TIPS: 
    A: 导出之前最好把不需要打包导出的东西统统干掉,比如 dependencies 下的 
    testCompile ‘junit:junit:4.12’ 
    compile ‘com.android.support:appcompat-v7:23.1.0’ 
    B: 以及Project -> Project -> JarDemo -> app -> src 下的 androidTest 和 test 这两个直接delect删掉。 
    C:第一次执行 gradlew makeJar 这个命令会下载一些东西不要关闭窗口,等他下载并配置完成之后再执行这个命令!

    4.Uniy 部分

    1)创建工程

    新建一个Unity的工程如叫 JarDemo,在 Assets 下Create Folder 名称为 Plugins ,在这个文件夹下继续创建一个 Android,然后再次在Android 下创建一个文件夹 bin ;然后将上边生成的 android2u3d.jar 拷贝到这个AssetsPluginsAndroidin 下,然后将安卓工程下JarDemoandroid2u3dsrcmain 中的AndroidManifest.xml 拷贝到 AssetsPluginsAndroid目录下。

    2)添加脚本

    添加C#脚本名称如AndroidAPI,然后添加如下代码:

    <code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> Update () {
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 返回键退出</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(Input.GetKey(KeyCode.Escape))
                Application.Quit();
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> OnGUI ()   
        {  
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通过API调用对话框  </span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(GUILayout.Button(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"调用安卓Jar中的函数 ShowDialog !"</span>,GUILayout.Height(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)))  
            {  
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Android的Java接口  </span>
                AndroidJavaClass jc=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> AndroidJavaClass(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.unity3d.player.UnityPlayer"</span>);  
                AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"currentActivity"</span>);  
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 参数  </span>
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span>[] mObject=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>];  
                mObject[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Jar4Android"</span>;  
                mObject[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wow,Amazing!It's worked!"</span>;  
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 调用方法  </span>
                jo.Call(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ShowDialog"</span>,mObject);  
            }  
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通过API调用Toast</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(GUILayout.Button(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"调用安卓Jar中的函数 ShowToast !"</span>,GUILayout.Height(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)))  
            { 
                AndroidJavaClass jc=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> AndroidJavaClass(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.unity3d.player.UnityPlayer"</span>);  
                AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"currentActivity"</span>); 
                jo.Call(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ShowToast"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Showing on Toast"</span>); 
            }
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通过API调用手机震动的方法</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(GUILayout.Button(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"调用安卓Jar中的函数 SetVibrator !"</span>,GUILayout.Height(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)))  
            { 
                AndroidJavaClass jc=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> AndroidJavaClass(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.unity3d.player.UnityPlayer"</span>);  
                AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"currentActivity"</span>); 
                jo.Call(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SetVibrator"</span>); 
            }
        } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute;  50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right- 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul>

    然后将这个 AndroidAPI.cs 拖动到 Main Camera 上进行绑定。

    3)真机调试

    需要先配置SDK,依次点击菜单栏Edit -> Preferences -> External Tools 在右侧下边的Android SDK Location 选择你的Android SDK所在目录。

    4)构建设置

    依次点击菜单栏 File -> Build Settings 在弹出的窗口中切换平台到 Android,右侧 Texture Compression选择 ETC1 or RGBA16(GLES 2.0),同时勾选Development buildAutoconnect profiler这两个复选框。

    点击Player Settings 在右侧的Inspector窗口下,常见的需要修改的 Bundle Identifier 等。 
    除此之外还需要修改下边那个Publishing Settings 中 keystore ,这里我们勾选 Create New KeyCode然后在keystore password 和 Confirm password 添加你设置的密码。

    5)开始调试

    OK,现在使用数据线连接安卓手机,允许调试后,点击Build And Run开始调试吧~

    5.分享一下源码做参考

    之前上传过一份源码到网盘,忘了在这里贴出来了!源码网盘地址

  • 相关阅读:
    JS替换字符串多余的空格符
    WebStorm2017.3.4版本 注册码
    使用$.getJSON()需要注意的地方
    45道CSS基础面试题(附答案)
    单机版搭建Hadoop环境图文教程详解
    Ubuntu下安装JDK图文详解
    VMtools安装以及设置
    Ubuntu 12.04中文输入法的安装
    Hadoop 学习之 FAQ
    java ant 命令大全
  • 原文地址:https://www.cnblogs.com/nafio/p/9137292.html
Copyright © 2020-2023  润新知