Просмотр исходного кода

修复Log的bug (#441)

支持EditorMode和多线程使用
肖红 2 лет назад
Родитель
Сommit
a0dd93c58a

+ 0 - 56
Unity/Assets/Scripts/Core/Module/Log/Log.cs

@@ -6,97 +6,41 @@ namespace ET
     {
         public static void Trace(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.Log(msg);
-                return;
-            }
-#endif
             Logger.Instance.Trace(msg);
         }
 
         public static void Debug(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.Log(msg);
-                return;
-            }
-#endif
             Logger.Instance.Debug(msg);
         }
 
         public static void Info(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.Log(msg);
-                return;
-            }
-#endif
             Logger.Instance.Info(msg);
         }
 
         public static void TraceInfo(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.Log(msg);
-                return;
-            }
-#endif
             Logger.Instance.Trace(msg);
         }
 
         public static void Warning(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.LogWarning(msg);
-                return;
-            }
-#endif
             Logger.Instance.Warning(msg);
         }
 
         public static void Error(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.LogError(msg);
-                return;
-            }
-#endif      
             Logger.Instance.Error(msg);
         }
 
         public static void Error(Exception msg)
         {
-#if UNITY     
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.LogError(msg);
-                return;
-            }
- #endif          
             Logger.Instance.Error(msg);
         }
 
         public static void Console(string msg)
         {
-#if UNITY
-            if (!UnityEngine.Application.isPlaying)
-            {
-                UnityEngine.Debug.Log(msg);
-                return;
-            }
-#endif  
             Logger.Instance.Console(msg);
         }
         

+ 62 - 0
Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs

@@ -0,0 +1,62 @@
+using UnityEditor;
+
+namespace ET
+{
+    [InitializeOnLoad]
+    public class EditorLogHelper
+    {
+        static EditorLogHelper()
+        {
+            EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
+            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
+            EditorApplication.update += CheckCompolingFinish;
+        }
+
+        private static void CheckCompolingFinish()
+        {
+            if (!EditorApplication.isCompiling)
+            {
+                CreateLog();
+                EditorApplication.update -= CheckCompolingFinish;
+            }
+        }
+
+        private static void OnPlayModeStateChanged(PlayModeStateChange state)
+        {
+            switch (state)
+            {
+                case PlayModeStateChange.EnteredEditMode:
+                case PlayModeStateChange.ExitingPlayMode:
+                    CreateLog();
+                    break;
+                case PlayModeStateChange.ExitingEditMode:
+                    DestroyLog();
+                    break;
+                default:
+                    break;
+            }
+        }
+
+        private static void CreateLog()
+        {
+            if (Logger.Instance != null)
+            {
+                return;
+            }
+
+            var log = new Logger();
+            ((ISingleton)log).Register();
+            log.ILog = new UnityLogger();
+        }
+
+        private static void DestroyLog()
+        {
+            if (Logger.Instance == null)
+            {
+                return;
+            }
+
+            ((ISingleton)Logger.Instance).Destroy();
+        }
+    }
+}

+ 3 - 0
Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: be974f10ed5e4b6d9fc1556f27c1ebea
+timeCreated: 1678077897