using System;
using UnityEditor;
using UnityEngine;
namespace Unity.Tutorials.Core.Editor
{
///
/// Criterion for checking a specific Play Mode state.
///
public class PlayModeStateCriterion : Criterion
{
enum PlayModeState
{
Playing,
NotPlaying
}
[SerializeField]
PlayModeState m_RequiredPlayModeState = PlayModeState.Playing;
///
/// Starts testing of the criterion.
///
public override void StartTesting()
{
UpdateCompletion();
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
///
/// Stops testing of the criterion.
///
public override void StopTesting()
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
}
void OnPlayModeStateChanged(PlayModeStateChange playModeStateChange)
{
switch (playModeStateChange)
{
case PlayModeStateChange.EnteredPlayMode:
case PlayModeStateChange.EnteredEditMode:
UpdateCompletion();
break;
}
}
///
/// Evaluates if the criterion is completed.
///
///
protected override bool EvaluateCompletion()
{
switch (m_RequiredPlayModeState)
{
case PlayModeState.NotPlaying:
return !EditorApplication.isPlaying;
case PlayModeState.Playing:
return EditorApplication.isPlaying;
default:
return false;
}
}
///
/// Auto-completes the criterion.
///
/// True if the auto-completion succeeded.
public override bool AutoComplete()
{
EditorApplication.isPlaying = m_RequiredPlayModeState == PlayModeState.Playing;
return true;
}
}
}