Dowemo
0 0 0 0

It looks like a lot of online blogs on unity and , and found lots of 's jar packages, unfortunately my computer 's eclipse is hung, and it's just!
First look at the effect of the rendering ( which isn't beautiful ). 了 )
Novel description: the first button is a method of calling android, returning the value.
The second button is called the toast message.
A third button is also a method of calling android, which is the way the android method is followed by a method called untiy ( here's a way to implement the unity

Here's the code for android

publicclassMainActivityextendsUnityPlayerActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //setContentView(R.layout.activity_main); }
 public String ShowDialog(final String _title, final String _content){
 return"Java return";
 }
 //定义一个显示Toast的方法,在Unity中调用此方法publicvoidShowToast(final String mStr2Show){
 //同样需要在UI线程下执行 runOnUiThread(new Runnable() {
 @Overridepublicvoidrun() {
 Toast.makeText(getApplicationContext(),mStr2Show, Toast.LENGTH_LONG).show();
 }
 });
 }
 //第一个参数是unity中的对象名字,记住是对象名字,不是脚本类名//第二个参数是函数名//第三个参数是传给函数的参数,目前只看到一个参数,并且是string的,自己传进去转吧publicvoidcallUnityFunc(String _objName, String _funcStr, String _content)
 {
 UnityPlayer.UnitySendMessage(_objName, _funcStr, "Come from:" + _content);
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

I need to explain here.
1. Mainactivity extends UnityPlayerActivity this is to inherit UnityPlayerActivity because the unity apk default is an activity.
2. The fi & t argument is the name of the object in unity, and remember that the object name isn't the script class name. I'm also looking at someone else 's blog, and actually the name of the object is gameobject, which will go to the method to call under this gameobject.

Next is c # code.

publicclass TestAndroidDemo : MonoBehaviour {
 public UILabel label;//显示的labelUI//Use this for initializationvoid Start () {
 }
 //Update is called once per framevoid Update () {
 }
 ///<summary>/// 调用安卓的对话框///</summary>publicvoidFirstButtonClick() 
 {
 //Android的Java接口  AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 //参数 string[] mObject = newstring[2];
 //调用方法 string ret = jo.Call<string>("ShowDialog", mObject);
 SetUILabelText(ref ret);
 }
 //调用安卓的ToastpublicvoidScendButtonClick()
 {
 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 jo.Call("ShowToast", "Showing on Toast");
 }
 ///<summary>/// 测试 unity->java->unity///</summary>publicvoidThirdButtonClick()
 {
 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 jo.Call("callUnityFunc", "Scripts", "BeCallFunc", "yangx");
 }
 //设置一个回掉方法privatevoidBeCallFunc(string _content)
 {
 SetUILabelText(ref _content);
 }
 privatevoidSetUILabelText(refstringvalue) {
 label.text = value;
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

I'm hanging on this script. I'm on the gameobject.
这里写图片描述

Through the entire code logic, the above process is clear, and then how to pack the problem of the craig file and some errors in this order, I also think that I. first of all, look at my tool version.
1, android studio 2.2.2
Jdk 1.8
sdk
这里写图片描述
Here's all the latest including android studio and sdk ( I'm not afraid of pit, ha ha )
Unity 2 is 5.4, of course packaged sdk and android studio.

All right, first create a new unity project, export the android development kit
这里写图片描述
This is the android directory that exports the package.
Open it with android studio, in the structure of project
这里写图片描述

Next, we write code to the mainactivity under src, which I've already got in the beginning of the code, which isn't repeated, and we put the class. Jar package in the libs directory, which is the path to the class. Jar: And D:UnityEditorDataPlaybackEnginesAndroidPlayerVariationsmonoReleaseClasses is my path, I'm installing untiy on the
For example, www. Com.

这里写图片描述

After you the library library import, you can find it in build. Gradle.

And then put my androidmainifest. Xml.

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.unitytest.lanjian.testunity"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@android:style/Theme.Black.NoTitleBar"><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter><!-- 一定要加上这句 --><meta-dataandroid:name="unityplayer.UnityActivity"android:value="true"/></activity><activityandroid:name=".Main2Activity"android:label="@string/title_activity_main2"></activity></application></manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Take a picture of what I want.
图标下文说明

1 this package and unity are consistent.
2 this change doesn't allow you to report an error that isn't found when the unity guide isn't found, and my solution is that.
3 must be this, I don't know why, please inform me.

Another one is to remove the original class. Jar when you export the arr package. Because the untiy driver will take one, it'll conflict ( you can also kill the unity of unity of unity, I'm trying to try it, and I'm sorry ).
( guide bulid package:>> bulid apk, directory address is open under your project 's appbuildoutputsaar, and no need to extract it, I'm using it.
这里写图片描述

1 open this file to delete the class. Jar.

We can open the values. Xml under the res file.
这里写图片描述
This picture should be very clear. It's an error.
这里写图片描述
It's the original theme that can be customized. I'm looking for a definition that couldn't be found in colorAccent. It's a three three lines, and it isn't an error. This may be a pit of android and unity.

Finally,
这里写图片描述

An androidmainifest package and can export the apk package.

It might be a bit confusing, and it's.
Finally provide the download address
http://download. Csdn. net/detail/sinat_23156865/9731010.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs