GameHelper

在app——src——main——java——com.DefaultCompany.ResLoadPrg文件夹内新建Java代码文件,并命名为GameHelper

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
package com.DefaultCompany.ResLoadPrg;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

import org.json.JSONException;
import org.json.JSONObject;

public class GameHelper {
private static MainActivity m_Activity = null;
private static final String m_PlatformObject = "PlatformObject"; //Unity内用来通信的GO
private static final String m_MethodName = "OnMessage";//Unity用于接收通信的方法名称
public static String TAG = "GameHelper";//SDK接入时无法直接调试,只能靠Log

//初始化方法
public static void Init(MainActivity activity){
m_Activity = activity;
}

public static final int PLATFORM_MSG_QQLOGINCALLBACK = 1;//登录QQ回调
public static final int PLATFORM_MSG_WXLOGINCALLBACK = 2;//登录微信回调
//android发送消息到Unity
public static void SendPlatformMessageToUnity(
int iMsgId,
int iParam1,
int iParam2,
int iParam3,
String strParam1,
String strParam2,
String strParam3){
String jsonString = GetJsonStr(iMsgId,iParam1,iParam2,iParam3,strParam1,strParam2,strParam3);
UnityPlayer.UnitySendMessage(m_PlatformObject,m_MethodName,jsonString);
}

public static final int PLATFORM_MSG_QQLOGIN = 1;//QQ登录
public static final int PLATFORM_MSG_QQLOGOUT = 2;//QQ注销
public static final int PLATFORM_MSG_WXLOGIN = 3;//微信登录
public static final int PLATFORM_MSG_WXLOGOUT = 4;//微信注销
//Unity发送消息到Android
public static void SendUnityMessageToPlatform(
int iMsgId,
int iParam1,
int iParam2,
int iParam3,
int iParam4,
String strParam1,
String strParam2,
String strParam3,
String strParam4
){

Log.d(TAG,"SendUnityMessageToPlatform: iMsgId: " + iMsgId + "\n" +
"iParam1" + iParam1 + "\n" +
"iParam2" + iParam2 + "\n" +
"iParam3" + iParam3 + "\n" +
"iParam4" + iParam4 + "\n" +
"strParam1" + strParam1 + "\n" +
"strParam2" + strParam2 + "\n" +
"strParam3" + strParam3 + "\n" +
"strParam4" + strParam4);

if(m_Activity == null)
{
Log.e(TAG,"m_Activity is null");
}
switch (iMsgId){
case PLATFORM_MSG_QQLOGIN:
TencentQQ.Login();
break;
case PLATFORM_MSG_QQLOGOUT:
TencentQQ.Logout();
break;
case PLATFORM_MSG_WXLOGIN:
break;
case PLATFORM_MSG_WXLOGOUT:
break;
}
}
//Unity从Android获取int
public static int GetIntFromPlatform(int type){
switch (type){

}
return 0;
}
public static final int PLATFORM_MSG_AUTHORVALID = 1;
public static final int PLATFORM_MSG_REFRESHSESSION = 2;
//Unity从Android获取String
public static String GetStringFromPlatform(int type){
switch (type){
case PLATFORM_MSG_AUTHORVALID:
return String.valueOf(TencentQQ.CheckAuthorValid());
case PLATFORM_MSG_REFRESHSESSION:
return TencentQQ.RefreshSession().toString();
}
return "";
}
//Unity从Android获取long
public static long GetLongFromPlatform(int type){
switch (type){

}
return 0;
}
//Unity从Android获取long带参数
public static long GetLongFromPlatform2(
int type,
int iParam1,
int iParam2,
int iParam3,
int iParam4,
String strParam1,
String strParam2,
String strParam3,
String strParam4
){
switch (type){

}
return 0;
}
//通过json对象构造字符串
public static String GetJsonStr(
int iMsgId,
int iParam1,
int iParam2,
int iParam3,
String strParam1,
String strParam2,
String strParam3){
try {
JSONObject object = new JSONObject();
object.put("iMsgId",iMsgId);
object.put("iParam1",iParam1);
object.put("iParam2",iParam2);
object.put("iParam3",iParam3);
object.put("strParam1",strParam1);
object.put("strParam2",strParam2);
object.put("strParam3",strParam3);
return object.toString();
}
catch (Exception e){
Log.e(TAG,"Json error" + e.toString());
}
return "";
}
}

Android Studio快速注释快捷键1:CTRL + /

Android Studio快速注释快捷键2:CTRL + SHIFT + /

QQ第一次登录流程调用链:

Unity——SendUnityMessageToPlatform——(在安卓底层)TencentQQ.Login——LoginCallBack——在回调内使用GameHelper.SendPlatformMessageToUnity将统一化的json文件返回给Unity

QQ重复登录流程调用链:

Unity——GetStringFromPlatform——(在安卓底层)TencentQQ.CheckAuthorValid()返回票据是否过期

如果上一步返回的是true,则Unity——GetStringFromPlatform——(在安卓底层)TencentQQ.RefreshSession().toString()返回统一化json文件

MainActivity初始化GameHelper

1
2
3
4
5
6
7
8
9
public class MainActivity extends UnityPlayerActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameHelper.Init(this);//+++
}

}

获取安卓系统内存

修改GameHelperGetLongFromPlatform方法

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
//Unity从Android获取long
public static long GetLongFromPlatform(int type){
switch (type){
case 1://获取当前安卓可用内存
ActivityManager am = (ActivityManager) m_Activity.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
case 2://总内存
return GetTotalMemory();
case 3://apk运行内存
Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
return memoryInfo.getTotalPss() * 1024;

}
return 0;
}
//通过读取内核文件来获取设备的总内存
protected static long GetTotalMemory(){
long tm = 0;
try{
RandomAccessFile reader = new RandomAccessFile("/proc/meminfo","r");
String load = reader.readLine();
reader.close();

String[] totrm = load.split("kB");
String[] trm = totrm[0].split("");
tm = Long.parseLong(trm[trm.length - 1]) * 1024;

}
catch (IOException e){
e.printStackTrace();
}
return tm;
}