using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Unity.Tutorials.Core.Editor
{
///
/// Criterion for checking that specific objects are selected.
///
public class RequiredSelectionCriterion : Criterion
{
[Serializable]
class ObjectReferenceCollection : CollectionWrapper
{
}
[SerializeField]
ObjectReferenceCollection m_ObjectReferences = new ObjectReferenceCollection();
///
/// Sets object references.
///
///
public void SetObjectReferences(IEnumerable objectReferences)
{
m_ObjectReferences.SetItems(objectReferences);
UpdateCompletion();
}
///
/// Starts testing of the criterion.
///
public override void StartTesting()
{
UpdateCompletion();
Selection.selectionChanged += UpdateCompletion;
}
///
/// Stops testing of the criterion.
///
public override void StopTesting()
{
Selection.selectionChanged -= UpdateCompletion;
}
///
/// Evaluates if the criterion is completed.
///
///
protected override bool EvaluateCompletion()
{
if (m_ObjectReferences.Count() != Selection.objects.Length)
return false;
foreach (var objectReference in m_ObjectReferences)
{
var referencedObject = objectReference.SceneObjectReference.ReferencedObject;
if (referencedObject == null)
return false;
if (!Selection.objects.Contains(referencedObject))
return false;
}
return true;
}
///
/// Auto-completes the criterion.
///
/// True if the auto-completion succeeded.
public override bool AutoComplete()
{
var referencedObjects = m_ObjectReferences.Select(or => or.SceneObjectReference.ReferencedObject);
if (referencedObjects.Any(obj => obj == null))
{
Debug.LogWarning("Cannot auto-complete RequiredSelectionCriterion with unresolved object references");
return false;
}
Selection.objects = referencedObjects.ToArray();
return true;
}
}
}