RecompileScripts.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections;
  3. using UnityEditor;
  4. namespace UnityEngine.TestTools
  5. {
  6. public class RecompileScripts : IEditModeTestYieldInstruction
  7. {
  8. public RecompileScripts() : this(true)
  9. {
  10. }
  11. public RecompileScripts(bool expectScriptCompilation) : this(expectScriptCompilation, true)
  12. {
  13. }
  14. public RecompileScripts(bool expectScriptCompilation, bool expectScriptCompilationSuccess)
  15. {
  16. ExpectScriptCompilation = expectScriptCompilation;
  17. ExpectScriptCompilationSuccess = expectScriptCompilationSuccess;
  18. ExpectDomainReload = true;
  19. }
  20. public bool ExpectDomainReload { get; private set; }
  21. public bool ExpectedPlaymodeState { get; }
  22. public bool ExpectScriptCompilation { get; private set; }
  23. public bool ExpectScriptCompilationSuccess { get; private set; }
  24. public static RecompileScripts Current { get; private set; }
  25. public IEnumerator Perform()
  26. {
  27. Current = this;
  28. // We need to yield, to give the test runner a chance to prepare for the domain reload
  29. // If the script compilation happens very fast, then EditModeRunner.MoveNextAndUpdateYieldObject will not have a chance to set m_CurrentYieldObject
  30. // This really should be fixed in EditModeRunner.MoveNextAndUpdateYieldObject
  31. yield return null;
  32. AssetDatabase.Refresh();
  33. if (ExpectScriptCompilation && !EditorApplication.isCompiling)
  34. {
  35. Current = null;
  36. throw new Exception("Editor does not need to recompile scripts");
  37. }
  38. EditorApplication.UnlockReloadAssemblies();
  39. while (EditorApplication.isCompiling)
  40. {
  41. yield return null;
  42. }
  43. Current = null;
  44. if (ExpectScriptCompilationSuccess && EditorUtility.scriptCompilationFailed)
  45. {
  46. EditorApplication.LockReloadAssemblies();
  47. throw new Exception("Script compilation failed");
  48. }
  49. }
  50. }
  51. }