No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EnterPlayMode.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections;
  3. using UnityEditor;
  4. namespace UnityEngine.TestTools
  5. {
  6. /// <summary>
  7. /// Implements <see cref="IEditModeTestYieldInstruction"/>. Creates a yield instruction to enter Play Mode.
  8. /// </summary>
  9. public class EnterPlayMode : IEditModeTestYieldInstruction
  10. {
  11. /// <summary>
  12. /// Returns true if the instruction expects a domain reload to occur.
  13. /// </summary>
  14. public bool ExpectDomainReload { get; }
  15. /// <summary>
  16. /// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
  17. /// </summary>
  18. public bool ExpectedPlaymodeState { get; private set; }
  19. /// <summary>
  20. /// When creating an Editor test that uses the UnityTest attribute, use this to trigger the Editor to enter Play Mode.
  21. /// Throws an exception if the Editor is already in Play Mode or if there is a script compilation error.
  22. /// </summary>
  23. /// <param name="expectDomainReload">A flag indication whether to expect a domain reload.</param>
  24. public EnterPlayMode(bool expectDomainReload = true)
  25. {
  26. ExpectDomainReload = expectDomainReload;
  27. }
  28. /// <summary>
  29. /// Performs the multi-step instructions of entering PlayMode.
  30. /// </summary>
  31. /// <returns>An IEnumerator with the async steps.</returns>
  32. /// <exception cref="Exception">An exception is thrown if the editor is already in PlayMode or if script compilation failed.</exception>
  33. public IEnumerator Perform()
  34. {
  35. if (EditorApplication.isPlaying)
  36. {
  37. throw new Exception("Editor is already in PlayMode");
  38. }
  39. if (EditorUtility.scriptCompilationFailed)
  40. {
  41. throw new Exception("Script compilation failed");
  42. }
  43. yield return null;
  44. ExpectedPlaymodeState = true;
  45. EditorApplication.UnlockReloadAssemblies();
  46. EditorApplication.isPlaying = true;
  47. while (!EditorApplication.isPlaying)
  48. {
  49. yield return null;
  50. }
  51. }
  52. }
  53. }