GetPropertyModifications() private method

private GetPropertyModifications ( Object targetPrefab ) : UnityEditor.PropertyModification[]
targetPrefab Object
return UnityEditor.PropertyModification[]
 public static void OnOverridenPrefabsInspector(GameObject gameObject)
 {
     GUI.enabled = true;
     UnityEngine.Object prefabObject = PrefabUtility.GetPrefabObject(gameObject);
     if (prefabObject == null)
     {
         return;
     }
     EditorGUIUtility.labelWidth = 200f;
     if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.PrefabInstance)
     {
         PropertyModification[] propertyModifications = PrefabUtility.GetPropertyModifications(gameObject);
         if (propertyModifications != null && propertyModifications.Length != 0)
         {
             GUI.changed = false;
             for (int i = 0; i < propertyModifications.Length; i++)
             {
                 propertyModifications[i].value = EditorGUILayout.TextField(propertyModifications[i].propertyPath, propertyModifications[i].value, new GUILayoutOption[0]);
             }
             if (GUI.changed)
             {
                 PrefabUtility.SetPropertyModifications(gameObject, propertyModifications);
             }
         }
     }
     PrefabInspector.AddComponentGUI(prefabObject);
 }
Example #2
0
        public static void OnOverridenPrefabsInspector(GameObject gameObject)
        {
            GUI.enabled = true;

            var prefab = PrefabUtility.GetPrefabObject(gameObject);

            if (prefab == null)
            {
                return;
            }

            EditorGUIUtility.labelWidth = 200;

            if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.PrefabInstance)
            {
                PropertyModification[] modifications = PrefabUtility.GetPropertyModifications(gameObject);
                if (modifications != null && modifications.Length != 0)
                {
                    GUI.changed = false;
                    for (int i = 0; i < modifications.Length; i++)
                    {
                        modifications[i].value = EditorGUILayout.TextField(modifications[i].propertyPath, modifications[i].value);
                    }

                    if (GUI.changed)
                    {
                        PrefabUtility.SetPropertyModifications(gameObject, modifications);
                    }
                }
            }

            AddComponentGUI(prefab);
        }
        // Returns true if overrides count changed during the update
        public bool MoveNext()
        {
            if (IsDone())
            {
                return(false);
            }

            var assetRoot  = m_PrefabAssetRoots[m_CurrentAssetIndex];
            var startCount = m_OverridesCount[m_CurrentAssetIndex];

            switch (m_CurrentStep)
            {
            case 0:
            {
                // First check if we have cached overides count for the prefab
                var path = AssetDatabase.GetAssetPath(assetRoot);
                var guid = AssetDatabase.AssetPathToGUID(path);
                var cachedPrefabAssetInfo = PrefabAssetStateCache.GetState(guid);
                if (cachedPrefabAssetInfo != null)
                {
                    m_OverridesCount[m_CurrentAssetIndex] = cachedPrefabAssetInfo.overrideCount;
                    m_ChangedCount = true;
                    m_CurrentAssetIndex++;
                    if (m_Debug)
                    {
                        Debug.Log($"[OverridesCounterForPrefabAssets] Using cached overridecount {cachedPrefabAssetInfo.overrideCount} ({path})");
                    }
                    return(true);
                }
            }
            break;

            case 1:
                PropertyModification[] mods = PrefabUtility.GetPropertyModifications(assetRoot);
                foreach (PropertyModification mod in mods)
                {
                    if (mod.target == null)
                    {
                        continue;
                    }

                    if (!PrefabUtility.IsDefaultOverride(mod))
                    {
                        m_OverridesCount[m_CurrentAssetIndex]++;
                    }
                }
                break;

            case 2:
                m_OverridesCount[m_CurrentAssetIndex] += PrefabOverridesUtility.GetAddedComponents(assetRoot).Count;
                break;

            case 3:
                m_OverridesCount[m_CurrentAssetIndex] += PrefabOverridesUtility.GetRemovedComponents(assetRoot).Count;
                break;

            case 4:
                m_OverridesCount[m_CurrentAssetIndex] += PrefabOverridesUtility.GetAddedGameObjects(assetRoot).Count;
                break;

            default:
            {
                // Cache result
                var path            = AssetDatabase.GetAssetPath(assetRoot);
                var guid            = AssetDatabase.AssetPathToGUID(path);
                var prefabAssetInfo = new PrefabAssetInfo();
                prefabAssetInfo.overrideCount  = m_OverridesCount[m_CurrentAssetIndex];
                prefabAssetInfo.prefabFileHash = AssetDatabase.GetSourceAssetFileHash(guid);
                PrefabAssetStateCache.SetState(guid, prefabAssetInfo);
                if (m_Debug)
                {
                    Debug.Log($"[OverridesCounterForPrefabAssets] Set cached overridecount {prefabAssetInfo.overrideCount} for {path}");
                }

                // Move to next asset
                m_CurrentAssetIndex++;
                m_CurrentStep = 0;
            }
            break;
            }

            if (m_Debug)
            {
                Debug.Log($"[OverridesCounterForPrefabAssets] Current asset index: {m_CurrentAssetIndex}, current step: {m_CurrentStep}");
            }

            m_CurrentStep++;

            // Simulate heavy calculation
            if (m_Debug)
            {
                System.Threading.Thread.Sleep(500);
            }

            m_ChangedCount = m_OverridesCount[m_CurrentAssetIndex] != startCount;

            return(true);
        }
        // Returns true if input gameobject or any of its descendants have modifications, otherwise returns false.
        bool AddTreeViewItemRecursive(TreeViewItem parentItem, GameObject gameObject, Dictionary <int, PrefabOverrides> prefabOverrideMap, IdSequence idSequence)
        {
            var gameObjectItem = new PrefabOverridesTreeViewItem
                                 (
                gameObject.GetInstanceID(),
                parentItem.depth + 1,
                gameObject.name
                                 );

            gameObjectItem.obj = gameObject;

            // We don't know yet if this item should be added to the parent.
            bool shouldAddGameObjectItemToParent = false;

            PrefabOverrides objectModifications;

            prefabOverrideMap.TryGetValue(gameObject.GetInstanceID(), out objectModifications);
            if (objectModifications != null)
            {
                // Added GameObject - note that this earlies out!
                AddedGameObject addedGameObjectData = objectModifications.addedGameObjects.Find(x => x.instanceGameObject == gameObject);
                if (addedGameObjectData != null)
                {
                    gameObjectItem.singleModification = addedGameObjectData;
                    gameObjectItem.type = ItemType.ADDED_OBJECT;

                    parentItem.AddChild(gameObjectItem);
                    return(true);
                }
                else
                {
                    // Modified GameObject
                    ObjectOverride modifiedGameObjectData = objectModifications.objectOverrides.Find(x => x.instanceObject == gameObject);
                    if (modifiedGameObjectData != null)
                    {
                        gameObjectItem.singleModification = modifiedGameObjectData;
                        gameObjectItem.type             = ItemType.PREFAB_OBJECT;
                        shouldAddGameObjectItemToParent = true;
                    }
                }

                // Added components and component modifications
                foreach (var component in gameObject.GetComponents(typeof(Component)))
                {
                    // GetComponents will return Missing Script components as null, we will skip them here to prevent NullReferenceExceptions. (case 1197599)
                    if (component == null)
                    {
                        continue;
                    }

                    // Skip coupled components (they are merged into the display of their owning component)
                    if (component.IsCoupledComponent())
                    {
                        continue;
                    }

                    var componentItem = new PrefabOverridesTreeViewItem
                                        (
                        component.GetInstanceID(),
                        gameObjectItem.depth + 1,
                        ObjectNames.GetInspectorTitle(component)
                                        );
                    componentItem.obj = component;

                    AddedComponent addedComponentData = objectModifications.addedComponents.Find(x => x.instanceComponent == component);
                    if (addedComponentData != null)
                    {
                        // Skip coupled components (they are merged into the display of their owning component)
                        if (addedComponentData.instanceComponent.IsCoupledComponent())
                        {
                            continue;
                        }

                        componentItem.singleModification = addedComponentData;
                        componentItem.type = ItemType.ADDED_OBJECT;
                        gameObjectItem.AddChild(componentItem);
                        shouldAddGameObjectItemToParent = true;
                    }
                    else
                    {
                        var            coupledComponent          = component.GetCoupledComponent();
                        ObjectOverride modifiedObjectData        = objectModifications.objectOverrides.Find(x => x.instanceObject == component);
                        ObjectOverride modifiedCoupledObjectData = (coupledComponent != null) ? objectModifications.objectOverrides.Find(x => x.instanceObject == coupledComponent) : null;

                        if (modifiedObjectData != null || modifiedCoupledObjectData != null)
                        {
                            // If only the coupled component has modifications, create an
                            // ObjectOverride object for the main component since it doesn't exist yet.
                            if (modifiedObjectData == null)
                            {
                                modifiedObjectData = new ObjectOverride()
                                {
                                    instanceObject = component
                                }
                            }
                            ;

                            modifiedObjectData.coupledOverride = modifiedCoupledObjectData;

                            componentItem.singleModification = modifiedObjectData;
                            componentItem.type = ItemType.PREFAB_OBJECT;
                            gameObjectItem.AddChild(componentItem);
                            shouldAddGameObjectItemToParent = true;
                        }
                    }
                }

                // Removed components
                foreach (var removedComponent in objectModifications.removedComponents)
                {
                    // Skip coupled components (they are merged into the display of their owning component)
                    if (removedComponent.assetComponent.IsCoupledComponent())
                    {
                        continue;
                    }

                    var removedComponentItem = new PrefabOverridesTreeViewItem
                                               (
                        idSequence.get(),
                        gameObjectItem.depth + 1,
                        ObjectNames.GetInspectorTitle(removedComponent.assetComponent)
                                               );
                    removedComponentItem.obj = removedComponent.assetComponent;
                    removedComponentItem.singleModification = removedComponent;
                    removedComponentItem.type = ItemType.REMOVED_OBJECT;
                    gameObjectItem.AddChild(removedComponentItem);
                    shouldAddGameObjectItemToParent = true;
                }
            }

            // Recurse into children
            foreach (Transform childTransform in gameObject.transform)
            {
                var childGameObject = childTransform.gameObject;
                shouldAddGameObjectItemToParent |= AddTreeViewItemRecursive(gameObjectItem, childGameObject, prefabOverrideMap, idSequence);
            }

            if (objectModifications != null)
            {
                // Removed GameObjects
                foreach (var removedGameObject in objectModifications.removedGameObjects)
                {
                    string objectName            = removedGameObject.assetGameObject.name;
                    var    instanceModifications = PrefabUtility.GetPropertyModifications(gameObject);
                    foreach (var mod in instanceModifications)
                    {
                        if (mod.target == removedGameObject.assetGameObject && mod.propertyPath == "m_Name")
                        {
                            objectName = mod.value;
                            break;
                        }
                    }

                    var removedGameObjectItem = new PrefabOverridesTreeViewItem
                                                (
                        idSequence.get(),
                        gameObjectItem.depth + 1,
                        objectName
                                                );
                    removedGameObjectItem.obj = removedGameObject.assetGameObject;
                    removedGameObjectItem.singleModification = removedGameObject;
                    removedGameObjectItem.type = ItemType.REMOVED_OBJECT;
                    gameObjectItem.AddChild(removedGameObjectItem);
                    shouldAddGameObjectItemToParent = true;
                }
            }

            if (shouldAddGameObjectItemToParent)
            {
                parentItem.AddChild(gameObjectItem);
                if (maxDepthItem == null || gameObjectItem.depth > maxDepthItem.depth)
                {
                    maxDepthItem = gameObjectItem;
                }

                return(true);
            }

            return(false);
        }