前言
最近小编开始做关于手部康复的项目,需要Android集成Unity,以Android为主,Unity为辅的开发;上一篇给大家分享了Unity嵌入Android的操作过程,所以今天想给大家分享一下双端通信的知识;
一. Android与Unity哪个为主?
一般情况下,根据需求来决定Android与Unity的轻重,可以总结为以下两种
1. 将Unity作为Android程序中的一部分进行开发 ,将Unity3D场景当成一个界面或者说是界面的一部分
2. 将Android作为Unity程序中的一部分进行开发,将Android当作一个插件
小编这里由于项目需要,我是以Android为主,Unity辅助
二. Unity端调用Android端方法
实例化AndroidJavaClass对象,用于拿到Android的UnityClass和Activity要用到的接口
实例化AndroidJavaObject对象,是Unity发送消息给Android的关键
//获取Android中com.unity3d.player.UnityPlayer,这个类在Unity3d导出工程的unity-class.jar中
//这是通过反射的机制
1AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//获取Android当前正在运行的Activity
1AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
//调用Activity中的无返回值普通方法
1androidJavaObject.Call("Android方法名");
//调用Activity中的无返回值普通带参方法
1androidJavaObject.Call("Android方法名","参数");
//调用Activity中返回值String类的普通方法
1androidJavaObject.Call<string>("Android方法名");
//调用Activity中返回值String类的普通带参方法
1androidJavaObject.Call<string>("Android方法名","参数");
//调用Activity中返回值int类的普通方法
1androidJavaObject.Call<int>("Android方法名");
//调用Activity中返回值int类的普通带参方法
1androidJavaObject.Call<int>("Android方法名","参数");
//调用Activity中的无返回值静态方法
1androidJavaObject.GetStatic("Android方法名");
//调用Activity中的无返回值静态带参方法
1androidJavaObject.GetStatic("Android方法名","参数");
//调用Activity中返回值String类的静态方法
1androidJavaObject.GetStatic<string>("Android方法名");
//调用Activity中返回值String类的静态带参方法
1androidJavaObject.GetStatic<string>("Android方法名","参数");
//调用Activity中返回值int类的静态方法
1androidJavaObject.GetStatic<int>("Android方法名");
//调用Activity中返回值int类的静态带参方法
1androidJavaObject.GetStatic<int>("Android方法名","参数");
这里列举了一些Unity调用Android的一些初级方法,记住,这种方式的调用最多只能传递一个参数,下一篇将为大家分享高级用法
三. Android端调用Unity端方法
UnityPlayer.UnitySendMessage("Unity场景物体名","Unity脚本函数名","Unity脚本函数所需参数名");
UnityPlayer为Unity导出包,可以直接引用
UnitySendMessage()方法必须要传递三个参数
第一个参数为Unity场景中脚本挂载的游戏物体名
第二个参数为Unity脚本中的函数名
第三个参数为Unity脚本中函数所需要的参数,可以为空,但必须要有
这里只列举了双端通信的函数调用,可以满足Android与Unity之间参数不多的情况,如果需要大量参数传递的话要采用实现接口的方式在进行,双端通信接口之间的实现下一篇给大家分享
四. 双端通信实战演练
我给大家演示一个Android端控制Unity场景物体移动,并且Unity反过来控制Android端的一个Demo,希望大家能快速喜欢上Unity与Android的乐趣
1. Unity端
我在Unity的场景中创建一个Cube物体,在物体上挂载Move脚本,然后在场景中新建Canvas,Canvas下四个Button按钮,如下图所示
Move脚本:
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
84using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Move : MonoBehaviour { public Button btn_up; public Button btn_down; public Button btn_left; public Button btn_right; private AndroidJavaClass androidJavaClass; private AndroidJavaObject androidJavaObject; private void Start() { androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity"); btn_up.onClick.AddListener(AndroidUp); btn_down.onClick.AddListener(AndroidDown); btn_left.onClick.AddListener(AndroidLeft); btn_right.onClick.AddListener(AndroidRight); } public void Up() { transform.Translate(0, 0.5f, 0); Debug.Log("Android端调用了Unity场景Cube物体上的Move脚本中的Up方法"); } public void Down() { transform.Translate(0, -0.5f, 0); Debug.Log("Android端调用了Unity场景Cube物体上的Move脚本中的Down方法"); } public void Left() { transform.Translate(-0.5f, 0, 0); Debug.Log("Android端调用了Unity场景Cube物体上的Move脚本中的Left方法"); } public void Right() { transform.Translate(0.5f, 0 , 0); Debug.Log("Android端调用了Unity场景Cube物体上的Move脚本中的Right方法"); } public void AndroidUp() { androidJavaObject.Call("Up"); Debug.Log("Unity端调用了Android端的Up方法"); } public void AndroidDown() { androidJavaObject.Call("Down"); Debug.Log("Unity端调用了Android端的Down方法"); } public void AndroidLeft() { androidJavaObject.Call("Left"); Debug.Log("Unity端调用了Android端的Left方法"); } public void AndroidRight() { androidJavaObject.Call("Right"); Debug.Log("Unity端调用了Android端的Right方法"); } }
2. Android端
AndroidStudio新建空项目,按照我上一篇集成Unity的文章来操作,然后新建一个类,让这个类继承UntyPlayerActivity,具体步骤如下:
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235MainActivity.java: package com.example.test; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, AndroidToUnityActivity.class); startActivity(intent); } }); } } activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="进入Unity" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> AndroidToUnityActivity.java: package com.example.test; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import com.unity3d.player.UnityPlayer; public class AndroidToUnityActivity extends UnityPlayerActivity{ /** * 声明变量 */ private RelativeLayout Unity_View; private Button up_btn,left_btn,right_btn,down_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_androidtounity); /** * 为变量绑定控件 */ Unity_View = (RelativeLayout) findViewById(R.id.UnityView); up_btn = (Button) findViewById(R.id.btn1); left_btn = (Button) findViewById(R.id.btn2); right_btn = (Button) findViewById(R.id.btn3); down_btn = (Button) findViewById(R.id.btn4); /** * 将Unity视图添加到RelativeLayout中 */ Unity_View.addView(mUnityPlayer); up_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UnityPlayer.UnitySendMessage("Cube","Up",""); } }); left_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UnityPlayer.UnitySendMessage("Cube","Left",""); } }); right_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UnityPlayer.UnitySendMessage("Cube","Right",""); } }); down_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UnityPlayer.UnitySendMessage("Cube","Down",""); } }); } private void Up(){ UnityPlayer.UnitySendMessage("Cube","Up",""); } private void Down(){ UnityPlayer.UnitySendMessage("Cube","Down",""); } private void Left(){ UnityPlayer.UnitySendMessage("Cube","Left",""); } private void Right(){ UnityPlayer.UnitySendMessage("Cube","Right",""); } } activity_androidtounity.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/UnityView" android:layout_width="match_parent" android:layout_height="600dp"> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/btn1" android:text="上移" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/btn2" android:text="左移" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginHorizontal="20dp"/> <Button android:id="@+id/btn3" android:text="右移" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginHorizontal="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/btn4" android:text="上移" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
到此为止,以上就是我的全部源码,大家可以参考一下,要是有问题,我们可以评论区讨论一下
3. 演示效果
Android与Unity双端通信
如果有致力于Android+Unity的小伙伴私信联系我,我们一起交流平时项目中的疑问,一起拓展这片领域
————————————————
版权声明:本文为CSDN博主「小尹编码中...」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Ai1114/article/details/128386292
关注我获取更多知识或者投稿
最后
以上就是魁梧乌冬面最近收集整理的关于Unity与Android交互(双端通信)的全部内容,更多相关Unity与Android交互(双端通信)内容请搜索靠谱客的其他文章。
发表评论 取消回复