399 lines
13 KiB
C#
399 lines
13 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
namespace WZ
|
|
{
|
|
public static class AdConfigParser
|
|
{
|
|
private static AdConfig _config;
|
|
private static Dictionary<string,object> _configDict = new Dictionary<string, object>();
|
|
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);
|
|
_configDict = JsonToDictionary(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;
|
|
}
|
|
}
|
|
|
|
public static Dictionary<string,object> JsonToDictionary(string jsonStr)
|
|
{
|
|
Dictionary<string,object> dic= JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
|
|
return dic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 Admob 广告单元ID
|
|
/// </summary>
|
|
public static List<string> GetAdmobAdUnits(AdsType adType, BannerType bannerType = BannerType.Standard)
|
|
{
|
|
if (_config == null)
|
|
{
|
|
|
|
switch (adType)
|
|
{
|
|
case AdsType.Banner:
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
return new List<string>() { StaticValue.AdmobNormalBannerId};
|
|
}
|
|
else
|
|
{
|
|
return new List<string> { StaticValue.AdmobCollapsibleBannerId };
|
|
}
|
|
case AdsType.Rewarded:
|
|
return new List<string> { StaticValue.AdmobRewardId };
|
|
case AdsType.Native:
|
|
return new List<string> { StaticValue.AdmobNativeId, StaticValue.AdmobMinddleNativeId, StaticValue.AdmobSmallNativeId, StaticValue.AdmobFullNativeId };
|
|
case AdsType.Splash:
|
|
return new List<string> { StaticValue.AdmobSplashId };
|
|
case AdsType.Interstitial:
|
|
return new List<string> { StaticValue.AdmobInterId };
|
|
default:
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
switch (adType)
|
|
{
|
|
case AdsType.Banner:
|
|
if (_configDict.ContainsKey("admob_banner_units"))
|
|
{
|
|
return _config.admob_banner_units ?? new List<string>();
|
|
}
|
|
else if (_configDict.ContainsKey("admob_normal_banner_units"))
|
|
{
|
|
return _config.admob_collbanner_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
return new List<string> { StaticValue.AdmobNormalBannerId };
|
|
}
|
|
else
|
|
{
|
|
return new List<string> { StaticValue.AdmobCollapsibleBannerId };
|
|
}
|
|
}
|
|
case AdsType.Rewarded:
|
|
if (_configDict.ContainsKey("admob_rewarded_units"))
|
|
{
|
|
return _config.admob_rewarded_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string> { StaticValue.AdmobRewardId };
|
|
}
|
|
case AdsType.Native:
|
|
if (_configDict.ContainsKey("admob_native_units"))
|
|
{
|
|
return _config.admob_native_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string>{ StaticValue.AdmobNativeId,
|
|
StaticValue.AdmobMinddleNativeId,
|
|
StaticValue.AdmobSmallNativeId,
|
|
StaticValue.AdmobFullNativeId };
|
|
}
|
|
case AdsType.Splash:
|
|
if (_configDict.ContainsKey("admob_splash_units"))
|
|
{
|
|
return _config.admob_splash_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string> { StaticValue.AdmobSplashId };
|
|
}
|
|
case AdsType.Interstitial:
|
|
if (_configDict.ContainsKey("admob_interstitial_units"))
|
|
{
|
|
return _config.admob_interstitial_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string> { StaticValue.AdmobInterId };
|
|
}
|
|
default:
|
|
return 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>()
|
|
};
|
|
}
|
|
|
|
switch (adType)
|
|
{
|
|
case AdsType.Interstitial:
|
|
if (_configDict.ContainsKey("topon_interstitial_units"))
|
|
{
|
|
return _config.topon_interstitial_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string>{ StaticValue.TopOnInterAdUnitID };
|
|
}
|
|
case AdsType.Rewarded:
|
|
if (_configDict.ContainsKey("topon_rewarded_units"))
|
|
{
|
|
return _config.topon_rewarded_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string>{ StaticValue.TopOnRewardAdUnitID };;
|
|
}
|
|
default:
|
|
return 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.MaxInterAdUnitID,StaticValue.MaxSparespareInterAdUnitID },
|
|
AdsType.Rewarded => new List<string> { StaticValue.MaxRewardAdUnitID,StaticValue.MaxSparespareRewardAdUnitID },
|
|
_ => new List<string>()
|
|
};
|
|
}
|
|
|
|
switch (adType)
|
|
{
|
|
case AdsType.Interstitial:
|
|
if (_configDict.ContainsKey("max_interstitial_units"))
|
|
{
|
|
return _config.max_interstitial_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string>{ StaticValue.MaxInterAdUnitID,StaticValue.MaxSparespareInterAdUnitID };
|
|
}
|
|
|
|
case AdsType.Rewarded:
|
|
if (_configDict.ContainsKey("max_rewarded_units"))
|
|
{
|
|
return _config.max_rewarded_units ?? new List<string>();
|
|
}
|
|
else
|
|
{
|
|
return new List<string>{ StaticValue.MaxRewardAdUnitID,StaticValue.MaxSparespareRewardAdUnitID };
|
|
}
|
|
default:
|
|
return 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;
|
|
if (_configDict.ContainsKey("topon_app_id"))
|
|
{
|
|
return _config?.topon_app_id;
|
|
}
|
|
else
|
|
{
|
|
return StaticValue.TopOnAppID;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 TopOn App Key
|
|
/// </summary>
|
|
public static string GetTopOnAppKey()
|
|
{
|
|
if (_config == null) return StaticValue.TopOnAppKey;
|
|
|
|
if (_configDict.ContainsKey("topon_app_key"))
|
|
{
|
|
return _config?.topon_app_key;
|
|
}
|
|
else
|
|
{
|
|
return StaticValue.TopOnAppKey;
|
|
}
|
|
}
|
|
|
|
/// <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_collbanner_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;
|
|
}
|
|
|
|
}
|
|
|