Skip to content

Commit

Permalink
Fixed issue with copier creating another avatar hierarchy our own
Browse files Browse the repository at this point in the history
  • Loading branch information
rurre committed Feb 28, 2024
1 parent b6ed066 commit 95953d9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
39 changes: 30 additions & 9 deletions Editor/Copiers/GenericCopier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -225,29 +229,47 @@ 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<Transform> ignoreSet)
{
List<GameObject> allPrefabRoots = new List<GameObject>();
List<GameObject> prefabs = new List<GameObject>();
foreach(var trans in inst.from.GetComponentsInChildren<Transform>(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<GameObject> prefabs = new HashSet<GameObject>(allPrefabRoots);
List<Transform> newPrefabTransformsToIgnore = addPrefabsToIgnoreList ? new List<Transform>() : null;

foreach(var fromPref in prefabs)
{
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;
Expand All @@ -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<GameObject>(prefabAssetPath)) as GameObject;
if(!toPref)
continue;
Expand Down
9 changes: 4 additions & 5 deletions Editor/Helpers/PumkinsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Pumkin.HelperFunctions;
using Pumkin.AvatarTools;
using Pumkin.HelperFunctions;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}");
}
}
}
Expand Down Expand Up @@ -176,7 +175,7 @@ public static bool IsNullOrEmpty<T>(this T[] array)
{
return array == null || array.Length == 0;
}

/// <summary>
/// Invokes <paramref name="action"/> for each visible property of <paramref name="so"/>
/// </summary>
Expand Down
41 changes: 21 additions & 20 deletions Editor/Helpers/PumkinsHelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "/";
}

/// <summary>
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions Editor/PumkinsAvatarTools_UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Pumkin.HelperFunctions;
using Pumkin.Presets;
using UnityEngine.Animations;
using UnityEngine.SceneManagement;

namespace Pumkin.AvatarTools
{
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 95953d9

Please sign in to comment.