EnterPlayModeHook.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #if !UNITY_2020_3_OR_NEWER
  2. // make sure we weaved successfully when entering play mode.
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. public class EnterPlayModeSettingsCheck : MonoBehaviour
  8. {
  9. [InitializeOnLoadMethod]
  10. static void OnInitializeOnLoad()
  11. {
  12. // Hook this event to see if we have a good weave every time
  13. // user attempts to enter play mode or tries to do a build
  14. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  15. }
  16. static void OnPlayModeStateChanged(PlayModeStateChange state)
  17. {
  18. // Per Unity docs, this fires "when exiting edit mode before the Editor is in play mode".
  19. // This doesn't fire when closing the editor.
  20. if (state == PlayModeStateChange.ExitingEditMode)
  21. {
  22. // Check if last weave result was successful
  23. if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false))
  24. {
  25. // Last weave result was a failure...try to weave again
  26. // Faults will show in the console that may have been cleared by "Clear on Play"
  27. SessionState.SetBool("MIRROR_WEAVE_SUCCESS", true);
  28. Weaver.CompilationFinishedHook.WeaveExistingAssemblies();
  29. // Did that clear things up for us?
  30. if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false))
  31. {
  32. // Nope, still failed, and console has the issues logged
  33. Debug.LogError("Can't enter play mode until weaver issues are resolved.");
  34. EditorApplication.isPlaying = false;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. #endif