Esempio n. 1
0
        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;
            }
        void UpdateFutureReferences(UnityObject prefabInstance)
        {
            UnityObject prefabParent = PrefabUtilityShim.GetCorrespondingObjectFromSource(prefabInstance);

            foreach (var futurePrefabInstance in m_FuturePrefabInstances)
            {
                if (futurePrefabInstance.prefabParent == prefabParent)
                {
                    futurePrefabInstance.futureReference.sceneObjectReference.Update(prefabInstance);
                }
            }
        }
        void OnSelectionChanged()
        {
            if (completed)
            {
                return;
            }

            foreach (var gameObject in Selection.gameObjects)
            {
                if (PrefabUtilityShim.GetCorrespondingObjectFromSource(gameObject) != null)
                {
                    var prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject);
                    if (prefabInstanceRoot == gameObject && m_ExistingPrefabInstances.Add(prefabInstanceRoot.GetInstanceID()))
                    {
                        OnPrefabInstantiated(prefabInstanceRoot);
                    }
                }
            }
        }
        public override void StartTesting()
        {
            // Record existing prefab instances
            m_ExistingPrefabInstances.Clear();
            foreach (var gameObject in UnityObject.FindObjectsOfType <GameObject>())
            {
                if (PrefabUtilityShim.GetCorrespondingObjectFromSource(gameObject) != null)
                {
                    var prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject);
                    m_ExistingPrefabInstances.Add(prefabInstanceRoot.GetInstanceID());
                }
            }

            Selection.selectionChanged += OnSelectionChanged;

            if (completed)
            {
                EditorApplication.update += OnUpdateWhenCompleted;
            }

            UpdateCompletion();
        }
Esempio n. 6
0
        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);
        }
        void OnPrefabInstantiated(GameObject prefabInstanceRoot)
        {
            if (m_PrefabParent == null)
            {
                return;
            }

            if (PrefabUtilityShim.GetCorrespondingObjectFromSource(prefabInstanceRoot) == m_PrefabParent)
            {
                foreach (var component in prefabInstanceRoot.GetComponentsInChildren <Component>())
                {
                    UpdateFutureReferences(component);

                    if (component is Transform)
                    {
                        UpdateFutureReferences(component.gameObject);
                    }
                }

                m_PrefabInstance = prefabInstanceRoot.GetInstanceID();

                UpdateCompletion();
            }
        }