Write me a c# script for unity that will have a transform list named Clothing_Armatures_Bones and a transform list named Base_Armature_Bones and for each child transform and children of children transforms from Clothing_Armatures_Bones that have the same name as Base_Armature_Bones then parent everything basically this is a clothing script to put armatures together.
Assistant
User
using UnityEngine;
using System.Collections.Generic;
public class ClothingArmatureMerger : MonoBehaviour
{
public List Clothing_Armatures_Bones;
public List Base_Armature_Bones;
code
Assistant
Certainly! Below is a C# script for Unity that will iterate through two lists of transforms: Clothing_Armatures_Bones and Base_Armature_Bones. The script will match transforms by name and parent them accordingly, allowing you to merge clothing armatures with the base armature.
csharp
using UnityEngine;
using System.Collections.Generic;
public class ClothingArmatureMerger : MonoBehaviour
{
public List<Transform> Clothing_Armatures_Bones;
public List<Transform> Base_Armature_Bones;
void Start()
{
if (Clothing_Armatures_Bones == null || Base_Armature_Bones == null)
{
Debug.LogError("The transform lists have not been initialized.");
return;
}
// Dictionary for fast lookup of base armature bones by name
Dictionary<string, Transform> baseBoneDictionary = new Dictionary<string, Transform>();
foreach (Transform baseBone in Base_Armature_Bones)
{
PopulateBoneDictionary(baseBone, baseBoneDictionary);
}
foreach (Transform clothingBone in Clothing_Armatures_Bones)
{
ReparentClothingBones(clothingBone, baseBoneDictionary);
}
}
private void PopulateBoneDictionary(Transform transform, Dictionary<string, Transform> dictionary)
{
if (transform == null || dictionary.ContainsKey(transform.name))
return;
dictionary.Add(transform.name, transform);
foreach (Transform child in transform)
{
PopulateBoneDictionary(child, dictionary);
}
}
private void ReparentClothingBones(Transform clothingBone, Dictionary<string, Transform> baseBoneDictionary)
{
if (clothingBone == null)
return;
if (baseBoneDictionary.TryGetValue(clothingBone.name, out Transform baseBone))
{
clothingBone.SetParent(baseBone);
}
foreach (Transform child in clothingBone)
{
ReparentClothingBones(child, baseBoneDictionary);
}
}
}
How It Works:
Initialization: The script assumes you have input both Clothing_Armatures_Bones and Base_Armature_Bones lists in the Unity Editor.
Dictionary Creation: A dictionary is used to map the base armature bones for quick lookup, using the bone's name as the key.
Recursive Parenting:
PopulateBoneDictionary: Recursively adds each bone from Base_Armature_Bones to the dictionary.
ReparentClothingBones: Recursively iterates over each bone in Clothing_Armatures_Bones, reparenting them to the corresponding base bone if they share the same name.
This script effectively matches and parents each clothing bone to its corresponding base bone, allowing for proper alignment of clothing to the character’s skeleton.
Usage:
Attach this script to an empty GameObject or the main character object.
Populate the Clothing_Armatures_Bones and Base_Armature_Bones lists in the Unity Inspector with the respective armature hierarchies.
Make sure all transform objects are correctly referenced in the Unity Editor to avoid runtime errors.
void Start()
{
if (Clothing_Armatures_Bones == null || Base_Armature_Bones == null)
{
Debug.LogError("The transform lists have not been initialized.");
return;
}
// Dictionary for fast lookup of base armature bones by name
Dictionary<string, Transform> baseBoneDictionary = new Dictionary<string, Transform>();
foreach (Transform baseBone in Base_Armature_Bones)
{
PopulateBoneDictionary(baseBone, baseBoneDictionary);
}
foreach (Transform clothingBone in Clothing_Armatures_Bones)
{
ReparentClothingBones(clothingBone, baseBoneDictionary);
}
}
private void PopulateBoneDictionary(Transform transform, Dictionary<string, Transform> dictionary)
{
if (transform == null || dictionary.ContainsKey(transform.name))
return;
dictionary.Add(transform.name, transform);
foreach (Transform child in transform)
{
PopulateBoneDictionary(child, dictionary);
}
}
private void ReparentClothingBones(Transform clothingBone, Dictionary<string, Transform> baseBoneDictionary)
{
if (clothingBone == null)
return;
if (baseBoneDictionary.TryGetValue(clothingBone.name, out Transform baseBone))
{
clothingBone.SetParent(baseBone);
}
foreach (Transform child in clothingBone)
{
ReparentClothingBones(child, baseBoneDictionary);
}
}
}
// Make the script not require a list of transforms in order to work, make it require just two transform references and gather all the child and subchildren from those transforms to make their lists.
Certainly! Below is a modified version of the script, which automatically gathers all child and sub-child transforms from specified root transforms for both clothing and base armatures. With this approach, you only need to specify the root transforms for clothing and base armatures in the Unity Editor.
csharp
using UnityEngine;
using System.Collections.Generic;
public class ClothingArmatureMerger : MonoBehaviour
{
public Transform ClothingRoot;
public Transform BaseRoot;
void Start()
{
if (ClothingRoot == null || BaseRoot == null)
{
Debug.LogError("Root transform references have not been assigned.");
return;
}
List<Transform> clothingArmatureBones = new List<Transform>();
List<Transform> baseArmatureBones = new List<Transform>();
// Populate lists with all children and sub-children
GetAllChildTransforms(ClothingRoot, clothingArmatureBones);
GetAllChildTransforms(BaseRoot, baseArmatureBones);
// Dictionary for fast lookup of base armature bones by name
Dictionary<string, Transform> baseBoneDictionary = new Dictionary<string, Transform>();
foreach (Transform baseBone in baseArmatureBones)
{
baseBoneDictionary[baseBone.name] = baseBone;
}
foreach (Transform clothingBone in clothingArmatureBones)
{
if (baseBoneDictionary.TryGetValue(clothingBone.name, out Transform baseBone))
{
clothingBone.SetParent(baseBone);
}
}
}
private void GetAllChildTransforms(Transform parentTransform, List<Transform> transformList)
{
foreach (Transform child in parentTransform)
{
transformList.Add(child);
GetAllChildTransforms(child, transformList);
}
}
}
Key Changes:
Transform References: The script now uses ClothingRoot and BaseRoot as single root transform references, each representing the starting point of your clothing and base armature hierarchies.
Dynamic List Population:
GetAllChildTransforms: This method recursively populates the provided transformList with all descendants of the given parentTransform.
Automatic List Construction:
At the start, the script builds lists of all transforms under the specified root transforms for clothing and base armatures.
Reparenting:
The same dictionary-based reparenting logic is applied, using the dynamically constructed lists.
Usage:
Attach this script to a GameObject in your Unity scene.
Assign the root transforms of both the clothing and base armatures in the Unity Inspector.
This setup eliminates the need to manually populate lists, making it more robust and reducing the chance of error when adding new clothing armatures to your project.