Unity3D与Android互相调用踩坑总结

来源:https://blog.csdn.net/kongxingxing/article/details/81119831

 

1.引入unity的 classes.jar包
本人安装unity-5.6.1版本,目录为D:\Program Files\unity-561\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar

若直接将classes.jar导入AndroidStudio项目,编译出aar,Unity编译会报错:

IOException: Failed to Move File / Directory from 'Temp/StagingArea\android-libraries\app-debug\classes.jar' to 'Temp/StagingArea\android-libraries\app-debug\libs\classes.jar'.
UnityEditor.Android.PostProcessor.Tasks.ProcessAAR.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry)
UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args)
UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTargetGroup targetGroup, BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.BuildReporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:186)
UnityEditor.HostView:OnGUI()
1
2
3
4
5
6
7
将classes.jar更名unity-classes.jar后又报如下错误:

CommandInvokationFailure: Unable to convert classes into dex format.
D:/Program Files/Java/jdk1.8.0_181\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="D:/AndroidSDK\tools" -Dfile.encoding=UTF8 -jar "D:\Program Files\unity-561\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar" -

stderr[
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/NativeLoader;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$10;

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:614)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:310)
at com.android.dx.command.dexer.Main.runDx(Main.java:288)
at com.android.dx.command.dexer.Main.main(Main.java:244)
at com.android.dx.command.Main.main(Main.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at SDKMain.main(SDKMain.java:130)
Caused by: java.lang.InterruptedException: Too many errors
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:606)
... 9 more
]
stdout[
processing archive D:\UnityWorkSpace\New Unity Project\Temp\StagingArea\android-libraries\app-debug\libs\.\classes.jar...
processing com/example/cyk/myapplication1/BuildConfig.class...
processing com/example/cyk/myapplication1/UnityActivity.class...
processing com/example/cyk/myapplication1/UnityActivity$1.class...
processing archive D:\UnityWorkSpace\New Unity Project\Temp\StagingArea\android-libraries\app-debug\libs\.\unity-classes.jar...
processing bitter/jnibridge/JNIBridge.class...
processing bitter/jnibridge/JNIBridge$a.class...
processing com/unity3d/player/NativeLoader.class...
processing com/unity3d/player/ReflectionHelper.class...
processing com/unity3d/player/ReflectionHelper$1.class...
processing com/unity3d/player/ReflectionHelper$a.class...
processing com/unity3d/player/UnityPlayer.class...
processing com/unity3d/player/UnityPlayer$1.class...
processing com/unity3d/player/UnityPlayer$10.class...
processing com/unity3d/player/UnityPlayer$1$1.class...
processing com/unity3d/player/UnityPlayer$11.class...
processing com/unity3d/player/UnityPlayer$12.class...
processing com/unity3d/player/UnityPlayer$12$1.class...
processing com/unity3d/player/UnityPlayer$13.class...
processing com/unity3d/player/UnityPlayer$14.class...
processing com/unity3d/player/UnityPlayer$15.class...
processing com/unity3d/player/UnityPlayer$16.class...
processing com/unity3d/player/UnityPlayer$17.class...
processing com/unity3d/player/UnityPlayer$18.class...
processing com/unity3d/player/UnityPlayer$19.class...
processing com/unity3d/player/UnityPlayer$2.class...
processing com/unity3d/player/UnityPlayer$20.class...
processing com/unity3d/player/UnityPlayer$21.class...
processing com/unity3d/player/UnityPlayer$3.class...
processing com/unity3d/player/UnityPlayer$4.class...
processing com/unity3d/player/UnityPlayer$5.class...
processing com/unity3d/player/UnityPlayer$6.class...
processing com/unity3d/player/UnityPlayer$7.class...
processing com/unity3d/player/UnityPlayer$8.class...
processing com/unity3d/player/UnityPlayer$9.class...
processing com/unity3d/player/UnityPlayer$a.class...
processing com/unity3d/player/UnityPlayer$b.class...
processing com/unity3d/player/UnityPlayer$c.class...
processing com/unity3d/player/UnityPlayer$c$1.class...
processing com/unity3d/player/UnityPlayer$d.class...
processing com/unity3d/player/UnityPlayerActivity.class...
processing com/unity3d/player/UnityPlayerNativeActivity.class...
processing com/unity3d/player/UnityPlayerProxyActivity.class...
processing com/unity3d/player/UnityWebRequest.class...
processing com/unity3d/player/WWW.class...
processing com/unity3d/player/a.class...
processing com/unity3d/player/a$a.class...
processing com/unity3d/player/b.class...
processing com/unity3d/player/b$1.class...
processing com/unity3d/player/b$2.class...
processing com/unity3d/player/b$3.class...
processing com/unity3d/player/b$4.class...
processing com/unity3d/player/c.class...
processing com/unity3d/player/d.class...
processing com/unity3d/player/e.class...
processing com/unity3d/player/f.class...
processing com/unity3d/player/f$1.class...
processing com/unity3d/player/g.class...
processing com/unity3d/player/h.class...
processing com/unity3d/player/h$1.class...
processing com/unity3d/player/h$2.class...
processing com/unity3d/player/h$3.class...
processing com/unity3d/player/i.class...
processing com/unity3d/player/i$1.class...
processing com/unity3d/player/i$a.class...
processing com/unity3d/player/j.class...
processing com/unity3d/player/k.class...
processing com/unity3d/player/l.class...
processing com/unity3d/player/l$a.class...
processing com/unity3d/player/m.class...
processing com/unity3d/player/m$a.class...
processing com/unity3d/player/m$b.class...
processing org/fmod/FMODAudioDevice.class...
processing org/fmod/a.class...
processing bin\classes\.\com\example\cyk\myapplication1\R.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$attr.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$drawable.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$id.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$layout.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$mipmap.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$string.class...
processing bin\classes\.\com\example\cyk\myapplication1\R$style.class...
processing archive bin\classes.jar...
processing bitter/jnibridge/JNIBridge.class...
processing bitter/jnibridge/JNIBridge$a.class...
processing com/unity3d/player/NativeLoader.class...
processing com/unity3d/player/ReflectionHelper.class...
processing com/unity3d/player/ReflectionHelper$1.class...
processing com/unity3d/player/ReflectionHelper$a.class...
processing com/unity3d/player/UnityPlayer.class...
processing com/unity3d/player/UnityPlayer$1.class...
processing com/unity3d/player/UnityPlayer$10.class...
processing com/unity3d/player/UnityPlayer$1$1.class...
processing com/unity3d/player/UnityPlayer$11.class...
processing com/unity3d/player/UnityPlayer$12.class...
processing com/unity3d/player/UnityPlayer$12$1.class...
processing com/unity3d/player/UnityPlayer$13.class...
processing com/unity3d/player/UnityPlayer$14.class...
processing com/unity3d/player/UnityPlayer$15.class...
processing com/unity3d/player/UnityPlayer$16.class...
processing com/unity3d/player/UnityPlayer$17.class...
processing com/unity3d/player/UnityPlayer$18.class...
processing com/unity3d/player/UnityPlayer$19.class...
processing com/unity3d/player/UnityPlayer$2.class...
processing com/unity3d/player/UnityPlayer$20.class...
processing com/unity3d/player/UnityPlayer$21.class...
processing com/unity3d/player/UnityPlayer$3.class...
processing com/unity3d/player/UnityPlayer$4.class...
processing com/unity3d/player/UnityPlayer$5.class...
processing com/unity3d/player/UnityPlayer$6.class...
processing com/unity3d/player/UnityPlayer$7.class...
processing com/unity3d/player/UnityPlayer$8.class...
processing com/unity3d/player/UnityPlayer$9.class...
processing com/unity3d/player/UnityPlayer$a.class...
processing com/unity3d/player/UnityPlayer$b.class...
processing com/unity3d/player/UnityPlayer$c.class...
processing com/unity3d/player/UnityPlayer$c$1.class...
processing com/unity3d/player/UnityPlayer$d.class...
processing com/unity3d/player/UnityPlayerActivity.class...
processing com/unity3d/player/UnityPlayerNativeActivity.class...
processing com/unity3d/player/UnityPlayerProxyActivity.class...
processing com/unity3d/player/UnityWebRequest.class...
processing com/unity3d/player/WWW.class...
processing com/unity3d/player/a.class...
processing com/unity3d/player/a$a.class...
processing com/unity3d/player/b.class...
processing com/unity3d/player/b$1.class...
processing com/unity3d/player/b$2.class...
processing com/unity3d/player/b$3.class...
processing com/unity3d/player/b$4.class...
processing com/unity3d/player/c.class...
processing com/unity3d/player/d.class...
processing com/unity3d/player/e.class...
processing com/unity3d/player/f.class...
processing com/unity3d/player/f$1.class...
processing com/unity3d/player/g.class...
processing com/unity3d/player/h.class...
processing com/unity3d/player/h$1.class...
processing com/unity3d/player/h$2.class...
processing com/unity3d/player/h$3.class...
processing com/unity3d/player/i.class...
processing com/unity3d/player/i$1.class...
processing com/unity3d/player/i$a.class...
processing com/unity3d/player/j.class...
processing com/unity3d/player/k.class...
processing com/unity3d/player/l.class...
processing com/unity3d/player/l$a.class...
processing com/unity3d/player/m.class...
processing com/unity3d/player/m$a.class...
processing com/unity3d/player/m$b.class...
processing org/fmod/FMODAudioDevice.class...
processing org/fmod/a.class...
]
exit code: 2
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandInternal (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandSafe (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.HostView:OnGUI()
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
解决方法:
更改libs文件名,如unitylibs,classes.jar不用改名或者取任意名称即可,如图


2.AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cyk.myapplication1">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".UnityActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--固定写法-->
<meta-data android:name="unityplayer.UnityActivity"
android:value="true"/>
</activity>
</application>

</manifest>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
3.Android Activity设置

package com.example.cyk.myapplication1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class UnityActivity extends UnityPlayerActivity {

private LinearLayout mLlUnityContainer;
private Button mBtnRotate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unity);
initView();
}

private void initView() {
mLlUnityContainer = (LinearLayout) findViewById(R.id.ll_unity_container);
mBtnRotate = (Button) findViewById(R.id.btn_rotate);

//将Unity的View添加到布局里
View scene = mUnityPlayer.getView();
mLlUnityContainer.addView(scene);

mBtnRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//向Unity的Cube对象上的脚本里的RotateCube方法发送消息
//第三个参数是传递的消息
UnityPlayer.UnitySendMessage("Cube", "RotateCube", "80");
}
});
}

public void getNowTime(String temp) {
long time = System.currentTimeMillis();
String strTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINESE).format(new Date(time));
Toast.makeText(this,temp+" haha "+strTime,Toast.LENGTH_LONG).show();
}
}
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
51
52
4.将aar及AndroidManifest.xml文件导入到unity项目中
打开unity项目文件夹,目录不存在则创建相应文件夹,Assets\Plugins\Android,导入后效果如下

 

5.创建C#Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateTest : MonoBehaviour
{

// Use this for initialization
void Start()
{
Debug.Log("RotateTest Start");
}

// Update is called once per frame
void Update()
{
//Debug.Log("RotateTest Update");
}

void RotateCube(string angle)
{
Debug.Log("RotateTest RotateCube angle:" + angle);
this.gameObject.transform.Rotate(Vector3.up, float.Parse(angle));
}

public void unityBtnClick()
{
Debug.Log("RotateTest unityBtnClick");
//通过报名获取java class (固定写法,好坑,不要以为是自已的Android包名)
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

//获取当前的activity (固定写法,不要以为是自己定义的Activity名称。。。)
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

//调用activity里面的方法,传入方法名和参数
jo.Call("getNowTime", "Get Android Time");
}

}
————————————————
版权声明:本文为CSDN博主「德星」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kongxingxing/article/details/81119831

 

上一篇:遍历Java中的HashMap


下一篇:java-我可以将参数传递给动态创建的类吗?