-
Notifications
You must be signed in to change notification settings - Fork 40
/
AdsDemoManager.cs
257 lines (214 loc) · 8.2 KB
/
AdsDemoManager.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
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
using System.Collections.Generic;
using UnityEngine;
using HmsPlugin;
using HuaweiMobileServices.Ads;
using System;
using HuaweiMobileServices.Utils;
using HuaweiMobileServices.Id;
using static HmsPlugin.HMSAdsKitManager;
public class AdsDemoManager : MonoBehaviour
{
//private Toggle testAdStatusToggle;
private AdLoadMethod RewardedAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod InterstitialAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod BannerAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod SplashAdLoadMethod = AdLoadMethod.Default;
private RewardVerifyConfig rewardVerifyConfig;
private const string TAG = "[HMS] AdsDemoManager: ";
#region Singleton
public static AdsDemoManager Instance { get; private set; }
private void Singleton()
{
if (Instance != null && Instance != this)
{
Destroy(this);
}
else
{
Instance = this;
}
}
#endregion
private void Awake()
{
Singleton();
}
private void Start()
{
BeforeBuildManager();
}
private void Init()
{
HMSAdsKitManager.Instance = new Builder()
.SetHasPurchasedNoAds(false)
.SetRewardedAdLoadMethod(RewardedAdLoadMethod, rewardVerifyConfig)
.SetBannerAdLoadMethod(InterstitialAdLoadMethod)
.SetInterstitialAdLoadMethod(BannerAdLoadMethod)
.SetSplashAdLoadMethod(SplashAdLoadMethod)
.Build();
HMSAdsKitManager.Instance.OnRewardedAdLoaded = OnRewardedAdLoaded;
HMSAdsKitManager.Instance.OnRewarded = OnRewarded;
HMSAdsKitManager.Instance.OnInterstitialAdClosed = OnInterstitialAdClosed;
HMSAdsKitManager.Instance.ConsentOnSuccess = OnConsentSuccess;
HMSAdsKitManager.Instance.ConsentOnFail = OnConsentFail;
HMSAdsKitManager.Instance.RequestConsentUpdate();
//testAdStatusToggle = GameObject.FindGameObjectWithTag("Toggle").GetComponent<Toggle>();
//testAdStatusToggle.isOn = HMSAdsKitSettings.Instance.Settings.GetBool(HMSAdsKitSettings.UseTestAds);
#region SetNonPersonalizedAd , SetRequestLocation
var builder = HwAds.RequestOptions.ToBuilder();
builder
.SetConsent("tcfString")
.SetNonPersonalizedAd((int)NonPersonalizedAd.ALLOW_ALL)
.Build();
bool requestLocation = true;
var requestOptions = builder.SetConsent("testConsent").SetRequestLocation(requestLocation).Build();
Debug.Log($"{TAG}RequestOptions NonPersonalizedAds: {requestOptions.NonPersonalizedAd}");
Debug.Log($"{TAG}Consent: {requestOptions.Consent}");
#endregion
}
private void OnConsentSuccess(ConsentStatus consentStatus, bool isNeedConsent, IList<AdProvider> adProviders)
{
Debug.Log($"{TAG}OnConsentSuccess consentStatus:{consentStatus} isNeedConsent:{isNeedConsent}");
foreach (var AdProvider in adProviders)
{
Debug.Log($"{TAG}OnConsentSuccess adproviders: Id:{AdProvider.Id} Name:{AdProvider.Name} PrivacyPolicyUrl:{AdProvider.PrivacyPolicyUrl} ServiceArea:{AdProvider.ServiceArea}");
}
}
private void OnConsentFail(string desc)
{
Debug.LogError($"{TAG}OnConsentFail:{desc}");
}
public void ShowBannerAd()
{
Debug.Log($"{TAG}ShowBannerAd");
HMSAdsKitManager.Instance.ShowBannerAd();
}
public void HideBannerAd()
{
Debug.Log($"{TAG}HideBannerAd");
HMSAdsKitManager.Instance.HideBannerAd();
}
public void SetRewardVerifyConfig(RewardVerifyConfig config)
{
Debug.Log($"{TAG}SetRewardVerifyConfig");
HMSAdsKitManager.Instance.SetRewardVerifyConfig(config);
}
public void ShowRewardedAd()
{
Debug.Log($"{TAG}ShowRewardedAd");
HMSAdsKitManager.Instance.ShowRewardedAd();
}
public void ShowInterstitialAd()
{
Debug.Log($"{TAG}ShowInterstitialAd");
HMSAdsKitManager.Instance.ShowInterstitialAd();
}
public void ShowSplashImage()
{
Debug.Log($"{TAG}ShowSplashImage!");
HMSAdsKitManager.Instance.LoadSplashAd("testq6zq98hecj", SplashAd.SplashAdOrientation.PORTRAIT);
}
public void ShowSplashVideo()
{
Debug.Log($"{TAG}ShowSplashVideo!");
HMSAdsKitManager.Instance.LoadSplashAd("testd7c5cewoj6", SplashAd.SplashAdOrientation.PORTRAIT);
}
private void OnRewardedAdLoaded()
{
Debug.Log($"{TAG}OnRewardedAdLoaded");
#region RewardVerifyConfig
/*RewardVerifyConfig verifyConfig = new RewardVerifyConfig.Builder().SetData("CUSTOM_DATA").SetUserId("123456").Build();
SetRewardVerifyConfig(verifyConfig);
Debug.Log($"{TAG}OnRewardedAdLoaded:{verifyConfig.UserId} - {verifyConfig.Data}");*/
#endregion
}
public void OnRewarded(Reward reward)
{
Debug.Log($"{TAG}rewarded!");
}
public void OnInterstitialAdClosed()
{
Debug.Log($"{TAG}interstitial ad closed");
}
public void SetTestAdStatus()
{
// HMSAdsKitManager.Instance.SetTestAdStatus(testAdStatusToggle.isOn);
HMSAdsKitManager.Instance.SetTestAdStatus(HMSAdsKitSettings.Instance.Settings.GetBool(HMSAdsKitSettings.UseTestAds));
HMSAdsKitManager.Instance.DestroyBannerAd();
HMSAdsKitManager.Instance.LoadAllAds();
}
private void BeforeBuildManager()
{
if (RewardedAdLoadMethod == AdLoadMethod.WithConfig)
{
AssignLoadRewardAdWithConfig();
return;
}
Init();
}
private void AssignLoadRewardAdWithConfig()
{
var user = HMSAccountKitManager.Instance.HuaweiId;
Debug.Log($"{TAG} GetCurrentUser is null : {user == null}");
if (user != null)
{
SetRewardAdConfig(user.OpenId);
return;
}
HMSAccountKitManager.Instance.OnSignInSuccess += OnSignInAccountSilentSuccess;
HMSAccountKitManager.Instance.OnSignInFailed += OnSignInAccountSilentFailed;
HMSAccountKitManager.Instance.SilentSignIn();
}
private void OnSignInAccountSilentSuccess(AuthAccount authHuaweiId)
{
Debug.Log($"{TAG} SignIn success. AuthHuaweiId: {authHuaweiId?.OpenId}");
if (authHuaweiId != null)
{
SetRewardAdConfig(authHuaweiId.OpenId);
return;
}
else
{
Debug.LogError($"{TAG} SignIn failed. AuthHuaweiId is null.");
}
}
private void OnSignInAccountSilentFailed(HMSException exception)
{
Debug.LogError($"{TAG} SignIn failed with exception: {exception.WrappedExceptionMessage}");
if (exception.WrappedExceptionMessage.Contains("2002"))
{
Debug.Log($"{TAG} SilentSignIn failed. User is not signed in. Trying SignIn.");
HMSAccountKitManager.Instance.OnSignInSuccess -= OnSignInAccountSilentSuccess;
HMSAccountKitManager.Instance.OnSignInFailed -= OnSignInAccountSilentFailed;
HMSAccountKitManager.Instance.OnSignInSuccess += OnSignInAccountSuccess;
HMSAccountKitManager.Instance.OnSignInFailed += OnSignInAccountFailed;
HMSAccountKitManager.Instance.SignIn();
}
}
private void OnSignInAccountSuccess(AuthAccount authHuaweiId)
{
Debug.Log($"{TAG} SignIn success. AuthHuaweiId: {authHuaweiId?.OpenId}");
if (authHuaweiId != null)
{
SetRewardAdConfig(authHuaweiId.OpenId);
return;
}
else
{
Debug.LogError($"{TAG} SignIn failed. AuthHuaweiId is null.");
}
}
private void OnSignInAccountFailed(HMSException exception)
{
Debug.LogError($"{TAG} SignIn failed with exception: {exception.WrappedExceptionMessage}");
}
private void SetRewardAdConfig(string userId)
{
var exampleData = $"{{\"userId\":\"{userId}\", \"date\":\"{DateTime.Now}\"}}";
rewardVerifyConfig = new RewardVerifyConfig.Builder()
.SetUserId(userId)
.SetData(exampleData)
.Build();
Init();
}
}