using System;
using System.Collections;
using UnityEditor;
namespace UnityEngine.TestTools
{
///
/// Implements . Creates a yield instruction to enter Play Mode.
///
public class EnterPlayMode : IEditModeTestYieldInstruction
{
///
/// Returns true if the instruction expects a domain reload to occur.
///
public bool ExpectDomainReload { get; }
///
/// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
///
public bool ExpectedPlaymodeState { get; private set; }
///
/// When creating an Editor test that uses the UnityTest attribute, use this to trigger the Editor to enter Play Mode.
/// Throws an exception if the Editor is already in Play Mode or if there is a script compilation error.
///
/// A flag indication whether to expect a domain reload.
public EnterPlayMode(bool expectDomainReload = true)
{
ExpectDomainReload = expectDomainReload;
}
///
/// Performs the multi-step instructions of entering PlayMode.
///
/// An IEnumerator with the async steps.
/// An exception is thrown if the editor is already in PlayMode or if script compilation failed.
public IEnumerator Perform()
{
if (EditorApplication.isPlaying)
{
throw new Exception("Editor is already in PlayMode");
}
if (EditorUtility.scriptCompilationFailed)
{
throw new Exception("Script compilation failed");
}
yield return null;
ExpectedPlaymodeState = true;
EditorApplication.UnlockReloadAssemblies();
EditorApplication.isPlaying = true;
while (!EditorApplication.isPlaying)
{
yield return null;
}
}
}
}