-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameSDKManager.cs
140 lines (120 loc) · 4.53 KB
/
GameSDKManager.cs
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
using System;
using UnityEngine.SceneManagement;
using TapSDK.Login;
using TapSDK.Compliance;
using TapSDK.Core;
using System.Collections.Generic;
/// <summary>
/// SDK 初始化及合规认证回调处理管理类
/// </summary>
public sealed class GameSDKManager
{
// 游戏在 TapTap 开发者中心对应的 Client ID
private readonly string clientId = "游戏的 Client ID";
// 游戏在 TapTap 开发者中心对应的 Client Token
private readonly string clientToken = "游戏的 Client Token";
// 异常事件类型-适龄限制
public const int EVENT_TYPE_AGE_RESTRICT = 1;
// 异常事件类型-网络异常或应用信息配置错误
public const int EVENT_TYPE_NETWORK_ERROR = 2;
// 是否已初始化
private readonly bool hasInit = false;
// 是否已通过合规认证检查
public bool hasCheckedCompliance { get; private set; }
// 合规认证部分限制事件监听,用于显示对应提示 UI
private readonly List<Action<int>> restrictActionList;
private static readonly Lazy<GameSDKManager> lazy
= new Lazy<GameSDKManager>(() => new GameSDKManager());
public static GameSDKManager Instance { get { return lazy.Value; } }
private GameSDKManager()
{
restrictActionList = new List<Action<int>>();
}
// 声明合规认证回调
private readonly Action<int, string> ComplianceCallback = (code, errorMsg) =>
{
// 根据回调返回的参数 code 添加不同情况的处理
switch (code)
{
case 500: // 玩家未受限制,可正常开始
Instance.hasCheckedCompliance = true;
UnityEngine.Debug.Log("开始游戏");
break;
case 1000: // 防沉迷认证凭证无效时触发
case 1001: // 当玩家触发时长限制时,点击了拦截窗口中「切换账号」按钮
case 9002: // 实名认证过程中玩家关闭了实名窗口
TapTapLogin.Instance.Logout();
SceneManager.LoadScene("Login");
break;
case 1100: // 当前用户因触发应用设置的年龄限制无法进入游戏
foreach (Action<int> action in Instance.restrictActionList)
{
UnityEngine.Debug.Log("show anit ui");
action.Invoke(EVENT_TYPE_AGE_RESTRICT);
}
break;
case 1200: // 数据请求失败,应用信息错误或网络连接异常
foreach (Action<int> action in Instance.restrictActionList)
{
action.Invoke(EVENT_TYPE_NETWORK_ERROR);
}
break;
default:
UnityEngine.Debug.Log("其他可选回调");
break;
}
};
/// <summary>
/// 初始化登录与合规认证 SDK
/// </summary>
public void InitSDK()
{
if (!hasInit)
{
TapTapSdkOptions coreOptions = new TapTapSdkOptions
{
clientId = clientId,
clientToken = clientToken
};
TapTapComplianceOption complianceOption = new TapTapComplianceOption
{
showSwitchAccount = false, // 是否显示切换账号按钮
useAgeRange = true // 是否使用年龄段信息
};
// 创建其他选项数组
TapTapSdkBaseOptions[] otherOptions = new TapTapSdkBaseOptions[]
{
complianceOption
};
TapTapSDK.Init(coreOptions, otherOptions);
TapTapCompliance.RegisterComplianceCallback(ComplianceCallback);
}
}
/// <summary>
/// 开始合规认证检查
/// </summary>
/// <param name="userIdentifier">用户唯一标识</param>
public void StartCheckCompliance(string userIdentifier)
{
hasCheckedCompliance = false;
TapTapCompliance.Startup(userIdentifier);
}
/// <summary>
/// 注册合规认证异常回调监听
/// </summary>
/// <param name="action"> 监听实例</param>
public void RegisterListener(Action<int> action)
{
UnityEngine.Debug.Log("register anit ui");
restrictActionList.Add(action);
}
/// <summary>
/// 移除合规认证异常回调监听
/// </summary>
/// <param name="action"> 监听实例</param>
public void UnRegisterListener(Action<int> action)
{
UnityEngine.Debug.Log("移除监听");
restrictActionList.Remove(action);
}
}