| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #if UNITY_IOS && !UNITY_EDITOR
- using System;
- using GoogleMobileAds.Api;
- using GoogleMobileAds.Ump.Api;
- namespace DevilLink2.Ads {
- public sealed class AdMobRewardedAdService : IRewardedAdService {
- readonly Action<Action> post; RewardedAd ad,activeAd; bool initialized,initializing,loading,disposed,consenting;int generation;
- public string Status{get;private set;}="PRIVACY CHECK";
- public bool Ready=>!disposed&&ConsentInformation.CanRequestAds()&&ad!=null&&ad.CanShowAd();
- public bool PrivacyOptionsRequired=>ConsentInformation.PrivacyOptionsRequirementStatus==PrivacyOptionsRequirementStatus.Required;
- public AdMobRewardedAdService(Action<Action> mainThread){post=mainThread;}
- public void Initialize(){if(disposed||consenting||initialized||initializing)return;consenting=true;
- ConsentInformation.Update(new ConsentRequestParameters(),error=>post(()=>{
- if(disposed)return;
- if(error!=null){consenting=false;Status="PRIVACY UPDATE FAILED";StartIfAllowed();return;}
- ConsentForm.LoadAndShowConsentFormIfRequired(formError=>post(()=>{if(disposed)return;consenting=false;Status=formError==null?"PRIVACY READY":"PRIVACY FORM UNAVAILABLE";StartIfAllowed();}));
- }));
- }
- void StartIfAllowed(){if(disposed||!ConsentInformation.CanRequestAds()){Status="ADS UNAVAILABLE · PRIVACY";return;}
- if(initialized){Load();return;}if(initializing)return;initializing=true;Status="INITIALIZING ADS";
- MobileAds.Initialize(status=>post(()=>{if(disposed)return;initializing=false;initialized=status!=null;if(initialized)Load();else Status="AD INITIALIZATION FAILED";}));
- }
- public void Load(){if(disposed||loading)return;if(!initialized){Initialize();return;}if(!ConsentInformation.CanRequestAds()){Status="ADS UNAVAILABLE · PRIVACY";return;}
- ad?.Destroy();ad=null;loading=true;Status="LOADING AD";int request=++generation;
- RewardedAd.Load(AdMobConfiguration.RewardedId,new AdRequest(),(loaded,error)=>post(()=>{
- if(disposed||request!=generation){loaded?.Destroy();return;}loading=false;
- if(error!=null||loaded==null){loaded?.Destroy();Status="AD LOAD FAILED · TAP TO RETRY";return;}ad=loaded;Status="WATCH AD";
- }));
- }
- public void Show(Action earned,Action<string> closed){if(!Ready){closed("AD NOT READY");return;}var shown=ad;activeAd=shown;ad=null;Status="AD DISPLAYING";bool ended=false;
- Action<string> end=error=>{if(ended||disposed)return;ended=true;shown.Destroy();activeAd=null;Status=error??"AD CLOSED";closed(error);};
- shown.OnAdFullScreenContentClosed+=()=>post(()=>end(null));
- shown.OnAdFullScreenContentFailed+=error=>post(()=>end("AD SHOW FAILED"));
- try{shown.Show(_=>post(()=>{if(!disposed&&!ended)earned();}));}catch{shown.Destroy();end("AD SHOW FAILED");}
- }
- public void ShowPrivacyOptions(){if(disposed||!PrivacyOptionsRequired||consenting)return;consenting=true;ConsentForm.ShowPrivacyOptionsForm(error=>post(()=>{if(disposed)return;consenting=false;if(!ConsentInformation.CanRequestAds()){ad?.Destroy();ad=null;Status="ADS UNAVAILABLE · PRIVACY";}else StartIfAllowed();}));}
- public void Dispose(){disposed=true;generation++;ad?.Destroy();activeAd?.Destroy();ad=activeAd=null;}
- }
- }
- #endif
|