/// <summary> /// Evaluates if the criterion is completed. /// </summary> /// <returns></returns> protected override bool EvaluateCompletion() { if (PrefabParent == null) { return(false); } var matches = FindObjectsOfType <GameObject>().Where(go => PrefabUtilityShim.GetCorrespondingObjectFromSource(go) == PrefabParent); var count = matches.Count(); switch (ComparisonMode) { case InstanceCountComparison.AtLeast: return(count >= InstanceCount); case InstanceCountComparison.Exactly: var complete = count == InstanceCount; if (complete && InstanceCount == 1 && m_FutureReference != null) { m_FutureReference.SceneObjectReference.Update(matches.First()); } return(complete); case InstanceCountComparison.NoMoreThan: return(count <= InstanceCount); default: return(false); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { position.height = GetPropertyHeight(property, label); var prefabParentProperty = property.FindPropertyRelative(k_PrefabParentPropertyPath); var obj = prefabParentProperty.objectReferenceValue; EditorGUI.BeginProperty(position, GUIContent.none, prefabParentProperty); EditorGUI.BeginChangeCheck(); var newObj = EditorGUI.ObjectField(position, obj, typeof(UnityObject), true); if (EditorGUI.EndChangeCheck()) { // Replace prefab instance with its prefab parent if (newObj != null && PrefabUtility.GetPrefabInstanceStatus(newObj) != PrefabInstanceStatus.NotAPrefab) { newObj = PrefabUtilityShim.GetCorrespondingObjectFromSource(newObj); } prefabParentProperty.objectReferenceValue = newObj; } EditorGUI.EndProperty(); position.y += position.height + EditorGUIUtility.standardVerticalSpacing; }
/// <summary> /// Auto-completes the criterion. /// </summary> /// <returns>True if the auto-completion succeeded.</returns> public override bool AutoComplete() { var prefabInstances = FindObjectsOfType <GameObject>().Where(go => PrefabUtilityShim.GetCorrespondingObjectFromSource(go) == PrefabParent); var actualInstanceCount = prefabInstances.Count(); var difference = actualInstanceCount - InstanceCount; if (difference == 0) { return(true); } switch (ComparisonMode) { case InstanceCountComparison.AtLeast: difference = Math.Min(0, difference); break; case InstanceCountComparison.NoMoreThan: difference = Math.Max(0, difference); break; } if (difference < 0) { for (var i = 0; i < -difference; i++) { PrefabUtility.InstantiatePrefab(PrefabParent); } } else { foreach (var prefabInstance in prefabInstances.Take(difference)) { DestroyImmediate(prefabInstance); } } return(true); }