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安装目录\Editor\Data\PlaybackEngines\androidplayer\bin 下找到classes.jar
文件拷贝到JarDemo\android2u3d\libs 目录下,然后依次点击菜单栏中File
-> Project Structures -> 在左侧栏的Modules下点击 android2u3d ,右侧选择 Dependencies
标签页,点击右侧的加号按钮,选择File
,然后在弹出的对话框中选择libs下classes.jar;或者直接修改这个库对应的build.gradle文件,在
Dependencydependencies
节点下添加一行:
<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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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导出成功之后的物理位置在 JarDemo\android2u3d\build\libs
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
拷贝到这个Assets\Plugins\Android\bin 下,然后将安卓工程下JarDemo\android2u3d\src\main 中的AndroidManifest.xml
拷贝到 Assets\Plugins\Android目录下。
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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 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
选择 ETC1 or RGBA16(GLES 2.0),同时勾选
CompressionDevelopment build
和Autoconnect
这两个复选框。
profiler
点击Player Settings
在右侧的Inspector窗口下,常见的需要修改的 Bundle
等。
Identifier
除此之外还需要修改下边那个Publishing Settings
中 keystore ,这里我们勾选 Create New KeyCode然后在keystore
和
passwordConfirm password
添加你设置的密码。
5)开始调试
OK,现在使用数据线连接安卓手机,允许调试后,点击Build And Run
开始调试吧~
5.分享一下源码做参考
之前上传过一份源码到网盘,忘了在这里贴出来了!源码网盘地址
Unity 与 Android (Android Studio)的交互的更多相关文章
- Unity 3D与Android Studio安卓交互之-导出jar包
u3d与安卓 jar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...
- 在Android Studio上测试运行,Unity发布成Android包的环境搭建步骤
我用的是unity5.4.3版本,jdk是1.8.0_131版本,Android-SDK是r24.4.1版本,Android-NDK是r13b版本,作者是刚开始写博客,希望对在unity摸索的人有 ...
- java程序员图文并茂细说Unity中调用Android的接口
http://bbs.csdn.net/topics/391876421 最近做一个项目,为同事提供接口,能使他在Unity中调用Android中的函数来实现QQ登陆并获取用户信息.按照一些书上和一些 ...
- Unity3D 解决用Unity导出的Android工程在6.0及以上设备会弹出一串权限对话框的问题
解决用Unity导出的Android工程在6.0及以上设备会弹出一串权限对话框的问题 <meta-data android:name="unityplayer.SkipPermissi ...
- [转][Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
转自:http://blog.csdn.net/qiujuer/article/details/39754517?utm_source=tuicool [Android][Android Studi ...
- java攻城狮之路(Android篇)--与服务器交互
一.图片查看器和网页源码查看器 在输入地址的是不能输入127.0.0.1 或者是 localhost.ScrollView :可以看成一个滚轴 可以去包裹很多的控件在里面 练习1(图片查看器): pa ...
- Unity中加入Android项目的Build步骤
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介: 有的项目需要在Android中加入Unity功能,例如ANDROID应用中嵌入Un ...
- 3D语音天气球(源码分享)——在Unity中使用Android语音服务
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
- Android与JavaScrip进行交互(二)
一.思路分析 经过測试发现,JS中的点击事件仅仅能写一个,假设写了多个,也仅仅会响应第一个,假设写的方法是android端的方法,在web端执行时,后台会报没有定义这种方法的错误.前台点击无响应. 所 ...
- Android - 和其他APP交互 - 获得activity的返回值
启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...
随机推荐
- SAM4E单片机之旅——5、LED呼吸和PWM
PWM在高频情况下,一个很好的用处就是通过控制占空比来控制输出的功率,比如控制风扇转速.LED灯的亮度等.这次就利用PWM的中断功能,动态改变脉冲的占空比,来实现呼吸灯的效果. 一.实现思路 PWM可 ...
- Understanding Unicorn and unicorn-worker-killer Unicorn
We just wrote some new documentation on how Gitlab uses Unicorn and unicorn-worker-killer, available ...
- 解密阿里云Redis助力双十一背后的技术
摘要: Redis是一个使用范围很广的NOSQL数据库,阿里云Redis同时在公有云和阿里集团内部进行服务,本文介绍了阿里云Redis双11的一些业务场景:微淘社区之亿级关系链存储.天猫直播之评论商品 ...
- Juery插件-- jquery.cookie.js
1.引入jquery <script src="scripts/jquery-1.8.8.js" type="text/javascript">&l ...
- JavaWeb项目里面的路径获取方法总结
仅为资源搬运,个人还未充分理解... request.getRealPath不推荐使用request.getRealPath("") 这个方法已经不推荐使用了 request.ge ...
- 数据结构之 线性表---单链表操作A (删除链表中的指定元素)
数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...
- BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...
- codeforces A. Point on Spiral 解题报告
题目链接:http://codeforces.com/problemset/problem/279/A 题目意思:给出一个坐标点(x, y),问当从(0, 0) 开始到达该点转过的拐角有多少个.(拐角 ...
- 记一次关于return的错误
有时候瞎JB程序,调一天东改西改,都发现不了错:到最后弄出来发现就是那样一个SB错误,真不知道该笑还是该哭. 这个if语句中的return,如果加了那么判断了if语句成立后,下面就不再执行了. ...
- June 26,程序破解
1.android程序破解练习初级 方法一: 文件名:KeygenMe#1.apk工具:ApktoolGui v2.0 Final 先用ApktoolGui v2.0 Final反编译成java通过查 ...