using System; using UnityEditor; using UnityEngine; using SerializableCallback; namespace Unity.Tutorials.Core.Editor { /// /// Allows tutorial author to specify arbitrary completion criterion. /// Create a new ScriptableObject for your criterion and provide e.g. "bool IsMyCriterionSatisfied()" /// function as Callback to evalute the criterion. Provide a function that completes your criterion as /// AutoCompleteCallback if you wish to be able to auto-complete the page. /// public class ArbitraryCriterion : Criterion { /// /// Needed for serialization. /// [Serializable] public class BoolCallback : SerializableCallback { } /// /// The callback for criterion evalution logic. /// public BoolCallback Callback { get => m_Callback; set => m_Callback = value; } [SerializeField] BoolCallback m_Callback = default; /// /// The callback for auto-completion logic. /// public BoolCallback AutoCompleteCallback { get => m_AutoCompleteCallback; set => m_AutoCompleteCallback = value; } [SerializeField] BoolCallback m_AutoCompleteCallback = default; /// /// Evaluates if the criterion is completed. /// /// protected override bool EvaluateCompletion() { // TODO revisit the logic here -- should AutoCompleteCallback take a precedence over Callback? // Or set some internal state to completed state? if (m_Callback != null) return m_Callback.Invoke(); else return false; } /// /// Starts testing of the criterion. /// public override void StartTesting() { UpdateCompletion(); EditorApplication.update += UpdateCompletion; } /// /// Stops testing of the criterion. /// public override void StopTesting() { EditorApplication.update -= UpdateCompletion; } /// /// Auto-completes the criterion. /// /// True if the auto-completion succeeded. public override bool AutoComplete() { if (m_AutoCompleteCallback != null) return m_AutoCompleteCallback.Invoke(); else return false; } } }