ソースを参照

尝试使用takingdata来统计错误信息

lzx 19 時間 前
コミット
758d9d0f24

+ 1 - 1
Assets/Scripts/GameLogic/SDK/DBManager.cs

@@ -7,7 +7,7 @@ public class DBManager : Singleton<DBManager>
 {
     public DBManager()
     {
-        TalkingDataManager.Instance.CustomInit();
+       
     }
 
     /// <summary>

+ 9 - 8
Assets/Scripts/GameLogic/SDK/TalkingDataManager.cs

@@ -36,17 +36,18 @@ public class TalkingDataManager : Singleton<TalkingDataManager>
 
         try
         {
-            TalkingDataSDKConfig config = TalkingDataSDKConfig.CreateConfig();
-            config.SetIMEIAndMEIDEnabled(true) //不允许收集IMEI和MEID
-                .SetMACEnabled(true) //不允许收集MAC
-                .SetAppListEnabled(true) //不允许收集应用列表
-                .SetLocationEnabled(true) //不允许收集位置信息
-                .SetWifiEnabled(true); //不允许收集网络信息
-
-            TalkingDataSDK.SetConfig(config);
+            // TalkingDataSDKConfig config = TalkingDataSDKConfig.CreateConfig();
+            // config.SetIMEIAndMEIDEnabled(true) //不允许收集IMEI和MEID
+            //     .SetMACEnabled(true) //不允许收集MAC
+            //     .SetAppListEnabled(true) //不允许收集应用列表
+            //     .SetLocationEnabled(true) //不允许收集位置信息
+            //     .SetWifiEnabled(true); //不允许收集网络信息
+
+            // TalkingDataSDK.SetConfig(config);
             TalkingDataSDK.BackgroundSessionEnabled();
             TalkingDataSDK.InitSDK(appID, channelID, "");
             TalkingDataSDK.StartA();
+            TalkingDataSDK.SetReportUncaughtExceptions(true);
             isInitialized = true;
             LogTool.Log($"TalkingData 初始化成功 - AppID: {appID}, Channel: {channelID}");
         }

+ 4 - 0
Assets/Scripts/GameUI/GameApplction.cs

@@ -142,6 +142,7 @@ public class GameApplction : IGameStart, ILogSend
         CustomTweenManager.Init();
 
         RedDotManager.Instance.Init();
+        TalkingDataManager.Instance.CustomInit();
         // await GameNetworkClient.Instance.Connect("127.0.0.1", 1000);
         await Login();
     }
@@ -304,6 +305,8 @@ public class GameApplction : IGameStart, ILogSend
         if (_cache.Length > 100 * 1024 || GetLineCount(_cache.ToString()) > 50)
             Flush();
         Crasheye.SendScriptException(massge, massge, "");
+
+        TalkingDataSDK.OnError(massge, massge, LogType.Error);
     }
 
     private void Flush()
@@ -327,6 +330,7 @@ public class GameApplction : IGameStart, ILogSend
         _cache.AppendLine($"{e.Message} {e.StackTrace}");
         Flush();
         Crasheye.SendScriptException(e);
+        TalkingDataSDK.ReportException(e);
     }
 
     public void UnityLogic(string logString, string stackTrace, LogType type)

+ 91 - 9
Assets/Scripts/ThirdParty/TalkingDataScripts/TalkingDataSDK.cs

@@ -5,6 +5,7 @@ using System.Collections.Generic;
 #if UNITY_ANDROID
 using System;
 #endif
+
 #if UNITY_IPHONE
 using System.Runtime.InteropServices;
 using System.Collections;
@@ -183,6 +184,7 @@ public static class TalkingDataSDK
         {
             unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
         }
+
         AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
         return activity;
     }
@@ -194,7 +196,8 @@ public static class TalkingDataSDK
         {
             int count = parameters.Count;
             map = new AndroidJavaObject("java.util.HashMap", count);
-            IntPtr method_Put = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+            IntPtr method_Put = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put",
+                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
             object[] args = new object[2];
             foreach (KeyValuePair<string, object> kvp in parameters)
             {
@@ -205,6 +208,7 @@ public static class TalkingDataSDK
                 AndroidJNI.CallObjectMethod(map.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
             }
         }
+
         return map;
     }
 #endif
@@ -250,6 +254,7 @@ public static class TalkingDataSDK
             {
                 talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
             }
+
             talkingdataClass.CallStatic("setConfig", config.javaObj);
 #endif
         }
@@ -265,10 +270,12 @@ public static class TalkingDataSDK
             {
                 dz.SetStatic("a", 2);
             }
+
             if (talkingdataClass == null)
             {
                 talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
             }
+
             AndroidJavaObject activity = GetCurrentActivity();
             talkingdataClass.CallStatic("initSDK", activity, appId, channelId, custom);
 #endif
@@ -320,37 +327,45 @@ public static class TalkingDataSDK
     }
 
     private static string deviceId = null;
+
     public static string GetDeviceId()
     {
-        if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
+        if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor &&
+            Application.platform != RuntimePlatform.WindowsEditor)
         {
 #if UNITY_ANDROID
             if (talkingdataClass == null)
             {
                 talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
             }
+
             deviceId = talkingdataClass.CallStatic<string>("getDeviceId", GetCurrentActivity());
 #endif
 #if UNITY_IPHONE
             deviceId = TDGetDeviceId();
 #endif
         }
+
         return deviceId;
     }
 
     private static string oaid = null;
+
     public static string GetOAID()
     {
-        if (oaid == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
+        if (oaid == null && Application.platform != RuntimePlatform.OSXEditor &&
+            Application.platform != RuntimePlatform.WindowsEditor)
         {
 #if UNITY_ANDROID
             if (talkingdataClass == null)
             {
                 talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
             }
+
             oaid = talkingdataClass.CallStatic<string>("getOAID", GetCurrentActivity());
 #endif
         }
+
         return oaid;
     }
 
@@ -363,6 +378,7 @@ public static class TalkingDataSDK
             {
                 talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
             }
+
             talkingdataClass.CallStatic("setVerboseLogDisable");
 #endif
 #if UNITY_IPHONE
@@ -429,7 +445,8 @@ public static class TalkingDataSDK
         }
     }
 
-    public static void OnRegister(string profileId, TalkingDataProfile profile, string invitationCode, Dictionary<string, object> eventValue)
+    public static void OnRegister(string profileId, TalkingDataProfile profile, string invitationCode,
+        Dictionary<string, object> eventValue)
     {
         if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
         {
@@ -483,11 +500,12 @@ public static class TalkingDataSDK
                 talkingdataClass.CallStatic("onProfileUpdate", profile.javaObj);
             }
 #endif
-#if  UNITY_IPHONE
+#if UNITY_IPHONE
             TDOnProfileUpdate(profile.ToString());
 #endif
         }
     }
+
     public static void OnCreateCard(string profileId, string method, string content)
     {
         if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
@@ -992,7 +1010,8 @@ public static class TalkingDataSDK
     }
 #endif
 
-    public static void OnEvent(string eventId, Dictionary<string, object> parameters, Dictionary<string, object> eventValue)
+    public static void OnEvent(string eventId, Dictionary<string, object> parameters,
+        Dictionary<string, object> eventValue)
     {
         if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
         {
@@ -1006,6 +1025,7 @@ public static class TalkingDataSDK
                 {
                     parametersMap.Dispose();
                 }
+
                 if (eventValueMap != null)
                 {
                     eventValueMap.Dispose();
@@ -1020,6 +1040,68 @@ public static class TalkingDataSDK
         }
     }
 
+    /// <summary>
+    /// 手动报告错误
+    /// </summary>
+    public static void OnError(string message, string stackTrace, LogType logType)
+    {
+        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
+        {
+#if UNITY_ANDROID
+            if (talkingdataClass != null)
+            {
+                string exceptionMessage = $"{logType}: {message}\n{stackTrace}";
+                using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception", exceptionMessage))
+                {
+                    talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
+                }
+            }
+#endif
+        }
+    }
+
+    /// <summary>
+    /// 手动报告异常
+    /// </summary>
+    public static void ReportException(System.Exception exception)
+    {
+        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
+        {
+#if UNITY_ANDROID
+            try
+            {
+                using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception",
+                           $"{exception.Message}\n{exception.StackTrace}"))
+                {
+                    talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
+                }
+            }
+            catch (System.Exception e)
+            {
+                Debug.LogError("报告异常到TalkingData失败: " + e.Message);
+            }
+#endif
+        }
+    }
+
+
+    /// <summary>
+    /// 设置是否自动捕获未处理异常
+    /// </summary>
+    /// <param name="enabled">true:开启, false:关闭</param>
+    public static void SetReportUncaughtExceptions(bool enabled)
+    {
+        try
+        {
+            talkingdataClass.CallStatic("setReportUncaughtExceptions", enabled);
+            Debug.Log($"TalkingData自动异常捕获: {(enabled ? "开启" : "关闭")}");
+        }
+        catch (System.Exception e)
+        {
+            Debug.LogError("设置TalkingData自动异常捕获失败: " + e.Message);
+        }
+    }
+
     public static void SetGlobalKV(string key, object val)
     {
         if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
@@ -1028,8 +1110,8 @@ public static class TalkingDataSDK
             if (talkingdataClass != null)
             {
                 AndroidJavaObject valObject = typeof(string).IsInstanceOfType(val)
-                                            ? new AndroidJavaObject("java.lang.String", val)
-                                            : new AndroidJavaObject("java.lang.Double", "" + val);
+                    ? new AndroidJavaObject("java.lang.String", val)
+                    : new AndroidJavaObject("java.lang.Double", "" + val);
                 talkingdataClass.CallStatic("setGlobalKV", key, valObject);
             }
 #endif
@@ -1063,4 +1145,4 @@ public static class TalkingDataSDK
 #endif
         }
     }
-}
+}