EnterPlayModeSettingsCheck.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Unity 2019.3 has an experimental 'disable domain reload on play'
  2. // feature. keeping any global state between sessions will break
  3. // Mirror and most of our user's projects. don't allow it for now.
  4. // https://blogs.unity3d.com/2019/11/05/enter-play-mode-faster-in-unity-2019-3/
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Mirror
  8. {
  9. public class EnterPlayModeSettingsCheck : MonoBehaviour
  10. {
  11. [InitializeOnLoadMethod]
  12. static void OnInitializeOnLoad()
  13. {
  14. #if UNITY_2019_3_OR_NEWER
  15. // We can't support experimental "Enter Play Mode Options" mode
  16. // Check immediately on load, and before entering play mode, and warn the user
  17. CheckPlayModeOptions();
  18. #endif
  19. // Hook this event to see if we have a good weave every time
  20. // user attempts to enter play mode or tries to do a build
  21. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  22. }
  23. static void OnPlayModeStateChanged(PlayModeStateChange state)
  24. {
  25. // Per Unity docs, this fires "when exiting edit mode before the Editor is in play mode".
  26. // This doesn't fire when closing the editor.
  27. if (state == PlayModeStateChange.ExitingEditMode)
  28. {
  29. CheckSuccessfulWeave();
  30. #if UNITY_2019_3_OR_NEWER
  31. // We can't support experimental "Enter Play Mode Options" mode
  32. // Check and prevent entering play mode if enabled
  33. CheckPlayModeOptions();
  34. #endif
  35. }
  36. }
  37. static void CheckSuccessfulWeave()
  38. {
  39. // Check if last weave result was successful
  40. if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false))
  41. {
  42. // Last weave result was a failure...try to weave again
  43. // Faults will show in the console that may have been cleared by "Clear on Play"
  44. SessionState.SetBool("MIRROR_WEAVE_SUCCESS", true);
  45. Weaver.CompilationFinishedHook.WeaveExistingAssemblies();
  46. // Did that clear things up for us?
  47. if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false))
  48. {
  49. // Nope, still failed, and console has the issues logged
  50. Debug.LogError("Can't enter play mode until weaver issues are resolved.");
  51. EditorApplication.isPlaying = false;
  52. }
  53. }
  54. }
  55. #if UNITY_2019_3_OR_NEWER
  56. static void CheckPlayModeOptions()
  57. {
  58. // enabling the checkbox is enough. it controls all the other settings.
  59. if (EditorSettings.enterPlayModeOptionsEnabled)
  60. {
  61. Debug.LogError("Enter Play Mode Options are not supported by Mirror. Please disable 'ProjectSettings -> Editor -> Enter Play Mode Settings (Experimental)'.");
  62. EditorApplication.isPlaying = false;
  63. }
  64. }
  65. #endif
  66. }
  67. }