Skip to content

Commit

Permalink
add admob gdpr
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayWolf52 committed Jan 30, 2024
1 parent a857cb2 commit 6230f21
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
142 changes: 142 additions & 0 deletions VirtueSky/Advertising/Runtime/Admob/GDPR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System.Collections.Generic;
#if ADS_ADMOB
using GoogleMobileAds.Api;
using GoogleMobileAds.Ump.Api;
#endif
using UnityEngine;
using VirtueSky.Inspector;
using VirtueSky.Variables;

public class GDPR : MonoBehaviour
{
[SerializeField] private bool isDontDestroyOnLoad;
[SerializeField] private bool isTestDevice = false;

[ShowIf(nameof(isTestDevice))] [SerializeField]
private List<string> listTestDeviceHashedIds = new List<string>();

[SerializeField] private BooleanVariable isCanRequestAds;

private void Awake()
{
if (isDontDestroyOnLoad)
{
DontDestroyOnLoad(this.gameObject);
}
}

private void Start()
{
#if ADS_ADMOB
Init();
#endif
}


#if ADS_ADMOB
public void Init()
{
#if !UNITY_EDITOR
string deviceID = SystemInfo.deviceUniqueIdentifier;
string deviceIDUpperCase = deviceID.ToUpper();
Debug.Log("TestDeviceHashedId = " + deviceIDUpperCase);
if (isTestDevice)
{
ConsentRequestParameters request = new ConsentRequestParameters
{
TagForUnderAgeOfConsent = false,
ConsentDebugSettings = new ConsentDebugSettings()
{
DebugGeography = DebugGeography.EEA,
TestDeviceHashedIds = listTestDeviceHashedIds
}
};

ConsentInformation.Update(request, OnConsentInfoUpdated);
}
else
{
ConsentRequestParameters request = new ConsentRequestParameters
{
TagForUnderAgeOfConsent = false,
};

ConsentInformation.Update(request, OnConsentInfoUpdated);
}

#endif
}

private void OnConsentInfoUpdated(FormError consentError)
{
if (consentError != null)
{
Debug.Log("error consentError = " + consentError);
return;
}

ConsentForm.LoadAndShowConsentFormIfRequired(
(FormError formError) =>
{
if (formError != null)
{
Debug.Log("error consentError = " + consentError);
return;
}

Debug.Log("ConsentStatus = " + ConsentInformation.ConsentStatus.ToString());
Debug.Log("CanRequestAds = " + ConsentInformation.CanRequestAds());
if (ConsentInformation.CanRequestAds())
{
MobileAds.RaiseAdEventsOnUnityMainThread = true;
if (isCanRequestAds != null)
{
isCanRequestAds.Value = true;
}
}
else
{
if (isCanRequestAds != null)
{
isCanRequestAds.Value = false;
}
}
}
);
}

public bool GDPROnCanRequestAds => ConsentInformation.CanRequestAds();

public void LoadAndShowConsentForm()
{
Debug.Log("LoadAndShowConsentForm Start!");

ConsentForm.Load((consentForm, loadError) =>
{
if (loadError != null)
{
Debug.Log("error loadError = " + loadError);
return;
}


consentForm.Show(showError =>
{
if (showError != null)
{
Debug.Log("error showError = " + showError);
return;
}
});
});
}

public void GDPRReset()
{
//#if !UNITY_EDITOR
Debug.Log("Reset Start!");
ConsentInformation.Reset();
//#endif
}
#endif
}
11 changes: 11 additions & 0 deletions VirtueSky/Advertising/Runtime/Admob/GDPR.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"GUID:c904f6d969e991d459a0843b71c22ec5",
"GUID:324caed91501a9c47a04ebfd87b68794",
"GUID:a4cfc1a18fa3a469b96d885db522f42e",
"GUID:4c08fb56ff02b9a4ebfcb1c4779015e0"
"GUID:4c08fb56ff02b9a4ebfcb1c4779015e0",
"GUID:35d694408290717499b3838802212c7f"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit 6230f21

Please sign in to comment.