From f9e295df2b0c8ebe3f1801740f67318a1cbdc2e6 Mon Sep 17 00:00:00 2001 From: HuYufei Date: Mon, 8 Jan 2024 20:32:47 +0800 Subject: [PATCH] =?UTF-8?q?update:=20fix=20Ads=20Model=20=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GuruCore/Runtime/Adjust/AdjustService.cs | 24 +++- Runtime/GuruCore/Runtime/Ads/ADServiceBase.cs | 19 +-- Runtime/GuruCore/Runtime/Ads/AdsModel.cs | 127 ++++++++++++++++++ Runtime/GuruCore/Runtime/Ads/AdsModel.cs.meta | 3 + .../Runtime/Analytics/Analytics.Custom.cs | 10 +- 5 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 Runtime/GuruCore/Runtime/Ads/AdsModel.cs create mode 100644 Runtime/GuruCore/Runtime/Ads/AdsModel.cs.meta diff --git a/Runtime/GuruCore/Runtime/Adjust/AdjustService.cs b/Runtime/GuruCore/Runtime/Adjust/AdjustService.cs index ed8edb3..f450560 100644 --- a/Runtime/GuruCore/Runtime/Adjust/AdjustService.cs +++ b/Runtime/GuruCore/Runtime/Adjust/AdjustService.cs @@ -15,10 +15,24 @@ namespace Guru public static readonly float DelayTime = 1f; // 延迟启动时间 private static string _adId = ""; - public static string AdId => _adId; // Google AdId - + public static string AdId + { + get + { + if(string.IsNullOrEmpty(_adId)) FetchGoogleAdId(); + return _adId; // Google AdId + } + } + private static string _adujstId = ""; - public static string AdjustId => _adujstId; // Adjust AdId; + public static string AdjustId + { + get + { + if (string.IsNullOrEmpty(_adujstId)) _adujstId = Adjust.getAdid(); + return _adujstId; // Adjust AdId; + } + } @@ -83,10 +97,10 @@ namespace Guru // }); // }); - FetchAdId(); // 异步加载AdId + FetchGoogleAdId(); // 异步加载AdId } - public static void FetchAdId() + public static void FetchGoogleAdId() { Adjust.getGoogleAdId(gid => { diff --git a/Runtime/GuruCore/Runtime/Ads/ADServiceBase.cs b/Runtime/GuruCore/Runtime/Ads/ADServiceBase.cs index b0331e5..b8fef1c 100644 --- a/Runtime/GuruCore/Runtime/Ads/ADServiceBase.cs +++ b/Runtime/GuruCore/Runtime/Ads/ADServiceBase.cs @@ -34,6 +34,8 @@ namespace Guru public static Action OnRewardLoaded; public static Action OnRewardFailed; + private AdsModel _model; + /// /// 启动广告服务 /// @@ -48,6 +50,7 @@ namespace Guru _isAutoLoadAds = autoLoadAds; _isDebugMode = isDebugMode; _onSdkInitReady = callback; + _model = AdsModel.Create(); this.Log("AD SDK Start Init"); //-------------- 初始化回调 ------------------ @@ -114,14 +117,14 @@ namespace Guru private double TchAD001RevValue { - get => PlayerPrefs.GetFloat(nameof(TchAD001RevValue), 0f); - set => PlayerPrefs.SetFloat(nameof(TchAD001RevValue), (float)value); + get => _model.TchAD001RevValue; + set => _model.TchAD001RevValue = value; } private double TchAD02RevValue { - get => PlayerPrefs.GetFloat(nameof(TchAD02RevValue), 0f); - set => PlayerPrefs.SetFloat(nameof(TchAD02RevValue), (float)value); + get => _model.TchAD02RevValue; + set => _model.TchAD02RevValue = value; } public void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) @@ -155,10 +158,10 @@ namespace Guru { TchAD001RevValue += revenue; double revenueValue = TchAD001RevValue; - this.Log("[TaichConfig]", $"获取累计收入TchAD001RevValue:{revenueValue}"); + Debug.Log($"[TaichConfig] get totally: {revenueValue}"); if (revenueValue >= Analytics.Tch001TargetValue) { - this.Log("[TaichConfig]", "获取太极收入累计超过0.01打点"); + Debug.Log($"[TaichConfig] call with value: {revenueValue}"); Analytics.Tch001ADRev(revenueValue); TchAD001RevValue = 0.0; } @@ -174,10 +177,10 @@ namespace Guru TchAD02RevValue += revenue; double revenueValue = TchAD02RevValue; - this.Log("[TchConfig]", $"获取累计收入TchAD02RevValue:{revenueValue}"); + Debug.Log($"[TchConfig] get totally: {revenueValue}"); if (revenueValue >= 0.2f) { - this.Log("[TchConfig]", "获取太极收入累计超过0.2打点"); + Debug.Log($"[TchConfig] call with value:"); Analytics.Tch02ADRev(revenueValue); TchAD02RevValue = 0.0; } diff --git a/Runtime/GuruCore/Runtime/Ads/AdsModel.cs b/Runtime/GuruCore/Runtime/Ads/AdsModel.cs new file mode 100644 index 0000000..d93cfbd --- /dev/null +++ b/Runtime/GuruCore/Runtime/Ads/AdsModel.cs @@ -0,0 +1,127 @@ +namespace Guru +{ + using UnityEngine; + + + public class AdsModel + { + private AdsModelStorage _storage; + + public int radsRewardCount = 0; + public double tchAd001Value = 0; + public double tchAd02Value = 0; + + + public int RadsRewardCount + { + get => radsRewardCount; + set + { + radsRewardCount = value; + Save(); + } + } + + public double TchAD001RevValue + { + get => tchAd001Value; + set + { + tchAd001Value = value; + Save(); + } + } + + public double TchAD02RevValue + { + get => tchAd02Value; + set + { + tchAd02Value = value; + Save(); + } + } + + + + private void Save() + { + _storage.Save(ToJson()); + } + + public string ToJson() + { + return JsonUtility.ToJson(this); + } + + + + public static AdsModel Create() + { + AdsModel model; + AdsModelStorage storage; + AdsModelStorage.Create(out model, out storage); + model._storage = storage; + return model; + } + + + } + + + internal class AdsModelStorage : MonoBehaviour + { + private const string INSTANCE_NAME = "GuruSDK"; + private bool _needToSave = false; + private string _jsonData = ""; + + public static void Create(out AdsModel model, out AdsModelStorage storage) + { + model = null; + storage = null; + + var go = GameObject.Find(INSTANCE_NAME); + if (go == null) go = new GameObject(INSTANCE_NAME); + + AdsModelStorage _ins = null; + if (!go.TryGetComponent(out storage)) + { + storage = go.AddComponent(); + } + + string json = PlayerPrefs.GetString(nameof(AdsModel), ""); + if (!string.IsNullOrEmpty(json)) + { + model = JsonUtility.FromJson(json); + } + else + { + model = new AdsModel(); + } + } + + + + public void Save(string json) + { + _jsonData = json; + _needToSave = true; + } + + #region 生命周期 + + void Update() + { + if (_needToSave) + { + PlayerPrefs.SetString(nameof(AdsModel), _jsonData); + _needToSave = false; + } + } + + + #endregion + } + + +} \ No newline at end of file diff --git a/Runtime/GuruCore/Runtime/Ads/AdsModel.cs.meta b/Runtime/GuruCore/Runtime/Ads/AdsModel.cs.meta new file mode 100644 index 0000000..4da6777 --- /dev/null +++ b/Runtime/GuruCore/Runtime/Ads/AdsModel.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b436508368c24a179b99a5aec61d1f0b +timeCreated: 1704713051 \ No newline at end of file diff --git a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs index 8726f34..31489bb 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs @@ -100,8 +100,7 @@ namespace Guru if (_hasGotAdjustId) return; string adjustId = AdjustService.AdjustId; - if (!string.IsNullOrEmpty(adjustId) - && string.IsNullOrEmpty(IPMConfig.ADJUST_ID)) + if (!string.IsNullOrEmpty(adjustId)) { IPMConfig.ADJUST_ID = adjustId; } @@ -132,13 +131,8 @@ namespace Guru // Debug.Log($"---[ANA] ADID: {adId}"); IPMConfig.ADJUST_ADID = adId; } - else - { - AdjustService.FetchAdId(); - } - #else - // ============= ADID is not supported on Adjust iOS API ============== + // ============= ADID is not supported on Adjust iOS API ============== IPMConfig.ADJUST_ADID = VALUE_NOT_FOR_IOS;; #endif