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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
#if !DISABLE_PLUGIN
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class QGameKit {
public enum CaptureType {
AudioCapture = 1 << 0, //采集麦克风声音
AudioApolloVoice = 1 << 1, //Apollo Voice组件提供声音
AudioCustom = 1 << 2, //自定义声音数据
VideoCapture = 1 << 3, //SDK录制视频数据
VideoCustom = 1 << 4, //自定义视频数据
}
public enum LoginPlatform {
Guest,
QQ,
WeChat,
}
public enum LiveStatus {
Unknown, // 未知状态(在获取状态过程中发生了错误)
Uninitialized, // 尚未初始化
Prepared, // 已准备好
LiveStarting, // 直播开启中
LiveStarted, // 直播已开始
LivePaused, // 直播已暂停
LiveResume, // 暂停恢复中
LiveStopping, // 直播结束中
LiveStopped, // 直播已结束
Error // 直播过程出错
}
public enum CommentType {
Normal, // 普通消息
System, // 系统消息
Anchor, // 主播消息
RoomManager, // 房管消息
LiveManager, // 直播管理员消息
SuperManager, // 超管消息
Edit, // 小编消息
Gift, // 礼物消息
Welcome, // 用户进入消息
Vip, // VIP 用户消息
LoginVisitor // 登录用户消息
}
public enum Environment {
Release, //正式环境
Debug //测试环境
}
public enum WebViewStatus
{
Open,
Closed,
SmallWindow,
FullScreen
}
// 用户登录信息
public class UserAccount {
public LoginPlatform platform;
public string appId; // AppID:手Q或微信分享的AppID,必填
public string id; // openId:手Q或微信登录的Open ID,必填
public string token; // accessToken:必填
public string phoneNum; // 可选: Phone number
public long expires; // 可选: expires timestamp(ms)
}
// 直播评论信息
public class LiveComment {
public CommentType type; // 评论类型
public string nick; // 用户昵称
public string content; // 评论内容
public long timestamp; // 评论的产生时间
}
public class ShareContent {
public string fopenId; // 可选:若指定则直接发起后端分享,否则拉起手Q/微信分享
public string title; // 分享标题
public string description; // 分享描述
public string targetUrl; // 分享链接
public string imageUrl; // 分享消息缩略图 URL
}
public delegate UserAccount UserAccountDelegate(); // 用户信息委托:SDK 需要用户登录态时会回调委托,游戏应返回有效的用户信息
public delegate void CommentReceiveDelegate(List<LiveComment> comments); // 直播评论委托:直播过程中SDK会定时调用委托并将评论传入
public delegate void LogDelegate(string log); // SDK日志落入游戏日志记录体系 Delegate,可选择使用
public delegate void LiveStatusChangedDelegate(LiveStatus newState); // 直播状态变化委托,SDK通过它将直播状态变化通知游戏
public delegate void ShareDelegate(ShareContent shareContent); // 分享委托,SDK通过委托调用游戏分享能力
public delegate void ErrorCodeListenerDelegate(int errorCode, string errorMessage); // 直播过程中的错误监听委托
public delegate void WebViewStatusChangedDelegate(WebViewStatus status); //直播大厅在游戏中时,状态回调
public static LiveStatus liveStatus = LiveStatus.Uninitialized;
#if !UNITY_EDITOR
#if UNITY_ANDROID
private static QGameKitAndroidBridge QGameKitObj; // Android Native 桥接对象
#elif UNITY_IPHONE
private static QGameKitiOSBridge QGameKitObj; // iOS Native 桥接对象iOS 的桥接对象
#endif
#endif
// 初始化SDK,状态流转:Uninitialized -> Prepared,直播过程中出现任何错误会导致状态流转到 Error
// 参数 gameId 为企鹅电竞为游戏分配的唯一标识,accountDelegate 为用户信息回调,SDK会调用该委托获取游戏的用户登录态信息
public static bool Setup(string gameId, string wnsAppId, CaptureType captureType, UserAccountDelegate accountDelegate, Environment env) {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj = QGameKitAndroidBridge.Setup(gameId, wnsAppId, captureType, accountDelegate, env);
if (null == QGameKitObj)
{
Debug.LogError("QGameKitObj init failed!");
return false;
}
#elif UNITY_IPHONE && !UNITY_EDITOR
QGameKitObj = QGameKitiOSBridge.Setup ();
QGameKitObj.accountDelegate = accountDelegate;
_QGameSetup (gameId, wnsAppId, (int)captureType, (int)env);
#endif
UserAccount account = accountDelegate();
UpdateUserAccount(account);
liveStatus = LiveStatus.Prepared;
return true;
}
// 反初始化,状态流转:Setup -> Uninitialized
public static void TearDown() {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.TearDown();
#endif
liveStatus = LiveStatus.Uninitialized;
}
// 使用之前已配置的信息重新 Setup,状态流转:Error/LiveStopped -> Setup
public static bool Reset() {
#if UNITY_ANDROID && !UNITY_EDITOR
return QGameKitObj.Reset();
#endif
#if UNITY_IOS && !UNITY_EDITOR
return _QGameReset();
#endif
liveStatus = LiveStatus.Prepared;
return false;
}
// 游戏每帧刷新时调用通知 SDK 采集画面
public static void FrameUpdated() {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.CaptureFrame();
#endif
}
// 进入直播大厅
public static void EnterLiveHall() {
#if UNITY_IPHONE && !UNITY_EDITOR
_QGameEnterLiveHall ();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.EnterLiveHall ();
#endif
}
public static void DisabledLiveHall() {
#if UNITY_IPHONE && !UNITY_EDITOR
_QGameDisabledLiveHall ();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
#endif
}
// 进入直播大厅
public static void EnterLiveHallInGame() {
#if UNITY_IPHONE && !UNITY_EDITOR
_QGameEnterLiveHall ();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.EnterLiveHallInGame ();
#endif
}
// 开始直播,状态流转:Setup -> LiveStarted
public static bool StartLiveBroadcast(string title, string description) {
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj.StartLiveBroadcast(title, description))
{
return true;
} else {
Debug.LogError("StartLiveBroadcast failed!");
return false;
}
#endif
#if UNITY_IPHONE && !UNITY_EDITOR
return _QGameStartLiveBroadcast (title, description);
#endif
return false;
}
// 结束直播,状态流转:LiveStarted -> LiveStopped
public static bool StopLiveBroadcast() {
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj.StopLiveBroadcast())
{
return true;
} else {
Debug.LogError("StopLiveBroadcast failed!");
return false;
}
#endif
#if UNITY_IPHONE && !UNITY_EDITOR
return _QGameStopLiveBroadcast ();
#endif
return false;
}
// 暂定直播,状态流转:LiveStarted -> LivePaused
public static void PauseLiveBroadcast() {
Debug.LogError("PauseLiveBroadcast unsupported yet!");
}
// 恢复直播,状态流转:LivePaused -> LiveStarted
public static void ResumeLiveBroadcast() {
Debug.LogError("ResumeLiveBroadcast unsupported yet!");
}
//分享直播
public static void ShareLiveBroadcast() {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.ShareLiveBroadcast();
#elif UNITY_IOS && !UNITY_EDITOR
_QGameShareLiveBroadcast();
#endif
}
// 获取当前直播状态
public static LiveStatus GetLiveBroadcastStatus() {
#if UNITY_ANDROID && !UNITY_EDITOR
return QGameKitObj.GetLiveBroadcastStatus();
#endif
#if UNITY_IOS && !UNITY_EDITOR
return (LiveStatus)_QGameGetLiveBroadcastStatus ();
#endif
return LiveStatus.LiveStopped;
}
// 获取当前直播状态
public static int GetErrorCode() {
#if UNITY_ANDROID && !UNITY_EDITOR
return QGameKitObj.GetErrorCode();
#elif UNITY_IOS && !UNITY_EDITOR
return _QGameGetErrorCode();
#endif
return 0;
}
// 主动更新用户信息(游戏里用户注销重新登录后主动调用)
public static void UpdateUserAccount(UserAccount account) {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.UpdateUserAccount(account);
#elif UNITY_IOS && !UNITY_EDITOR
_QGameUpdateUserAccount(account.appId, account.id, account.token, (int)account.platform);
#endif
}
// 切换帐号时通知调用
public static void UpdateUserAccount() {
#if UNITY_ANDROID && !UNITY_EDITOR
QGameKitObj.UpdateUserAccount();
#endif
}
// 往当前自己的直播发送评论
public static void SendComment(LiveComment comment) {
Debug.LogError("SendComment unsupported yet!");
}
// 设置直播评论 Delegate
public static void SetCommentReceiveDelegate(CommentReceiveDelegate commentDelegate) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetCommentReceiveDelegete(commentDelegate);
# elif UNITY_IPHONE
QGameKitObj.commentDelegate = commentDelegate;
#endif
#endif
}
// 设置日志委托,让SDK的日志有机会跟随游戏的日志落地
public static void SetLogDelegate(LogDelegate logDelegate) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetLogDelegate(logDelegate);
# elif UNITY_IPHONE
QGameKitObj.logDelegate = logDelegate;
#endif
#endif
}
// 设置分享委托,让 SDK 能够使用游戏的分享能力
public static void SetShareDelegate(ShareDelegate shareDelegate) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetShareDelegate(shareDelegate);
#elif UNITY_IPHONE
QGameKitObj.shareDelegate = shareDelegate;
_QGameSetShareDelegateEnabled(shareDelegate != null ? true : false);
#endif
#endif
}
// 设置弹幕面板是否启用,默认不启用
public static void SetDanmakuEnabled(bool enabled) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetDanmakuEnabled(enabled);
#elif UNITY_IPHONE
_QGameSetDanmakuEnabled(enabled);
#endif
#endif
}
//Android弹幕面板会遮挡分享 这里添加显示隐藏的接口
public static void ShowDanmaku() {
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return;
}
QGameKitObj.ShowDanmaku();
#endif
}
public static void HideDanmaku() {
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return;
}
QGameKitObj.HideDanmaku();
#endif
}
// 游戏通知 SDK 发起分享的结果,result = 0 为分享成功否则为失败
public static void SetShareResult(ShareContent content, int result) {
}
// 设置直播状态 Delegate,如果游戏需要实时获得直播状态请注册 delegate
public static void SetLiveStatusDelegate(LiveStatusChangedDelegate liveStatusDelegate) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetLiveStatusChangedDelegate(liveStatusDelegate);
# elif UNITY_IPHONE
QGameKitObj.liveStatusDelegate = liveStatusDelegate;
#endif
#endif
}
public static void SetErrorCodeDelegate(ErrorCodeListenerDelegate errorCodeDelegate) {
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetErrorCodeDelegate(errorCodeDelegate);
#elif UNITY_IPHONE
QGameKitObj.errorCodeDelegate = errorCodeDelegate;
#endif
#endif
}
public static void SetWebViewStatusChangedDelegate(WebViewStatusChangedDelegate webviewDelegate)
{
#if !UNITY_EDITOR
#if UNITY_ANDROID
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.SetWebViewStatusChangedDelegate(webviewDelegate);
#elif UNITY_IPHONE
#endif
#endif
}
public static bool IsLiveBroadcastSupported()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return false;
}
return QGameKitObj.IsLiveBroadcastSupported();
#endif
return true;
}
public static string GetVersionName()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return null;
}
return QGameKitObj.GetVersionName();
#endif
return null;
}
public static bool ShowCamera()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return false;
}
return QGameKitObj.ShowCamera();
#endif
return false;
}
public static void HideCamera()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.HideCamera();
#endif
}
public static void DoOnResume()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.DoOnResume();
#endif
}
public static void DoOnPause()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.DoOnPause();
#endif
}
public static void DoOnDestroy()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return ;
}
QGameKitObj.DoOnDestroy();
#endif
}
public static bool DoOnBackPressed()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return false;
}
return QGameKitObj.DoOnBackPressed();
#endif
return false;
}
public static bool IsSupportCamera()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return false;
}
return QGameKitObj.IsSupportCamera();
#endif
return false;
}
public static bool IsSupportLiveHall()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (QGameKitObj == null)
{
Debug.LogError("QGameKitObj is null!");
return false;
}
return QGameKitObj.IsSupportLiveHall();
#endif
return true;
}
#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport ("__Internal")]
private static extern void _QGameSetup(string gameId, string wnsAppId, int captureType, int env);
[DllImport ("__Internal")]
private static extern void _QGameTearDown();
[DllImport ("__Internal")]
private static extern bool _QGameReset();
[DllImport ("__Internal")]
private static extern void _QGameEnterLiveHall();
[DllImport ("__Internal")]
private static extern bool _QGameStartLiveBroadcast(string title, string description);
[DllImport ("__Internal")]
private static extern bool _QGameStopLiveBroadcast();
[DllImport ("__Internal")]
private static extern void _QGamePauseLiveBroadcast();
[DllImport ("__Internal")]
private static extern void _QGameResumeLiveBroadcast();
[DllImport ("__Internal")]
private static extern void _QGameUpdateUserAccount(string appId, string openId, string accessToken, int loginType);
[DllImport ("__Internal")]
private static extern int _QGameGetErrorCode();
[DllImport ("__Internal")]
private static extern int _QGameGetLiveBroadcastStatus();
[DllImport ("__Internal")]
private static extern void _QGameShareLiveBroadcast();
[DllImport ("__Internal")]
private static extern void _QGameDisabledLiveHall();
[DllImport ("__Internal")]
private static extern void _QGameSetShareDelegateEnabled(bool enabled);
[DllImport ("__Internal")]
private static extern void _QGameSetDanmakuEnabled(bool enabled);
#endif
}
#endif
|