/// <summary> /// Connect copied objects to prefabs if original object is connected, and sets helper reference back to new prefab reference /// Also recursively calls itself in all children /// </summary> /// <param name="toMine">Whether we are copying theirs to mine (true) or mie to theirs (false)</param> void ConnectPrefabsAfterCopy(bool toMine) { var original = toMine ? theirs : mine; var copy = toMine ? mine : theirs; if (Util.IsPrefabParent(original) && PrefabUtility.GetPrefabType(original) == PrefabType.PrefabInstance) { copy = PrefabUtility.ConnectGameObjectToPrefab(copy, (GameObject)PrefabUtility.GetPrefabParent(original)); copy.name = original.name; // ConnectPrefab will rename to prefab name // ConnectPrefab will override transform var copyTransform = copy.transform; var originalTransform = original.transform; copyTransform.localPosition = originalTransform.localPosition; copyTransform.localRotation = originalTransform.localRotation; copyTransform.localScale = originalTransform.localScale; } if (toMine) { mine = copy; } else { theirs = copy; } if (children != null) { foreach (var child in children) { child.ConnectPrefabsAfterCopy(toMine); } } }
public override IEnumerator Refresh() { //This is where we initiate the merge for the first time var root = this.root; root.components.Clear(); root.attributes.Clear(); var children = root.children; if (children != null) { children.Clear(); } var rootMine = root.mine; var rootTheirs = root.theirs; if (!rootMine || !rootTheirs) { yield break; } #if !Unity3 //Check if the objects are prefabs switch (PrefabUtility.GetPrefabType(rootMine)) { case PrefabType.ModelPrefab: case PrefabType.Prefab: switch (PrefabUtility.GetPrefabType(rootTheirs)) { case PrefabType.ModelPrefab: case PrefabType.Prefab: if (EditorUtility.DisplayDialog("Instantiate prefabs?", "In order to merge prefabs, you must instantiate them and merge the instances. Then you must apply the changes.", "Instantiate", "Cancel")) { root.SetGameObjects( PrefabUtility.InstantiatePrefab(rootMine) as GameObject, PrefabUtility.InstantiatePrefab(rootTheirs) as GameObject); } else { root.SetGameObjects(null, null); } break; default: Debug.LogWarning("Sorry, you must compare a prefab with a prefab"); break; } break; case PrefabType.DisconnectedPrefabInstance: case PrefabType.PrefabInstance: case PrefabType.ModelPrefabInstance: case PrefabType.None: switch (PrefabUtility.GetPrefabType(rootTheirs)) { case PrefabType.DisconnectedPrefabInstance: case PrefabType.PrefabInstance: case PrefabType.ModelPrefabInstance: case PrefabType.None: break; default: Debug.LogWarning("Sorry, this prefab type is not supported"); break; } break; default: Debug.LogWarning("Sorry, this prefab type is not supported"); break; } #endif updateType = RefreshType.Updating; update = root.Refresh(); }
void MergeScenes(string myPath, string theirPath) { #if UNITY_5_3 || UNITY_5_3_OR_NEWER var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); var myScene = EditorSceneManager.OpenScene(myPath, OpenSceneMode.Additive); #else #if UNITY_5 EditorApplication.NewEmptyScene(); #else EditorApplication.NewScene(); var _allObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (var obj in _allObjects) { if (obj.transform.parent == null && PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab && PrefabUtility.GetPrefabType(obj) != PrefabType.ModelPrefab && obj.hideFlags == 0) //Want a better way to filter out "internal" objects { DestroyImmediate(obj); } } #endif EditorApplication.OpenSceneAdditive(myPath); #endif var split = myPath.Split('/'); myContainerName = split[split.Length - 1].Replace(".unity", ""); this.myContainer = new GameObject { name = myContainerName }; var myContainer = this.myContainer; Undo.RegisterCreatedObjectUndo(myContainer, "UniMerge"); var myTransform = myContainer.transform; var allObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (var obj in allObjects) { if (obj.transform.parent == null && PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab && PrefabUtility.GetPrefabType(obj) != PrefabType.ModelPrefab && obj.hideFlags == 0) //Want a better way to filter out "internal" objects { obj.transform.parent = myTransform; } } #if UNITY_5_3 || UNITY_5_3_OR_NEWER SceneManager.MergeScenes(myScene, newScene); #endif #if UNITY_5_3 || UNITY_5_3_OR_NEWER var theirScene = EditorSceneManager.OpenScene(theirPath, OpenSceneMode.Additive); SceneManager.MergeScenes(theirScene, newScene); #else EditorSceneManager.OpenSceneAdditive(theirPath); #endif split = theirPath.Split('/'); theirContainerName = split[split.Length - 1].Replace(".unity", ""); this.theirContainer = new GameObject { name = theirContainerName }; var theirContainer = this.theirContainer; Undo.RegisterCreatedObjectUndo(theirContainer, "UniMerge"); allObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (var obj in allObjects) { if (obj.transform.parent == null && obj.name != myContainerName && PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab && PrefabUtility.GetPrefabType(obj) != PrefabType.ModelPrefab && obj.hideFlags == 0) //Want a better way to filter out "internal" objects { obj.transform.parent = theirContainer.transform; } } }