Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static CachedAttachment InstantiateWearable(this IAttachmentsAssetsCache
for (var i = 0; i < meshRenderers.Value.Count; i++)
Object.DestroyImmediate(meshRenderers.Value[i].gameObject);

// Remove unused bone GameObjects from the wearable hierarchies as we don't need them anymore
RemoveBonesGameObjects(instantiatedWearable.transform);

cachedWearable = new CachedAttachment(originalAsset, instantiatedWearable, outlineCompatible);
}

Expand All @@ -51,5 +54,45 @@ public static CachedAttachment InstantiateWearable(this IAttachmentsAssetsCache
cachedWearable.Instance.gameObject.SetActive(true);
return cachedWearable;
}

private static void RemoveBonesGameObjects(Transform wearableRoot)
{
using PoolExtensions.Scope<List<Renderer>> pooledList =
wearableRoot.gameObject.GetComponentsInChildrenIntoPooledList<Renderer>(true);

if (pooledList.Value.Count == 0)
return;

// Re-parent all renderer GameObjects directly to the wearable root to reduce the hierarchy clutter
foreach (Renderer renderer in pooledList.Value)
{
Transform transform = renderer.transform;

if (transform != wearableRoot && transform.parent != wearableRoot)
transform.SetParent(wearableRoot, true);
}

for (int i = wearableRoot.childCount - 1; i >= 0; i--)
{
Transform child = wearableRoot.GetChild(i);

if (!HasRendererInHierarchy(child))
Object.Destroy(child.gameObject);
}
}

private static bool HasRendererInHierarchy(Transform transform)
{
if (transform.GetComponent<Renderer>() != null)
return true;

for (int i = 0; i < transform.childCount; i++)
{
if (HasRendererInHierarchy(transform.GetChild(i)))
return true;
}

return false;
}
}
}
Loading