Exemple #1
0
    static int SearchPrefabConnections(ApplyOrRevertDelegate inDelegate, string inDescriptor)
    {
        var count = 0;

        if (inDelegate != null)
        {
            var selectedGameObjects = Selection.gameObjects;
            if (selectedGameObjects.Length > 0)
            {
                foreach (var gameObject in selectedGameObjects)
                {
                    var prefabType = PrefabUtility.GetPrefabType(gameObject);

                    // Is the selected GameObject a prefab?
                    if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
                    {
                        // Get the prefab root.
                        var prefabParent = ((GameObject)PrefabUtility.GetPrefabParent(gameObject));
                        var prefabRoot   = prefabParent.transform.root.gameObject;

                        var currentGameObject      = gameObject;
                        var hasFoundTopOfHierarchy = false;
                        var canApply = true;

                        // We go up in the hierarchy until we locate a GameObject that doesn't have the same GetPrefabParent return value.
                        while (currentGameObject.transform.parent && !hasFoundTopOfHierarchy)
                        {
                            // Same prefab?
                            prefabParent = ((GameObject)PrefabUtility.GetPrefabParent(currentGameObject.transform.parent.gameObject));
                            if (prefabParent && prefabRoot == prefabParent.transform.root.gameObject)
                            {
                                // Continue upwards.
                                currentGameObject = currentGameObject.transform.parent.gameObject;
                            }
                            else
                            {
                                //The gameobject parent is another prefab, we stop here
                                hasFoundTopOfHierarchy = true;
                                if (prefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(currentGameObject)))
                                {
                                    //Gameobject is part of another prefab
                                    canApply = false;
                                }
                            }
                        }

                        if (canApply)
                        {
                            count++;
                            var parent = PrefabUtility.GetPrefabParent(currentGameObject);
                            inDelegate(currentGameObject, parent, ReplacePrefabOptions.ConnectToPrefab);
                            var assetPath = AssetDatabase.GetAssetPath(parent);
                            Debug.Log(assetPath + " " + inDescriptor, parent);
                        }
                    }
                }
                Debug.Log(count + " prefab" + (count > 1 ? "s" : "") + " updated");
            }
        }

        return(count);
    }
Exemple #2
0
    static int SearchPrefabConnections(ApplyOrRevertDelegate inDelegate, string inDescriptor)
    {
        var count = 0;
        if(inDelegate != null)
        {
            var selectedGameObjects = Selection.gameObjects;
            if(selectedGameObjects.Length > 0)
            {
                foreach(var gameObject in selectedGameObjects)
                {
                    var prefabType = PrefabUtility.GetPrefabType(gameObject);

                    // Is the selected GameObject a prefab?
                    if(prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
                    {
                        // Get the prefab root.
                        var prefabParent = ((GameObject)PrefabUtility.GetPrefabParent(gameObject));
                        var prefabRoot = prefabParent.transform.root.gameObject;

                        var currentGameObject = gameObject;
                        var hasFoundTopOfHierarchy = false;
                        var canApply = true;

                        // We go up in the hierarchy until we locate a GameObject that doesn't have the same GetPrefabParent return value.
                        while(currentGameObject.transform.parent && !hasFoundTopOfHierarchy)
                        {
                            // Same prefab?
                            prefabParent = ((GameObject)PrefabUtility.GetPrefabParent(currentGameObject.transform.parent.gameObject));
                            if(prefabParent && prefabRoot == prefabParent.transform.root.gameObject)
                            {
                                // Continue upwards.
                                currentGameObject = currentGameObject.transform.parent.gameObject;
                            }
                            else
                            {
                                //The gameobject parent is another prefab, we stop here
                                hasFoundTopOfHierarchy = true;
                                if(prefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(currentGameObject)))
                                {
                                    //Gameobject is part of another prefab
                                    canApply = false;
                                }
                            }
                        }

                        if(canApply)
                        {
                            count++;
                            var parent = PrefabUtility.GetPrefabParent(currentGameObject);
                            inDelegate(currentGameObject, parent, ReplacePrefabOptions.ConnectToPrefab);
                            var assetPath = AssetDatabase.GetAssetPath(parent);
                            Debug.Log(assetPath + " " + inDescriptor, parent);
                        }
                    }
                }
                Debug.Log(count + " prefab" + (count > 1 ? "s" : "") + " updated");
            }
        }

        return count;
    }