static PrefabOverrides GetPrefabOverrides(GameObject prefabInstance, bool includeDefaultOverrides = false)
        {
            if (!PrefabUtility.IsPartOfNonAssetPrefabInstance(prefabInstance))
            {
                Debug.LogError("GeneratePrefabOverrides should only be called with GameObjects that are part of a prefab");
                return(new PrefabOverrides());
            }

            var mods = new PrefabOverrides();

            mods.objectOverrides   = PrefabOverridesUtility.GetObjectOverrides(prefabInstance, includeDefaultOverrides);
            mods.addedComponents   = PrefabOverridesUtility.GetAddedComponents(prefabInstance);
            mods.removedComponents = PrefabOverridesUtility.GetRemovedComponents(prefabInstance);
            mods.addedGameObjects  = PrefabOverridesUtility.GetAddedGameObjects(prefabInstance);
            return(mods);
        }
        static GameObject[] GetOutermostPrefabInstancesFromSelection()
        {
            var gos         = new List <GameObject>();
            var gameObjects = Selection.gameObjects;

            for (int i = 0; i < gameObjects.Length; i++)
            {
                var go = gameObjects[i];
                if (go != null && PrefabUtility.IsPartOfNonAssetPrefabInstance(go) && PrefabUtility.IsOutermostPrefabInstanceRoot(go))
                {
                    gos.Add(go);
                }
            }

            return(gos.ToArray());
        }
示例#3
0
        internal static UInt64 GetOrGenerateFileIDHint(UnityEngine.Object obj)
        {
            UInt64 fileID = Unsupported.GetFileIDHint(obj);

            if (fileID == 0)
            {
                // GenerateFileIDHint only work on saved nested prefabs instances.
                var instanceHandle = PrefabUtility.GetPrefabInstanceHandle(obj);
                if (instanceHandle != null)
                {
                    bool isPrefabInstanceSaved = Unsupported.GetFileIDHint(instanceHandle) != 0;
                    if (isPrefabInstanceSaved && PrefabUtility.IsPartOfNonAssetPrefabInstance(obj) && PrefabUtility.GetPrefabAssetType(obj) != PrefabAssetType.MissingAsset)
                    {
                        fileID = Unsupported.GenerateFileIDHint(obj);
                    }
                }
            }

            return(fileID);
        }
示例#4
0
        internal static GameObject FindSelectionBase(GameObject go)
        {
            if (go == null)
            {
                return(null);
            }

            // Find prefab based base
            Transform prefabBase = null;

            if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))
            {
                prefabBase = PrefabUtility.GetOutermostPrefabInstanceRoot(go).transform;
            }

            // Find attribute based base
            Transform tr = go.transform;

            while (tr != null)
            {
                // If we come across the prefab base, no need to search further down.
                if (tr == prefabBase)
                {
                    return(tr.gameObject);
                }

                // If this one has the attribute, return this one.
                if (AttributeHelper.GameObjectContainsAttribute <SelectionBaseAttribute>(tr.gameObject))
                {
                    return(tr.gameObject);
                }

                tr = tr.parent;
            }

            // There is neither a prefab or attribute based selection root, so return null
            return(null);
        }
        internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)
        {
            if (gameObjects == null)
            {
                throw new ArgumentNullException("gameObjects");
            }

            if (gameObjects.Length == 0)
            {
                throw new ArgumentException("gameObjects array is empty");
            }

            if (targetPrefab == null)
            {
                throw new ArgumentNullException("targetPrefab");
            }

            if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))
            {
                throw new ArgumentException("Target Prefab has to be a Prefab Asset");
            }

            Object targetPrefabInstance = null;

            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                {
                    throw new ArgumentException("GameObject in input 'gameObjects' array is null");
                }

                if (EditorUtility.IsPersistent(go))  // Prefab asset
                {
                    throw new ArgumentException("Game object is part of a prefab");
                }

                var parentPrefabInstance = GetParentPrefabInstance(go);
                if (parentPrefabInstance == null)
                {
                    throw new ArgumentException("GameObject is not (directly) parented under a target Prefab instance.");
                }

                if (targetPrefabInstance == null)
                {
                    targetPrefabInstance = parentPrefabInstance;
                    if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))
                    {
                        throw new ArgumentException("GameObject is not parented under a target Prefab instance.");
                    }
                }
                else
                {
                    if (parentPrefabInstance != targetPrefabInstance)
                    {
                        throw new ArgumentException("GameObjects must be parented under the same Prefab instance.");
                    }
                }

                if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))
                {
                    var correspondingGO             = PrefabUtility.GetCorrespondingObjectFromSource(go);
                    var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO);
                    if (targetPrefabObject == correspondingGOPrefabObject)
                    {
                        throw new ArgumentException("GameObject is already part of target prefab");
                    }
                }
            }

            string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab));

            if (!VerifyNestingFromScript(gameObjects, prefabGUID, null))
            {
                throw new ArgumentException("Cyclic nesting detected");
            }

            AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab);
        }