| 
									
										
										
										
											2025-08-30 13:48:51 +00:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using GoogleMobileAds.Api; | 
					
						
							|  |  |  | using Script.SDKManager.AdsSDKManager.AdmobAdsManager.Utils; | 
					
						
							|  |  |  | using Script.Utils; | 
					
						
							| 
									
										
										
										
											2025-08-31 08:26:53 +00:00
										 |  |  | using SDKManager.AdsSDKManager.Constant; | 
					
						
							| 
									
										
										
										
											2025-08-30 13:48:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Script.SDKManager.AdsSDKManager.AdmobAdsManager | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public class AdmobRewardedAdManager  | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         private Dictionary<string, RewardedAd> _rewardedAds = new Dictionary<string, RewardedAd>(); | 
					
						
							|  |  |  |         private Dictionary<string, double> _adRevenueCache = new Dictionary<string, double>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void InitializeAdUnits(List<string> adUnitIds, Action<string> onAdLoaded = null, | 
					
						
							|  |  |  |             Action<string, int, string> onAdLoadFailed = null, | 
					
						
							|  |  |  |             Action<string> onAdShowed = null, | 
					
						
							|  |  |  |             Action<string> onAdDismissed = null, | 
					
						
							|  |  |  |             Action<string, int, string> onAdError = null, | 
					
						
							|  |  |  |             Action<AdValue> onAdPaid = null, | 
					
						
							|  |  |  |             Action<string> onAdClicked = null) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             foreach (var adUnitId in adUnitIds) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 CreateRewardedAd(adUnitId, onAdLoaded, onAdLoadFailed, onAdShowed, onAdDismissed, onAdError,onAdPaid, onAdClicked); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         private void CreateRewardedAd(string adUnitId, Action<string> onAdLoaded,  | 
					
						
							|  |  |  |             Action<string, int, string> onAdLoadFailed, | 
					
						
							|  |  |  |             Action<string> onAdShowed, | 
					
						
							|  |  |  |             Action<string> onAdDismissed, | 
					
						
							|  |  |  |             Action<string, int, string> onAdError, | 
					
						
							|  |  |  |             Action<AdValue> onAdPaid, | 
					
						
							|  |  |  |             Action<string> onAdClicked) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_rewardedAds.ContainsKey(adUnitId)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 LoggerUtils.Debug($"[Admob] rewarded Ad unit {adUnitId} already exists"); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             _adRevenueCache[adUnitId] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             RewardedAd.Load(adUnitId, new AdRequest(), (RewardedAd ad, LoadAdError error) => | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (error != null || ad == null) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad failed to load an ad with error : " + error); | 
					
						
							|  |  |  |                     onAdLoadFailed?.Invoke(adUnitId, error.GetCode(), error.GetMessage()); | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 LoggerUtils.Debug("[Admob] rewarded ad loaded with response : " + ad.GetResponseInfo().ToString()); | 
					
						
							|  |  |  |                 _rewardedAds[adUnitId] = ad; | 
					
						
							|  |  |  |                 _adRevenueCache[adUnitId] = AdmobUtils.GetRewardedAdEcpm(ad); | 
					
						
							|  |  |  |                 onAdLoaded?.Invoke(adUnitId); | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  |                 ad.OnAdPaid += (AdValue adValue) => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug(String.Format("[Admob] rewarded ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode)); | 
					
						
							| 
									
										
										
										
											2025-08-31 08:26:53 +00:00
										 |  |  |                     AdmobAdsManager.TrackAdImpression(ad.GetResponseInfo().GetLoadedAdapterResponseInfo(), | 
					
						
							|  |  |  |                         adValue, | 
					
						
							|  |  |  |                         AdsType.Rewarded, | 
					
						
							|  |  |  |                         AdmobUtils.ParseResponseInfo(ad.GetResponseInfo())); | 
					
						
							| 
									
										
										
										
											2025-08-30 13:48:51 +00:00
										 |  |  |                     onAdPaid?.Invoke(adValue); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ad.OnAdImpressionRecorded += () => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad recorded an impression."); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ad.OnAdClicked += () => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad was clicked."); | 
					
						
							|  |  |  |                     onAdClicked?.Invoke(adUnitId); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ad.OnAdFullScreenContentOpened += () => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad full screen content opened."); | 
					
						
							|  |  |  |                     onAdShowed?.Invoke(adUnitId); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ad.OnAdFullScreenContentClosed += () => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad full screen content closed."); | 
					
						
							|  |  |  |                     onAdDismissed?.Invoke(adUnitId); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ad.OnAdFullScreenContentFailed += (AdError error) => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     LoggerUtils.Debug("[Admob] rewarded ad failed to open full screen content with error : " + error); | 
					
						
							|  |  |  |                     onAdError?.Invoke(adUnitId, error.GetCode(), error.GetMessage()); | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         // 显示价格最高的广告 | 
					
						
							|  |  |  |         public void ShowHighestPayingAd() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var highestPayingAdUnit = GetHighestPayingAdUnit(); | 
					
						
							|  |  |  |             if (!string.IsNullOrEmpty(highestPayingAdUnit)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ShowAd(highestPayingAdUnit); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 显示特定广告位的广告 | 
					
						
							|  |  |  |         private void ShowAd(string adUnitId) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_rewardedAds.TryGetValue(adUnitId, out var ad)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ad.Show(null); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         // 检查特定广告位是否可用 | 
					
						
							|  |  |  |         private bool IsAdAvailable(string adUnitId) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return _rewardedAds.TryGetValue(adUnitId, out var ad) && ad.CanShowAd(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         // 获取所有可用的广告位 | 
					
						
							|  |  |  |         public List<string> GetAvailableAdUnits() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var available = new List<string>(); | 
					
						
							|  |  |  |             foreach (var kvp in _rewardedAds) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (kvp.Value.CanShowAd()) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     available.Add(kvp.Key); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return available; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |         // 获取价格最高的广告位ID | 
					
						
							|  |  |  |         public string GetHighestPayingAdUnit() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             string highestPayingAdUnit = null; | 
					
						
							|  |  |  |             double highestRevenue = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach (var kvp in _adRevenueCache) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 var adUnitId = kvp.Key; | 
					
						
							|  |  |  |                 var revenue = kvp.Value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 确保广告确实已加载并且价格更高 | 
					
						
							|  |  |  |                 if (IsAdAvailable(adUnitId) && revenue > highestRevenue) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     highestRevenue = revenue; | 
					
						
							|  |  |  |                     highestPayingAdUnit = adUnitId; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return highestPayingAdUnit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         // 获取价格最高的广告收益信息 | 
					
						
							|  |  |  |         public double GetHighestPayingAdRevenue() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var highestPayingAdUnit = GetHighestPayingAdUnit(); | 
					
						
							|  |  |  |             if (!string.IsNullOrEmpty(highestPayingAdUnit) &&  | 
					
						
							|  |  |  |                 _adRevenueCache.TryGetValue(highestPayingAdUnit, out var revenue)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return revenue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-08-31 05:15:14 +00:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2025-08-30 13:48:51 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 清理资源 | 
					
						
							|  |  |  |         public void Destroy() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             foreach (var ad in _rewardedAds.Values) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ad.Destroy(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             _rewardedAds.Clear(); | 
					
						
							|  |  |  |             _adRevenueCache.Clear(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |