暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RecompileScripts.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using UnityEditor;
  4. namespace UnityEngine.TestTools
  5. {
  6. /// <summary>
  7. /// `RecompileScripts` is an <see cref="IEditModeTestYieldInstruction"/> that you can yield in Edit Mode tests. It lets you trigger a recompilation of scripts in the Unity Editor.
  8. /// </summary>
  9. public class RecompileScripts : IEditModeTestYieldInstruction
  10. {
  11. /// <summary>
  12. /// Creates a new instance of the `RecompileScripts` yield instruction.
  13. /// <example>
  14. /// <code>
  15. /// [UnitySetUp]
  16. /// public IEnumerator SetUp()
  17. /// {
  18. /// using (var file = File.CreateText("Assets/temp/myScript.cs"))
  19. /// {
  20. /// file.Write("public class ATempClass { }");
  21. /// }
  22. /// AssetDatabase.Refresh();
  23. /// yield return new RecompileScripts();
  24. /// }
  25. /// </code>
  26. /// </example>
  27. /// </summary>
  28. public RecompileScripts() : this(true)
  29. {
  30. }
  31. /// <summary>
  32. /// Creates a new instance of the `RecompileScripts` yield instruction.
  33. /// </summary>
  34. /// <param name="expectScriptCompilation">This parameter indicates if you expect a script compilation to start (defaults to true). If a script compilation does not start and `expectScriptCompilation` is true, then it throws an exception.</param>
  35. public RecompileScripts(bool expectScriptCompilation) : this(expectScriptCompilation, true)
  36. {
  37. }
  38. /// <summary>
  39. /// Creates a new instance of the `RecompileScripts` yield instruction.
  40. /// </summary>
  41. /// <param name="expectScriptCompilation">This parameter indicates if you expect a script compilation to start (defaults to true). If a script compilation does not start and `expectScriptCompilation` is `true`, then it throws an exception.</param>
  42. /// <param name="expectScriptCompilationSuccess">This parameter indicates if you expect a script compilation to succeed. If not succeeded then an exception will be thrown.</param>
  43. public RecompileScripts(bool expectScriptCompilation, bool expectScriptCompilationSuccess)
  44. {
  45. ExpectScriptCompilation = expectScriptCompilation;
  46. ExpectScriptCompilationSuccess = expectScriptCompilationSuccess;
  47. ExpectDomainReload = true;
  48. }
  49. /// <summary>
  50. /// Returns true if the instruction expects a domain reload to occur.
  51. /// </summary>
  52. public bool ExpectDomainReload { get; private set; }
  53. /// <summary>
  54. /// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
  55. /// </summary>
  56. public bool ExpectedPlaymodeState { get; }
  57. /// <summary>
  58. /// Indicates whether a script compilation is expected.
  59. /// </summary>
  60. public bool ExpectScriptCompilation { get; private set; }
  61. /// <summary>
  62. /// Indicates whether the expected script compilation is expected to succeed.
  63. /// </summary>
  64. public bool ExpectScriptCompilationSuccess { get; private set; }
  65. /// <summary>
  66. /// The current active instance of the RecompileScripts yield instruction.
  67. /// </summary>
  68. public static RecompileScripts Current { get; private set; }
  69. /// <summary>
  70. /// Perform the multi step instruction of triggering a recompilation of scripts and waiting for its completion.
  71. /// </summary>
  72. /// <returns>An IEnumerator with the async steps.</returns>
  73. /// <exception cref="Exception">Throws an exception if the editor does not need to recompile scripts or if the script compilation failed when expected to succeed.</exception>
  74. public IEnumerator Perform()
  75. {
  76. Current = this;
  77. // We need to yield, to give the test runner a chance to prepare for the domain reload
  78. // If the script compilation happens very fast, then EditModeRunner.MoveNextAndUpdateYieldObject will not have a chance to set m_CurrentYieldObject
  79. // This really should be fixed in EditModeRunner.MoveNextAndUpdateYieldObject
  80. yield return null;
  81. AssetDatabase.Refresh();
  82. if (ExpectScriptCompilation && !EditorApplication.isCompiling)
  83. {
  84. Current = null;
  85. throw new Exception("Editor does not need to recompile scripts");
  86. }
  87. EditorApplication.UnlockReloadAssemblies();
  88. while (EditorApplication.isCompiling)
  89. {
  90. yield return null;
  91. }
  92. Current = null;
  93. if (ExpectScriptCompilationSuccess && EditorUtility.scriptCompilationFailed)
  94. {
  95. EditorApplication.LockReloadAssemblies();
  96. throw new Exception("Script compilation failed");
  97. }
  98. }
  99. }
  100. }