update: 更新和修复启动时空对象造成 Crash 的问题

deeplink
胡宇飞 2024-03-08 20:15:33 +08:00
parent c5a23e19d1
commit fde37d807a
10 changed files with 157 additions and 31 deletions

View File

@ -60,6 +60,12 @@ namespace Guru
Debug.Log($"[Ads] --- PubMatic is not enabled");
return;
}
if (string.IsNullOrEmpty(PMStoreUrl))
{
Debug.Log($"[Ads] --- PubMatic with empty store url. skip initialize...");
return;
}
var appInfo = new POBApplicationInfo();
appInfo.StoreURL = new Uri(PMStoreUrl);

View File

@ -10,6 +10,9 @@ namespace Guru
{
[Header("Tradplus 广告配置")]
public AdChannelSettings TradplusSetting;
}
}

View File

@ -49,8 +49,7 @@ namespace Guru
//------------ 以下为扩展的广告渠道 ------------------
// 请根据项目需求实现各渠道接入的逻辑
// 开启渠道需要添加对应的宏
// #if AD_AMAZON
channel = new AdChanelAmazon();
channel.Initialize();
_adChannels.Add(channel); // Amazon
@ -73,13 +72,11 @@ namespace Guru
// if (success && firstLoad) OnLoadMaxRV();
};
}
// #endif
// #if AD_PUBMATIC
channel = new AdChannelPubMatic();
channel.Initialize();
_adChannels.Add(channel); // PubMatic
// #endif
}
#endregion

View File

@ -1,11 +1,13 @@
namespace Guru
{
using UnityEngine;
using System;
public class AdsModel
{
private AdsModelStorage _storage;
internal AdsModelStorage _storage;
public int radsRewardCount = 0;
public double tchAd001Value = 0;
@ -42,12 +44,7 @@ namespace Guru
}
}
private void Save()
{
_storage.Save(ToJson());
}
private void Save() => _storage.Save();
public string ToJson()
{
@ -73,7 +70,9 @@ namespace Guru
{
private const string INSTANCE_NAME = "GuruSDK";
private bool _needToSave = false;
private string _jsonData = "";
private float _lastSavedTime = 0;
private float _saveInterval = 2;
private AdsModel _model;
public static void Create(out AdsModel model, out AdsModelStorage storage)
{
@ -98,27 +97,53 @@ namespace Guru
{
model = new AdsModel();
}
model._storage = storage;
storage._model = model;
}
public void Save(string json)
public void Save(bool forceSave = false)
{
_jsonData = json;
_needToSave = true;
_needToSave = forceSave || (Time.realtimeSinceStartup - _lastSavedTime > _saveInterval);
}
#region 生命周期
// 主线程进行写入操作
void Update()
{
if (_needToSave)
{
PlayerPrefs.SetString(nameof(AdsModel), _jsonData);
_needToSave = false;
var json = _model?.ToJson() ?? "";
if (!string.IsNullOrEmpty(json))
{
PlayerPrefs.SetString(nameof(AdsModel), json);
_needToSave = false;
_lastSavedTime = Time.realtimeSinceStartup;
}
}
}
// 监听特殊事件
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
Save( true);
}
}
// 监听特殊事件
private void OnApplicationFocus(bool hasFocus)
{
if (!hasFocus)
{
Save( true);
}
}
#endregion
}

View File

@ -24,11 +24,17 @@ namespace Guru
public static bool IsDebug { get; set; } = false;
private static bool _hasInited = false;
public static bool IsReady => _hasInited;
/// <summary>
/// 初始化Guru自打点系统 (请优先于 Firebase 初始化调用)
/// </summary>
public static void InstallGuruAnalytics(bool isDebug = false)
{
if (_hasInited) return;
try
{
#if UNITY_EDITOR
@ -49,17 +55,15 @@ namespace Guru
_reportSuccessInterval = 120; // 2分钟上报一次
UpdateAllValues();
_hasInited = true;
}
catch (Exception e)
{
Crashlytics.LogException(e);
}
}
#region 各ID上报信息
/// <summary>

View File

@ -1,5 +1,7 @@
using System.Collections;
namespace Guru
{
using System;
@ -136,6 +138,7 @@ namespace Guru
{
Log.I(TAG, $"eventName:{eventName}");
CustomLogEvent(eventName); // 自定义打点上报
CheckLogCache(eventName, null, eventSetting); // log缓存和消费
if (!IsEnable) return;
@ -167,6 +170,8 @@ namespace Guru
{
Log.I(TAG, $"eventName:{eventName}, params:{string.Join(",", extras)}");
CustomLogEvent(eventName, extras); // 自定义打点上报
CheckLogCache(eventName, extras, eventSetting); // log缓存和消费
if (!IsEnable) return;
@ -263,6 +268,70 @@ namespace Guru
}
#endregion
#region 打点缓存
private static Queue<SavedLog> _savedLogs;
internal static Queue<SavedLog> SavedLogs
{
get
{
if (_savedLogs == null) _savedLogs = new Queue<SavedLog>(20);
return _savedLogs;
}
}
private static void CheckLogCache(string key, Dictionary<string, dynamic> data = null, EventSetting setting = null)
{
try
{
if (!_isInited)
{
if (data == null) data = new Dictionary<string, dynamic>();
data["log_stamp"] = TimeUtil.GetCurrentTimeStamp().ToString();
SavedLogs.Enqueue(new SavedLog(key, data, setting));
}
else
{
int len = SavedLogs.Count;
if (len > 0)
{
while (SavedLogs.Count > 0)
{
var log = SavedLogs.Dequeue();
LogEvent(log.key, log.data, log.setting);
}
}
}
}
catch (Exception ex)
{
Crashlytics.LogException(ex);
}
}
#endregion
}
internal class SavedLog
{
public string key;
public Dictionary<string, dynamic> data;
public Analytics.EventSetting setting;
public SavedLog()
{
}
public SavedLog(string _key, Dictionary<string, dynamic> _data = null, Analytics.EventSetting _setting = null)
{
key = _key;
data = _data;
setting = _setting;
}
}
}

View File

@ -398,8 +398,6 @@ public class CDNLoader : MonoBehaviour
public string text = "";
public byte[] data = null;
public Texture2D texture = null;
public AssetBundle assetBundle = null;
internal static LoadResult Create(LoadTask task, bool success = false, string error = "")
{

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Firebase;
using Firebase.Analytics;
using Firebase.Extensions;
@ -9,6 +10,8 @@ namespace Guru
{
private static readonly string LOG_TAG = "Firebase";
private static bool _isDebug = false;
private static bool _isReady = false;
public static bool IsReady => _isReady;
public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther;
public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available;
@ -18,8 +21,9 @@ namespace Guru
GuruRepoter.Install();
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
DependencyStatus = task.Result;
if (DependencyStatus == DependencyStatus.Available)
if (DependencyStatus == DependencyStatus.Available)
{
_isReady = true;
InitializeFirebaseComp();
callback?.Invoke();
}

View File

@ -147,6 +147,7 @@ namespace Guru
ProductSetting item;
IDs ids;
bool emptyIDs = false;
for (int i = 0; i < len; i++)
{
item = settings[i];
@ -155,12 +156,32 @@ namespace Guru
{
ids.Add(item.GooglePlayProductId, GooglePlay.Name);
}
else
{
#if UNITY_ADNROID
emptyIDs = true;
LogE($"[IAP] --- GoogleProductId is empty, please check the product setting: {item.ProductName}");
#endif
}
if (!string.IsNullOrEmpty(item.AppStoreProductId))
{
ids.Add(item.AppStoreProductId, AppleAppStore.Name);
}
else
{
#if UNITY_IOS
emptyIDs = true;
LogE($"[IAP] --- AppleProductId is empty, please check the product setting: {item.ProductName}");
#endif
}
if (emptyIDs)
{
continue;
}
_configBuilder.AddProduct(item.ProductId, item.Type, ids); // 添加商品
// 建立本地的商品信息列表

View File

@ -7,7 +7,6 @@ namespace Guru
/// </summary>
public partial class GuruSettings
{
[Header("IAP 商品配置")]
public ProductSetting[] Products;