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()) ...
随机推荐
- 第 2 章 第 9 题 顺序 & 二分搜索效率分析问题
问题分析 顺序搜索的时间复杂度是O( n ),二分搜索的时间复杂度级别是O( lgn ).但这并不代表二分的时间开销就一定比顺序的小,因为二分搜索有个前提:元素必须要是有序的.如果仅仅为了二分搜索几个 ...
- Android-可随意拖动的View
http://blog.csdn.net/w8320273/article/details/8101687
- Eliminates these repeated computation in multi aggregations query
https://github.com/pingcap/tispark/commit/dcca23bfa1aa0c356a4280d82bc8301c0de08318 scala> spark.s ...
- SAP 系统账期开关
(1)OB52 财务账期-C 财务维护表 T001B[维护表T001B] (2)OB29 -C FI 财政年变式 (3)MMPV / MMRV -物料账期 MMPV 商品会计期间设置-结帐期间 [ 如 ...
- 扩散(diffusion)和弥散(dispersion)有什么区别
作者:谢易正链接:https://www.zhihu.com/question/23914350/answer/177359196来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Java线程面试题 Top 50(转载)
原文链接:http://www.importnew.com/12773.html 本文由 ImportNew - 李 广 翻译自 javarevisited 不管你是新程序员还是老手,你一定在面试中遇 ...
- 关于VLOOKUP函数的用法
“Lookup”的汉语意思是“查找”,在Excel中与“Lookup”相关的函数有三个:VLOOKUP.HLOOKUO和LOOKUP.下面介绍VLOOKUP函数的用法. 一.功能 在表格的首列查找指定 ...
- Partition算法及Partition算法用于快速排序
JavaScript简单方便,所以用JavaScript实现,可以在Chrome控制台下观察运行结果.主要实现Partition算法,比如输入为 var array = [4, 2, 1, 3, ...
- 值域线段树 bzoj 4627
这是题目链接4627: [BeiJing2016]回转寿司 题目大意: 给定n个数,求有多少个字段和在 满足 L<=sum<=R; 解题思路 需要解这个题目,需要有线段树加可持续化的思想, ...
- hdu-5621 KK's Point(dp+数学)
题目链接: KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...