275 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			275 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| 
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace WZ
 | |
| {
 | |
|     public static class AdConfigParser
 | |
|     {
 | |
|         private static AdConfig _config;
 | |
|         public static AdConfig Parse(string json)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 // 处理 JSON 字符串,确保它是有效的
 | |
|                 if (string.IsNullOrEmpty(json))
 | |
|                 {
 | |
|                     LoggerUtils.Error("JSON string is null or empty");
 | |
|                     return null;
 | |
|                 }
 | |
| 
 | |
|                 // 使用 JsonUtility 解析 JSON
 | |
|                 _config = JsonUtility.FromJson<AdConfig>(json);
 | |
| 
 | |
|                 if (_config == null)
 | |
|                 {
 | |
|                     LoggerUtils.Error("Failed to parse JSON to AdConfig");
 | |
|                     return null;
 | |
|                 }
 | |
| 
 | |
|                 LoggerUtils.Info("AdConfig parsed successfully");
 | |
|                 return _config;
 | |
|             }
 | |
|             catch (Exception e)
 | |
|             {
 | |
|                 LoggerUtils.Error($"Error parsing AdConfig: {e.Message}");
 | |
|                 return null;
 | |
|             }
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Admob 广告单元ID
 | |
|         /// </summary>
 | |
|         public static List<string> GetAdmobAdUnits(AdsType adType)
 | |
|         {
 | |
|             if (_config == null)
 | |
|             {
 | |
|                 return adType switch
 | |
|                 {
 | |
|                     AdsType.Banner => new List<string> { StaticValue.AdmobNormalBannerId },
 | |
|                     AdsType.Rewarded => new List<string> { StaticValue.AdmobRewardId },
 | |
|                     AdsType.Native => new List<string> { StaticValue.AdmobNativeId },
 | |
|                     AdsType.Splash => new List<string> { StaticValue.AdmobSplashId },
 | |
|                     AdsType.Interstitial => new List<string> { StaticValue.AdmobInterId },
 | |
|                     _ => new List<string>()
 | |
|                 };
 | |
|             }
 | |
|             
 | |
|             return adType switch
 | |
|             {
 | |
|                 AdsType.Interstitial => _config.admob_interstitial_units ?? new List<string>(),
 | |
|                 AdsType.Splash => _config.admob_splash_units ?? new List<string>(),
 | |
|                 AdsType.Rewarded => _config.admob_rewarded_units ?? new List<string>(),
 | |
|                 AdsType.Banner => _config.admob_banner_units ?? new List<string>(),
 | |
|                 AdsType.Native => _config.admob_native_units ?? new List<string>(),
 | |
|                 _ => new List<string>()
 | |
|             };
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 TopOn 广告单元ID
 | |
|         /// </summary>
 | |
|         public static List<string> GetTopOnAdUnits(AdsType adType)
 | |
|         {
 | |
|             if (_config == null)
 | |
|             {
 | |
|                 return adType switch
 | |
|                 {
 | |
|                     AdsType.Interstitial => new List<string> { StaticValue.TopOnInterAdUnitID },
 | |
|                     AdsType.Rewarded => new List<string> { StaticValue.TopOnRewardAdUnitID },
 | |
|                     _ => new List<string>()
 | |
|                 };
 | |
|             }
 | |
|             
 | |
|             return adType switch
 | |
|             {
 | |
|                 AdsType.Interstitial => _config.topon_interstitial_units ?? new List<string>(),
 | |
|                 AdsType.Splash => _config.topon_splash_units ?? new List<string>(),
 | |
|                 AdsType.Rewarded => _config.topon_rewarded_units ?? new List<string>(),
 | |
|                 AdsType.Banner => _config.topon_banner_units ?? new List<string>(),
 | |
|                 AdsType.Native => _config.topon_native_units ?? new List<string>(),
 | |
|                 _ => new List<string>()
 | |
|             };
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 AppLovin MAX 广告单元ID
 | |
|         /// </summary>
 | |
|         public static List<string> GetMaxAdUnits(AdsType adType)
 | |
|         {
 | |
|             if (_config == null)
 | |
|             {
 | |
|                 return adType switch
 | |
|                 {
 | |
|                     AdsType.Interstitial => new List<string> { StaticValue.InterAdUnitID },
 | |
|                     AdsType.Rewarded => new List<string> { StaticValue.RewardAdUnitID },
 | |
|                     _ => new List<string>()
 | |
|                 };
 | |
|             }
 | |
| 
 | |
| 
 | |
|             return adType switch
 | |
|             {
 | |
|                 AdsType.Interstitial => _config.max_interstitial_units ?? new List<string>(),
 | |
|                 AdsType.Splash => _config.max_splash_units ?? new List<string>(),
 | |
|                 AdsType.Rewarded => _config.max_rewarded_units ?? new List<string>(),
 | |
|                 AdsType.Banner => _config.max_banner_units ?? new List<string>(),
 | |
|                 AdsType.Native => _config.max_native_units ?? new List<string>(),
 | |
|                 _ => new List<string>()
 | |
|             };
 | |
| 
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Bigo 广告单元ID
 | |
|         /// </summary>
 | |
|         public static List<string> GetBigoAdUnits(AdsType adType)
 | |
|         {
 | |
|             if (_config == null) return new List<string>();
 | |
|             
 | |
|             return adType switch
 | |
|             {
 | |
|                 AdsType.Interstitial => _config.bigo_i_inter_units ?? new List<string>(),
 | |
|                 AdsType.Rewarded => _config.bigo_i_reward_units ?? new List<string>(),
 | |
|                 _ => new List<string>()
 | |
|             };
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Kwai 广告单元ID
 | |
|         /// </summary>
 | |
|         public static List<string> GetKwaiAdUnits(AdsType adType)
 | |
|         {
 | |
|             if (_config == null) return new List<string>();
 | |
|             
 | |
|             return adType switch
 | |
|             {
 | |
|                 AdsType.Interstitial => _config.kwai_i_inter_units ?? new List<string>(),
 | |
|                 AdsType.Rewarded => _config.kwai_i_reward_units ?? new List<string>(),
 | |
|                 _ => new List<string>()
 | |
|             };
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 TopOn App ID
 | |
|         /// </summary>
 | |
|         public static string GetTopOnAppId()
 | |
|         {
 | |
|             if (_config == null) return StaticValue.TopOnAppID;
 | |
|             return _config?.topon_app_id;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 TopOn App Key
 | |
|         /// </summary>
 | |
|         public static string GetTopOnAppKey()
 | |
|         {
 | |
|             if (_config == null) return StaticValue.TopOnAppKey;
 | |
|             return _config?.topon_app_key;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 AppLovin MAX App Key
 | |
|         /// </summary>
 | |
|         public static string GetMaxAppKey()
 | |
|         {
 | |
|             if (_config == null) return StaticValue.ApplovinKey;
 | |
|             return _config?.max_app_key;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Bigo App ID
 | |
|         /// </summary>
 | |
|         public static string GetBigoAppId()
 | |
|         {
 | |
|             return _config?.bigo_app_id;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Kwai App ID
 | |
|         /// </summary>
 | |
|         public static string GetKwaiAppId()
 | |
|         {
 | |
|             return _config?.kwai_app_id;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取 Kwai App Token
 | |
|         /// </summary>
 | |
|         public static string GetKwaiAppToken()
 | |
|         {
 | |
|             return _config?.kwai_app_token;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 获取广告过期时间(秒)
 | |
|         /// </summary>
 | |
|         public static int GetAdExpireInSec()
 | |
|         {
 | |
|             return _config?.ad_expire_in_sec ?? -1; // 默认值
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 检查配置是否已加载
 | |
|         /// </summary>
 | |
|         public static bool IsConfigLoaded()
 | |
|         {
 | |
|             return _config != null;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 清空已加载的配置
 | |
|         /// </summary>
 | |
|         public static void ClearConfig()
 | |
|         {
 | |
|             _config = null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     [Serializable]
 | |
|     public class AdConfig
 | |
|     {
 | |
|         // Admob 配置
 | |
|         public List<string> admob_interstitial_units;
 | |
|         public List<string> admob_splash_units;
 | |
|         public List<string> admob_rewarded_units;
 | |
|         public List<string> admob_banner_units;
 | |
|         public List<string> admob_native_units;
 | |
| 
 | |
|         // TopOn 配置
 | |
|         public string topon_app_id;
 | |
|         public string topon_app_key;
 | |
|         public List<string> topon_interstitial_units;
 | |
|         public List<string> topon_splash_units;
 | |
|         public List<string> topon_rewarded_units;
 | |
|         public List<string> topon_banner_units;
 | |
|         public List<string> topon_native_units;
 | |
| 
 | |
|         // AppLovin MAX 配置
 | |
|         public string max_app_key;
 | |
|         public List<string> max_interstitial_units;
 | |
|         public List<string> max_splash_units;
 | |
|         public List<string> max_rewarded_units;
 | |
|         public List<string> max_banner_units;
 | |
|         public List<string> max_native_units;
 | |
| 
 | |
|         // Bigo 配置
 | |
|         public string bigo_app_id;
 | |
|         public List<string> bigo_i_inter_units;
 | |
|         public List<string> bigo_i_reward_units;
 | |
| 
 | |
|         // Kwai 配置
 | |
|         public string kwai_app_id;
 | |
|         public string kwai_app_token;
 | |
|         public List<string> kwai_i_inter_units;
 | |
|         public List<string> kwai_i_reward_units;
 | |
| 
 | |
|         // 通用配置
 | |
|         public int ad_expire_in_sec;
 | |
|     }
 | |
|     
 | |
|  }
 | |
| 
 |