277 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			277 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| #if USE_FIREBASE
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using Firebase;
 | |
| using Firebase.Analytics;
 | |
| using Firebase.RemoteConfig;
 | |
| using Touka;
 | |
| 
 | |
| public class FirebaseTool : NormalSingleton<FirebaseTool>
 | |
| {
 | |
|     public event Action OnInitializeDone;
 | |
|     public event Action OnRemoteConfigSucceed;
 | |
|     public event Action OnRemoteConfigFail;
 | |
| 
 | |
|     public bool IsReady => mIsReady;
 | |
|     private bool mIsReady = false;
 | |
|     private bool mIsDefaultConfigReady = false;
 | |
|     private bool mIsRemoteConfigReady = false;
 | |
|     private bool mIsConfigDone => mIsDefaultConfigReady && mIsRemoteConfigReady;
 | |
| 
 | |
|     private Dictionary<string, object> mConfigDic = new Dictionary<string, object>();
 | |
| 
 | |
|     public void Initialize()
 | |
|     {
 | |
|         foreach (string tKey in ToukaInnerParams.OnlineParamDic.Keys)//加入touka内置参数
 | |
|         {
 | |
|             mConfigDic.Add(tKey, ToukaInnerParams.OnlineParamDic[tKey]);
 | |
|         }
 | |
| 
 | |
|         foreach (string tKey in TKGParams.OnlineParamDic.Keys)//加入自定义参数
 | |
|         {
 | |
|             mConfigDic.Add(tKey, TKGParams.OnlineParamDic[tKey]);
 | |
|         }
 | |
| 
 | |
|         FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(pTask =>
 | |
|         {
 | |
|             DependencyStatus tStatus = pTask.Result;
 | |
|             if (tStatus == DependencyStatus.Available)
 | |
|             {
 | |
|                 InitDone();
 | |
| #if ANDROID_GP
 | |
|                 FirebaseAnalytics.SetUserProperty("ALLOW_AD_PERSONALIZATION_SIGNALS", "true");
 | |
|                 FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
 | |
| #endif
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 mIsReady = false;
 | |
|                 Debug.LogError(string.Format("Could not resolve all Firebase dependencies: {0}", tStatus));
 | |
|             }
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void InitDone()
 | |
|     {
 | |
|         Debug.Log("Firebase init successfully!!");
 | |
|         mIsReady = true;
 | |
|         FirebaseApp.LogLevel = LogLevel.Debug;
 | |
| 
 | |
|         OnInitializeDone?.Invoke();
 | |
| 
 | |
|         InitRemoteConfig();
 | |
|     }
 | |
| 
 | |
|     private void InitRemoteConfig()
 | |
|     {
 | |
|         FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(mConfigDic).ContinueWith(t =>
 | |
|         {
 | |
|             mIsDefaultConfigReady = true;
 | |
|             Debug.Log("firebase default config ready");
 | |
|         });
 | |
|         FirebaseRemoteConfig.DefaultInstance.FetchAndActivateAsync().ContinueWith(t =>
 | |
|         {
 | |
|             mIsRemoteConfigReady = true;
 | |
|             Debug.Log("firebase remote config ready");
 | |
| 
 | |
|             ConfigInfo tInfo = FirebaseRemoteConfig.DefaultInstance.Info;
 | |
|             switch (tInfo.LastFetchStatus)
 | |
|             {
 | |
|                 case LastFetchStatus.Success:
 | |
|                     Debug.Log(string.Format("Remote data loaded and ready (last fetch time {0}).", tInfo.FetchTime));
 | |
| 
 | |
|                     string tKVLog = "";
 | |
|                     foreach (string tKey in TKGParams.OnlineParamDic.Keys)
 | |
|                     {
 | |
|                         tKVLog = tKey + "_" + TKGSDKManager.Instance.GetConfigStr(tKey);
 | |
|                         if (tKVLog.Length <= 64)
 | |
|                         {
 | |
|                             TKGSDKManager.Instance.LogEvent(tKVLog);
 | |
|                         }
 | |
|                         Debug.Log("TKG get firebase remote config is: " + tKVLog);
 | |
|                     }
 | |
|                     foreach (string tKey in ToukaInnerParams.OnlineParamDic.Keys)
 | |
|                     {
 | |
|                         tKVLog = tKey + "_" + TKGSDKManager.Instance.GetConfigStr(tKey);
 | |
|                         if (tKVLog.Length <= 64)
 | |
|                         {
 | |
|                             TKGSDKManager.Instance.LogEvent(tKVLog);
 | |
|                         }
 | |
|                         Debug.Log("TKG get firebase remote config is: " + tKVLog);
 | |
|                     }
 | |
| 
 | |
|                     OnRemoteConfigSucceed?.Invoke();
 | |
|                     break;
 | |
|                 case LastFetchStatus.Failure:
 | |
|                     switch (tInfo.LastFetchFailureReason)
 | |
|                     {
 | |
|                         case FetchFailureReason.Error:
 | |
|                             Debug.Log("Fetch failed for unknown reason");
 | |
|                             break;
 | |
|                         case FetchFailureReason.Throttled:
 | |
|                             Debug.Log("Fetch throttled until " + tInfo.ThrottledEndTime);
 | |
|                             break;
 | |
|                     }
 | |
|                     OnRemoteConfigFail?.Invoke();
 | |
|                     break;
 | |
|                 case LastFetchStatus.Pending:
 | |
|                     Debug.Log("Latest Fetch call still pending.");
 | |
|                     OnRemoteConfigFail?.Invoke();
 | |
|                     break;
 | |
|             }
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     public void LogEvent(string pEvtName)
 | |
|     {
 | |
|         if (!mIsReady)
 | |
|             return;
 | |
| 
 | |
|         Debug.Log("Firebase log event : " + pEvtName);
 | |
|         FirebaseAnalytics.LogEvent(pEvtName);
 | |
|     }
 | |
| 
 | |
|     public void LogEvent(string pEvtName, string pEvtParam, string pEvtValue)
 | |
|     {
 | |
|         if (!mIsReady)
 | |
|             return;
 | |
| 
 | |
|         FirebaseAnalytics.LogEvent(pEvtName, pEvtParam, pEvtValue);
 | |
|     }
 | |
| 
 | |
|     public void LogEvent(string pEvtName, Dictionary<string, string> pParamDic)
 | |
|     {
 | |
|         if (!mIsReady)
 | |
|             return;
 | |
| 
 | |
|         Parameter[] tParams = new Parameter[pParamDic.Count];
 | |
|         int tIndex = 0;
 | |
|         foreach (string tParamName in pParamDic.Keys)
 | |
|         {
 | |
|             tParams[tIndex] = new Parameter(tParamName, pParamDic[tParamName]);
 | |
|             tIndex++;
 | |
|         }
 | |
| 
 | |
|         FirebaseAnalytics.LogEvent(pEvtName, tParams);
 | |
|     }
 | |
| 
 | |
|     public string GetString(string pKey, string pDefault = "")
 | |
|     {
 | |
| #if UNITY_EDITOR
 | |
|         if (mConfigDic.ContainsKey(pKey))
 | |
|             return mConfigDic[pKey].ToString();
 | |
| 
 | |
|         return pDefault;
 | |
| #endif
 | |
|         if (!mConfigDic.ContainsKey(pKey))
 | |
|             return pDefault;
 | |
| 
 | |
|         if (!(mIsReady && mIsConfigDone))
 | |
|             return mConfigDic[pKey].ToString();
 | |
| 
 | |
|         return FirebaseRemoteConfig.DefaultInstance.GetValue(pKey).StringValue;
 | |
|     }
 | |
| 
 | |
|     public int GetInt(string pKey, int pDefault = 0)
 | |
|     {
 | |
| #if UNITY_EDITOR
 | |
|         if (mConfigDic.ContainsKey(pKey))
 | |
|             return int.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return pDefault;
 | |
| #endif
 | |
|         if (!mConfigDic.ContainsKey(pKey))
 | |
|             return pDefault;
 | |
| 
 | |
|         if (!(mIsReady && mIsConfigDone))
 | |
|             return int.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return (int)FirebaseRemoteConfig.DefaultInstance.GetValue(pKey).LongValue;
 | |
|     }
 | |
| 
 | |
|     public bool GetBool(string pKey, bool pDefault = false)
 | |
|     {
 | |
| #if UNITY_EDITOR
 | |
|         if (mConfigDic.ContainsKey(pKey))
 | |
|             return bool.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return pDefault;
 | |
| #endif
 | |
|         if (!mConfigDic.ContainsKey(pKey))
 | |
|             return pDefault;
 | |
| 
 | |
|         if (!(mIsReady && mIsConfigDone))
 | |
|             return bool.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return FirebaseRemoteConfig.DefaultInstance.GetValue(pKey).BooleanValue;
 | |
|     }
 | |
| 
 | |
|     public double GetDouble(string pKey, double pDefault = 0)
 | |
|     {
 | |
| #if UNITY_EDITOR
 | |
|         if (mConfigDic.ContainsKey(pKey))
 | |
|             return double.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return pDefault;
 | |
| #endif
 | |
|         if (!mConfigDic.ContainsKey(pKey))
 | |
|             return pDefault;
 | |
| 
 | |
|         if (!(mIsReady && mIsConfigDone))
 | |
|             return double.Parse(mConfigDic[pKey].ToString());
 | |
| 
 | |
|         return FirebaseRemoteConfig.DefaultInstance.GetValue(pKey).DoubleValue;
 | |
|     }
 | |
| 
 | |
| #if ANDROID_GP
 | |
|     private const string AD_REVENUE = "ad_revenue";
 | |
| 
 | |
|     private const string EVENT_SINGLE_REVENUE = "Single_Ads_Revenue";
 | |
|     private const string EVENT_ACCUMULATIVE_REVENUE = "Total_Ads_Revenue";
 | |
| 
 | |
|     public void RecordAdRevenue(MaxSdkBase.AdInfo pAdInfo, string pAdType)
 | |
|     {
 | |
|         if (pAdInfo.Revenue > 0)
 | |
|         {
 | |
|             double tAddRevenue = pAdInfo.Revenue;
 | |
|             //log single revenue
 | |
|             LogAdRevenueEvent(EVENT_SINGLE_REVENUE, pAdInfo, pAdType);
 | |
| 
 | |
|             //log accumulative revenue
 | |
|             float tBeforeRevenue = PlayerPrefs.GetFloat(AD_REVENUE, 0);
 | |
|             float tTotalRevenue = (float)(tAddRevenue + tBeforeRevenue);
 | |
|             if (tTotalRevenue >= 0.01)
 | |
|             {
 | |
|                 LogAdRevenueEvent(EVENT_ACCUMULATIVE_REVENUE, pAdInfo, pAdType);
 | |
|                 PlayerPrefs.SetFloat(AD_REVENUE, 0);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 PlayerPrefs.SetFloat(AD_REVENUE, tTotalRevenue);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void LogAdRevenueEvent(string pEventName, MaxSdkBase.AdInfo pAdInfo, string pAdType)
 | |
|     {
 | |
|         if (!mIsReady)
 | |
|             return;
 | |
|         if (pAdInfo != null)
 | |
|         {
 | |
|             Parameter[] tEventParams =
 | |
|             {
 | |
|                 new Parameter("ad_platform", "Max"),
 | |
|                 new Parameter("ad_source", pAdInfo.NetworkName),
 | |
|                 new Parameter("ad_unit_name", pAdInfo.AdUnitIdentifier),
 | |
|                 new Parameter(FirebaseAnalytics.ParameterCurrency,"USD"),
 | |
|                 new Parameter("value", pAdInfo.Revenue),
 | |
|                 new Parameter("ad_type",pAdType)
 | |
|             };
 | |
| 
 | |
|             FirebaseAnalytics.LogEvent(pEventName, tEventParams);
 | |
|         }
 | |
|     }
 | |
| #endif
 | |
| }
 | |
| #endif |