EditorLogHelper.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEditor;
  2. namespace ET
  3. {
  4. [InitializeOnLoad]
  5. public class EditorLogHelper
  6. {
  7. static EditorLogHelper()
  8. {
  9. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  10. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  11. EditorApplication.update += CheckCompolingFinish;
  12. }
  13. private static void CheckCompolingFinish()
  14. {
  15. if (!EditorApplication.isCompiling)
  16. {
  17. CreateLog();
  18. EditorApplication.update -= CheckCompolingFinish;
  19. }
  20. }
  21. private static void OnPlayModeStateChanged(PlayModeStateChange state)
  22. {
  23. switch (state)
  24. {
  25. case PlayModeStateChange.EnteredEditMode:
  26. case PlayModeStateChange.ExitingPlayMode:
  27. CreateLog();
  28. break;
  29. case PlayModeStateChange.ExitingEditMode:
  30. DestroyLog();
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. private static void CreateLog()
  37. {
  38. if (Logger.Instance == null)
  39. {
  40. World.Instance.AddSingleton<Logger>().Log = new UnityLogger();
  41. }
  42. if (Options.Instance == null)
  43. {
  44. World.Instance.AddSingleton(new Options());
  45. }
  46. }
  47. private static void DestroyLog()
  48. {
  49. World.Instance.Dispose();
  50. }
  51. }
  52. }