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"); Debug.Log($"[Ads] --- PubMatic is not enabled");
return; return;
} }
if (string.IsNullOrEmpty(PMStoreUrl))
{
Debug.Log($"[Ads] --- PubMatic with empty store url. skip initialize...");
return;
}
var appInfo = new POBApplicationInfo(); var appInfo = new POBApplicationInfo();
appInfo.StoreURL = new Uri(PMStoreUrl); appInfo.StoreURL = new Uri(PMStoreUrl);

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
using System.Collections;
namespace Guru namespace Guru
{ {
using System; using System;
@ -136,6 +138,7 @@ namespace Guru
{ {
Log.I(TAG, $"eventName:{eventName}"); Log.I(TAG, $"eventName:{eventName}");
CustomLogEvent(eventName); // 自定义打点上报 CustomLogEvent(eventName); // 自定义打点上报
CheckLogCache(eventName, null, eventSetting); // log缓存和消费
if (!IsEnable) return; if (!IsEnable) return;
@ -167,6 +170,8 @@ namespace Guru
{ {
Log.I(TAG, $"eventName:{eventName}, params:{string.Join(",", extras)}"); Log.I(TAG, $"eventName:{eventName}, params:{string.Join(",", extras)}");
CustomLogEvent(eventName, extras); // 自定义打点上报 CustomLogEvent(eventName, extras); // 自定义打点上报
CheckLogCache(eventName, extras, eventSetting); // log缓存和消费
if (!IsEnable) return; if (!IsEnable) return;
@ -263,6 +268,70 @@ namespace Guru
} }
#endregion #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 string text = "";
public byte[] data = null; public byte[] data = null;
public Texture2D texture = null; public Texture2D texture = null;
public AssetBundle assetBundle = null;
internal static LoadResult Create(LoadTask task, bool success = false, string error = "") internal static LoadResult Create(LoadTask task, bool success = false, string error = "")
{ {

View File

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

View File

@ -147,6 +147,7 @@ namespace Guru
ProductSetting item; ProductSetting item;
IDs ids; IDs ids;
bool emptyIDs = false;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
item = settings[i]; item = settings[i];
@ -155,12 +156,32 @@ namespace Guru
{ {
ids.Add(item.GooglePlayProductId, GooglePlay.Name); 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)) if (!string.IsNullOrEmpty(item.AppStoreProductId))
{ {
ids.Add(item.AppStoreProductId, AppleAppStore.Name); 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); // 添加商品 _configBuilder.AddProduct(item.ProductId, item.Type, ids); // 添加商品
// 建立本地的商品信息列表 // 建立本地的商品信息列表

View File

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