Skip to content

Commit

Permalink
When ad removal is purchased, a structure has been added to prevent H… (
Browse files Browse the repository at this point in the history
#460)

* When ad removal is purchased, a structure has been added to prevent HMSAdsKitManager from showing ads by transferring a boolean when creating the Instance.

* When ad removal is purchased, a structure has been added to prevent HMSAdsKitManager from showing ads by transferring a boolean when creating the Instance.
* SplashAd now checks when loading to see if ad removal has been purchased.
  • Loading branch information
alihan98ersoy authored Jan 3, 2024
1 parent 2217bf8 commit a53c309
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
14 changes: 7 additions & 7 deletions Assets/Huawei/Demos/Ads/AdsDemoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ private void Awake()

private void Start()
{
HMSAdsKitManager.Instance.OnRewarded = OnRewarded;
HMSAdsKitManager.Instance.OnInterstitialAdClosed = OnInterstitialAdClosed;

HMSAdsKitManager.Instance.ConsentOnFail = OnConsentFail;
HMSAdsKitManager.Instance.ConsentOnSuccess = OnConsentSuccess;
HMSAdsKitManager.Instance = new HMSAdsKitManager(hasPurchasedNoAds: false)
{
OnRewarded = OnRewarded,
OnInterstitialAdClosed = OnInterstitialAdClosed,
ConsentOnFail = OnConsentFail,
ConsentOnSuccess = OnConsentSuccess
};
HMSAdsKitManager.Instance.RequestConsentUpdate();

//testAdStatusToggle = GameObject.FindGameObjectWithTag("Toggle").GetComponent<Toggle>();
//testAdStatusToggle.isOn = HMSAdsKitSettings.Instance.Settings.GetBool(HMSAdsKitSettings.UseTestAds);


#region SetNonPersonalizedAd , SetRequestLocation

Expand All @@ -59,7 +60,6 @@ private void Start()
Debug.Log($"{TAG}Consent: {requestOptions.Consent}");

#endregion

}

private void OnConsentSuccess(ConsentStatus consentStatus, bool isNeedConsent, IList<AdProvider> adProviders)
Expand Down
28 changes: 20 additions & 8 deletions Assets/Huawei/Scripts/Ads/HMSAdsKitManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using HuaweiConstants;

using HuaweiMobileServices.Ads;
using HuaweiMobileServices.Ads.InstallReferrer;
using HuaweiMobileServices.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using UnityEngine;

using static HuaweiConstants.UnityBannerAdPositionCode;
using static HuaweiMobileServices.Ads.SplashAd;

Expand All @@ -34,24 +31,31 @@ public class HMSAdsKitManager : HMSManagerSingleton<HMSAdsKitManager>

private bool isInitialized;

private InstallReferrerClient installReferrerClient;

public Action<ReferrerDetails> InstallReferrerSuccess { get; set; }
public Action<InstallReferrerResponse> InstallReferrerFail { get; set; }
public Action InstallReferrerDisconnect { get; set; }

InstallReferrerClient installReferrerClient;
public bool hasPurchasedNoAds = false;

public HMSAdsKitManager()
{
adsKitSettings = HMSAdsKitSettings.Instance.Settings;
HMSManagerStart.Start(OnAwake, OnStart, TAG);
}

public HMSAdsKitManager(bool hasPurchasedNoAds)
{
adsKitSettings = HMSAdsKitSettings.Instance.Settings;
this.hasPurchasedNoAds = hasPurchasedNoAds;
HMSManagerStart.Start(OnAwake, () => { OnStart(hasPurchasedNoAds);}, TAG);
}

private void OnAwake()
{
Debug.Log($"{TAG} OnAwake");
Init();
if (adsKitSettings.GetBool(HMSAdsKitSettings.EnableSplashAd))
LoadSplashAd();
}

private void OnStart()
Expand All @@ -60,6 +64,12 @@ private void OnStart()
LoadAdsWhenInternetIsAvailable();
}

private void OnStart(bool hasPurchasedNoAds = false)
{
Debug.Log($"{TAG} OnStart");
LoadAdsWhenInternetIsAvailable(hasPurchasedNoAds);
}

private void Init()
{
Debug.Log($"{TAG} Init");
Expand All @@ -68,19 +78,21 @@ private void Init()
adsKitSettings = HMSAdsKitSettings.Instance.Settings;
}

private async void LoadAdsWhenInternetIsAvailable()
private async void LoadAdsWhenInternetIsAvailable(bool hasPurchasedNoAds = false)
{
while (Application.internetReachability == NetworkReachability.NotReachable)
await Task.Delay(TimeSpan.FromSeconds(0.5));

Debug.Log($"{TAG} Loading Ads");
LoadAllAds();
LoadAllAds(hasPurchasedNoAds);
}

public void LoadAllAds(bool hasPurchasedNoAds = false)
{
if (!hasPurchasedNoAds)
{
if (adsKitSettings.GetBool(HMSAdsKitSettings.EnableSplashAd))
LoadSplashAd();
UnityBannerAdPositionCodeType _adPositionCodeType = UnityBannerAdPositionCodeType.POSITION_BOTTOM;
UnityBannerAdSizeType _adSizeType = UnityBannerAdSizeType.BANNER_SIZE_360_57;

Expand Down
4 changes: 3 additions & 1 deletion Assets/Huawei/Scripts/Utils/HMSSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ public static T Instance
}
}
}

public class HMSManagerSingleton<T> where T : new()
{
private static readonly Lazy<T> _instance = new Lazy<T>(() => new T());
private static Lazy<T> _instance = new Lazy<T>(() => new T());

public static T Instance
{
get
{
return _instance.Value;
}
set => _instance = new Lazy<T>(() => value);
}
}

0 comments on commit a53c309

Please sign in to comment.