EnterPlayModeSettingsCheck.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #if UNITY_2019_3_OR_NEWER
  30. // We can't support experimental "Enter Play Mode Options" mode
  31. // Check and prevent entering play mode if enabled
  32. CheckPlayModeOptions();
  33. #endif
  34. }
  35. }
  36. #if UNITY_2019_3_OR_NEWER
  37. static void CheckPlayModeOptions()
  38. {
  39. // enabling the checkbox is enough. it controls all the other settings.
  40. if (EditorSettings.enterPlayModeOptionsEnabled)
  41. {
  42. Debug.LogError("Enter Play Mode Options are not supported by Mirror. Please disable 'ProjectSettings -> Editor -> Enter Play Mode Settings (Experimental)'.");
  43. EditorApplication.isPlaying = false;
  44. }
  45. }
  46. #endif
  47. }
  48. }