From 95953d91eb54afd5c22ba1e081f3444f9953c0e8 Mon Sep 17 00:00:00 2001 From: rurre Date: Wed, 28 Feb 2024 18:42:15 +0200 Subject: [PATCH] Fixed issue with copier creating another avatar hierarchy our own --- Editor/Copiers/GenericCopier.cs | 39 ++++++++++++++++------ Editor/Helpers/PumkinsExtensions.cs | 9 +++--- Editor/Helpers/PumkinsHelperFunctions.cs | 41 ++++++++++++------------ Editor/PumkinsAvatarTools_UI.cs | 15 +++++++++ 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/Editor/Copiers/GenericCopier.cs b/Editor/Copiers/GenericCopier.cs index 31761d5..677f4d6 100644 --- a/Editor/Copiers/GenericCopier.cs +++ b/Editor/Copiers/GenericCopier.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -167,7 +168,7 @@ public static void FixInstanceReferences(CopyInstance inst) public static void FixReferencesOnComponent(Component newComp, Transform currentHierarchyRoot, Transform targetHierarchyRoot, bool createGameObjects) { - if(!newComp) + if(!newComp || newComp is Transform) return; var serialComp = new SerializedObject(newComp); @@ -186,6 +187,9 @@ public static void FixReferencesOnComponent(Component newComp, Transform current .ToList() .IndexOf(oldComp); + if(oldComp.gameObject.scene.name == null) // Don't fix if we're referencing an asset + return; + var transTarget = Helpers.FindTransformInAnotherHierarchy(oldComp.transform, currentHierarchyRoot, targetHierarchyRoot, createGameObjects); if(transTarget == null) return; @@ -225,18 +229,17 @@ static void AdjustScale(Component newComp, Transform oldCompTransform, params st public static void CopyPrefabs(CopyInstance inst, bool createGameObjects, bool adjustScale, bool fixReferences, bool copyPropertyOverrides, bool addPrefabsToIgnoreList, HashSet ignoreSet) { - List allPrefabRoots = new List(); + List prefabs = new List(); foreach(var trans in inst.from.GetComponentsInChildren(true)) { var pref = PrefabUtility.GetNearestPrefabInstanceRoot(trans); - if(pref && pref.transform != inst.from.transform) - allPrefabRoots.Add(pref); + if(pref && pref.transform != inst.from.transform && !prefabs.Contains(pref)) + prefabs.Add(pref); } Transform tTo = inst.to.transform; Transform tFrom = inst.from.transform; - HashSet prefabs = new HashSet(allPrefabRoots); List newPrefabTransformsToIgnore = addPrefabsToIgnoreList ? new List() : null; foreach(var fromPref in prefabs) @@ -244,10 +247,29 @@ public static void CopyPrefabs(CopyInstance inst, bool createGameObjects, bool a if(Helpers.ShouldIgnoreObject(fromPref.transform, ignoreSet, Settings.bCopier_ignoreArray_includeChildren)) continue; + var prefabStatus = PrefabUtility.GetPrefabInstanceStatus(fromPref); + if(prefabStatus == PrefabInstanceStatus.MissingAsset) + { + PumkinsAvatarTools.Log($"_Tried to copy prefab with missing asset. {fromPref.name}. Skipping", LogType.Warning); + + if(addPrefabsToIgnoreList) + ignoreSet.Add(fromPref.transform); + + continue; + } + else if(prefabStatus == PrefabInstanceStatus.NotAPrefab) + { + PumkinsAvatarTools.Log($"_Prefab Copier tried to copy object {fromPref.name}, which isn't a prefab. Uh oh.. Skipping"); + continue; + } + + PumkinsAvatarTools.Log($"_Attempting to copy prefab {fromPref.name}"); + Transform tToParent = null; - tToParent = fromPref.transform.parent == tFrom - ? tTo - : Helpers.FindTransformInAnotherHierarchy(fromPref.transform.parent, inst.from.transform, inst.to.transform, createGameObjects); + if(fromPref.transform.parent == tFrom) + tToParent = tTo; + else + tToParent = Helpers.FindTransformInAnotherHierarchy(fromPref.transform.parent, inst.from.transform, inst.to.transform, createGameObjects); if(!tToParent) continue; @@ -257,7 +279,6 @@ public static void CopyPrefabs(CopyInstance inst, bool createGameObjects, bool a prefabMods = PrefabUtility.GetPropertyModifications(fromPref); string prefabAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(fromPref); - GameObject toPref = PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(prefabAssetPath)) as GameObject; if(!toPref) continue; diff --git a/Editor/Helpers/PumkinsExtensions.cs b/Editor/Helpers/PumkinsExtensions.cs index c878e52..306022d 100644 --- a/Editor/Helpers/PumkinsExtensions.cs +++ b/Editor/Helpers/PumkinsExtensions.cs @@ -1,4 +1,5 @@ -using Pumkin.HelperFunctions; +using Pumkin.AvatarTools; +using Pumkin.HelperFunctions; using System; using System.Collections; using System.Collections.Generic; @@ -26,14 +27,11 @@ public static Transform Find(this Transform transform, string name, bool createI path += (arr[i] + '/'); var path2 = (arr[i] + (arr.Length > 1 ? "/" : "")); var tNew = transform.Find(path); - var tNew2 = transform.Find(path2); if(tNew == null) { string s = Helpers.GetPathNoName(path); - string ss = Helpers.GetPathNoName(path2); var parent = transform.Find(s); - var parent2 = transform.Find(ss); if(!parent) return null; @@ -63,6 +61,7 @@ public static Transform Find(this Transform transform, string name, bool createI tNew.localScale = Vector3.one; } t = tNew; + PumkinsAvatarTools.LogVerbose($"Created new object {tNew.name}"); } } } @@ -176,7 +175,7 @@ public static bool IsNullOrEmpty(this T[] array) { return array == null || array.Length == 0; } - + /// /// Invokes for each visible property of /// diff --git a/Editor/Helpers/PumkinsHelperFunctions.cs b/Editor/Helpers/PumkinsHelperFunctions.cs index 81fe3ec..05e6683 100644 --- a/Editor/Helpers/PumkinsHelperFunctions.cs +++ b/Editor/Helpers/PumkinsHelperFunctions.cs @@ -922,25 +922,8 @@ public static string GetTransformPath(Transform trans, Transform root, bool skip if(!trans) return string.Empty; - StringBuilder sb = new StringBuilder(); - if(trans != root) - { - if(!skipRoot) - { - sb.Append(trans.name); - sb.Append('/'); - } - sb.Append(AnimationUtility.CalculateTransformPath(trans, root)); - } - else - { - if(!skipRoot) - { - sb.Clear(); - sb.Append(root.name); - } - } - return sb.ToString(); + string path = AnimationUtility.CalculateTransformPath(trans, root); + return skipRoot ? path : root.name + "/"; } /// @@ -1056,7 +1039,25 @@ public static Transform FindTransformInAnotherHierarchy(Transform trans, Transfo if(trans == currentHierarchyRoot) return otherHierarchyRoot; - var childPath = GetTransformPath(trans, currentHierarchyRoot); + Transform targetHierarchy = otherHierarchyRoot; + Transform nextTransform = trans; + do // Figure out if the transform we're looking for is part of our current or the other hierarchy + { + if(nextTransform == currentHierarchyRoot) + { + targetHierarchy = currentHierarchyRoot; + break; + } + else if(nextTransform == otherHierarchyRoot) + { + targetHierarchy = otherHierarchyRoot; + break; + } + nextTransform = nextTransform.parent; + } + while(nextTransform != null); + + var childPath = GetTransformPath(trans, targetHierarchy); var childTrans = otherHierarchyRoot.Find(childPath, createIfMissing, trans); return childTrans; diff --git a/Editor/PumkinsAvatarTools_UI.cs b/Editor/PumkinsAvatarTools_UI.cs index ace174e..de2b9c2 100644 --- a/Editor/PumkinsAvatarTools_UI.cs +++ b/Editor/PumkinsAvatarTools_UI.cs @@ -12,6 +12,7 @@ using Pumkin.HelperFunctions; using Pumkin.Presets; using UnityEngine.Animations; +using UnityEngine.SceneManagement; namespace Pumkin.AvatarTools { @@ -421,6 +422,20 @@ void DrawCopierMenuGUI() if(Selection.activeGameObject != null && GetAvatarFromSceneSelection(false, out GameObject avatar)) CopierSelectedFrom = avatar; +#if PUMKIN_DEV + if(GUILayout.Button("Auto Select")) + { + var objs = SceneManager.GetActiveScene().GetRootGameObjects(); + foreach(var obj in objs) + { + if(obj.name.ToLower().Contains("copy from")) + CopierSelectedFrom = obj; + else if(obj.name.ToLower().Contains("copy to")) + SelectedAvatar = obj; + } + } +#endif + if(_copierArmatureScalesDontMatch == true) EditorGUILayout.LabelField(Strings.Warning.armatureScalesDontMatch, Styles.HelpBox_OneLine); }