因为手机游戏需要接入的平台又多又杂,所以我们在Unity内部写一个平台管理类,方便统一调用。

Unity调用安卓

Unity调用安卓一般使用AndroidJavaClass这个类,直接调用安卓中声明好的GameHelper类,并且Unity调用的一般都是GameHelper类里面的静态方法。

Unity - Scripting API: AndroidJavaClass

PlatMsgManager

在Assets——Scripts——Manager文件夹内新建PlatMsgManager

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
using UnityEngine;

public class PlatMsgManager : SingletonPattern<PlatMsgManager>
{
GameObject m_PlatformObject = null;
public PlatformScript m_PlatformScript = null;
public void Init()
{
if (m_PlatformObject == null)
{
m_PlatformObject = new GameObject("PlatFormObject");
m_PlatformObject.hideFlags = HideFlags.HideAndDontSave;
m_PlatformScript = m_PlatformObject.AddComponent<PlatformScript>();

#if UNITY_EDITOR
if (!Application.isPlaying)
{
return;
}
#endif
GameObject.DontDestroyOnLoad(m_PlatformObject);
}
#if UNITY_ANDROID && !UNITY_EDITOR
m_GameHelperJavaClass = new AndroidJavaClass("com.DefaultCompany.ResLoadPrg.GameHelper");
Debug.Log("Platform Init com.DefaultCompany.ResLoadPrg.GameHelper");
#endif
}


#if UNITY_ANDROID && !UNITY_EDITOR
private AndroidJavaClass m_GameHelperJavaClass = null;

/// <summary>
/// 从Unity发送消息到Android平台
/// </summary>
public void SendUnityMessageToPlatform(
int iMsgId,
int iParam1 = 0,
int iParam2 = 0,
int iParam3 = 0,
int iParam4 = 0,
string strParam1 = "",
string strParam2 = "",
string strParam3 = "",
string strParam4 = "")
{
if(m_GameHelperJavaClass == null) { return; }

m_GameHelperJavaClass.CallStatic(
"SendUnityMessageToPlatform",
iMsgId,
iParam1,
iParam2,
iParam3,
iParam4,
strParam1,
strParam2,
strParam3,
strParam4);
}
/// <summary>
/// 从平台获取int数据
/// </summary>
public int GetIntFromPlatform(int type)
{
if(m_GameHelperJavaClass == null) return 0;
return m_GameHelperJavaClass.CallStatic<int>("GetIntFromPlatform", type);
}
/// <summary>
/// 从平台获取long数据
/// </summary>
public long GetLongFromPlatform(int type)
{
if (m_GameHelperJavaClass == null) return 0;
return m_GameHelperJavaClass.CallStatic<long>("GetLongFromPlatform", type);
}
/// <summary>
/// 从平台获取long数据
/// </summary>
public long GetLongFromPlatform2(
int type,
int iParam1 = 0,
int iParam2 = 0,
int iParam3 = 0,
int iParam4 = 0,
string strParam1 = "",
string strParam2 = "",
string strParam3 = "",
string strParam4 = "")
{
if (m_GameHelperJavaClass == null) return 0;
return m_GameHelperJavaClass.CallStatic<long>(
"GetLongFromPlatform2",
type,
iParam1,
iParam2,
iParam3,
iParam4,
strParam1,
strParam2,
strParam3,
strParam4);
}
/// <summary>
/// 从平台获取string数据
/// </summary>
public string GetStringFromPlatform(int type)
{
if(m_GameHelperJavaClass == null) return null;
return m_GameHelperJavaClass.CallStatic<string>("GetStringFromPlatform", type);
}
#else
/// <summary>
/// 从Unity发送消息到Android平台,空函数放报错
/// </summary>
public void SendUnityMessageToPlatform(
int iMsgId,
int iParam1 = 0,
int iParam2 = 0,
int iParam3 = 0,
int iParam4 = 0,
string strParam1 = "",
string strParam2 = "",
string strParam3 = "",
string strParam4 = "")
{

}
/// <summary>
/// 从平台获取int数据,空函数放报错
/// </summary>
public int GetIntFromPlatform(int type)
{
return 0;
}
/// <summary>
/// 从平台获取long数据,空函数放报错
/// </summary>
public long GetLongFromPlatform(int type)
{
return 0;
}
/// <summary>
/// 从平台获取long数据,空函数放报错
/// </summary>
public long GetLongFromPlatform2(
int type,
int iParam1 = 0,
int iParam2 = 0,
int iParam3 = 0,
int iParam4 = 0,
string strParam1 = "",
string strParam2 = "",
string strParam3 = "",
string strParam4 = "")
{
return 0;
}
/// <summary>
/// 从平台获取string数据,空函数放报错
/// </summary>
public string GetStringFromPlatform(int type)
{
return string.Empty;
}
#endif
}

安卓、IOS调用Unity

安卓、IOS要调用Unity,需要Unity在运行时的scene内有一个GameObject,实时接收调用信息。

调用信息一般用Json打包,所以我们首先在Unity的Package Manager中下载Newtonsoft Json包,然后再在Unity工程中添加引用:Unity.Plastic.Newtonsoft.Json

在Assets——Scripts——Manager文件夹内新建PlatformScript

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

#if UNITY_ANDROID || UNITY_IOS
public class PlatformScript : MonoBehaviour
{
private struct PlatformMsg
{
public int iMsgId;
public int iParam1;
public int iParam2;
public int iParam3;
public string strParam1;
public string strParam2;
public string strParam3;
}

private const int PLATFORM_MSG_QQLOGINCALLBACK = 1;//QQ登录回调
private const int PLATFORM_MSG_WXLOGINCALLBACK = 2;//微信登录回调

private Queue<PlatformMsg> msgQueue = new Queue<PlatformMsg>();
protected void OnMessage(string param)//使用这个方法接收平台底层传过来的数据
{
PlatformMsg msg = JsonConvert.DeserializeObject<PlatformMsg>(param);
msgQueue.Enqueue(msg);
}
private void Update()
{
while (msgQueue.Count > 0)
{
PlatformMsg msg = msgQueue.Dequeue();
switch (msg.iMsgId)
{
case PLATFORM_MSG_QQLOGINCALLBACK:

break;
case PLATFORM_MSG_WXLOGINCALLBACK:
break;
default:
break;
}
}
}
}
#endif