我正在创建类似模拟人生的东西,你有一个角色,你可以为角色添加不同的衣服。
我使用的所有模型都使用相同的骨骼和装备。我已将这些模型放在不同的资产包中。在运行时,我读取资产包并将这些模型组合到一个 SkinnedMeshRenderer 中,从而组合到一个对象中。但是每次我添加另一个对象时,骨骼数量都会增加。
我知道为什么会这样,但我想再次减少骨头的数量。我试图找到重复的骨骼并删除它们以及与删除的骨骼在同一索引上的绑定姿势,但这仍然给我错误“绑定姿势的数量与骨骼的数量不匹配”,即使它们都是 45。
这是附加模型的代码:
private void UpdateSkinnedMesh()
{
float startTime = Time.realtimeSinceStartup;
// Create a list of for all data
List combineInstances = new List();
List materials = new List();
List bones = new List();
Transform[] transforms = baseModel.GetComponentsInChildren();
//TEMP
List elements = new List();
elements.Add(body);
if(_currentActiveProps.ContainsKey(ItemSubCategory.Skin))
elements.Add(tutu);
//Go over all active elements
foreach (CharacterElement element in elements)
{
//Get the skinned mesh renderer
SkinnedMeshRenderer smr = element.GetSkinnedMeshRenderer();
//Add materials to the entire list
materials.AddRange(smr.materials);
//Add all submeshes to a combined mesh
for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++)
{
CombineInstance ci = new CombineInstance();
ci.mesh = smr.sharedMesh;
ci.subMeshIndex = sub;
combineInstances.Add(ci);
}
//Bones are not saved in asset bundle, get the names and reference them again
foreach (string bone in element.GetBoneNames())
{
foreach (Transform transform in transforms)
{
if (transform.name != bone) continue;
bones.Add(transform);
break;
}
}
//Destroy the temp object
Destroy(smr.gameObject);
}
//Get skinned mesh renderer
SkinnedMeshRenderer r = baseModel.GetComponentInChildren();
//Create enew combined mesh
r.sharedMesh = new Mesh();
r.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
//Add bones and materials
r.bones = bones.ToArray();
r.materials = materials.ToArray();
Debug.Log("Bindposes: " + r.sharedMesh.bindposes.Length);
Debug.Log("Generating character took: " + (Time.realtimeSinceStartup - startTime) * 1000 + " ms");
Debug.Log("Bone count: " + r.bones.Length);
}
我已经在 Unity Answers 上提出了这个问题,但是因为版主批准这个问题需要几个小时,所以我也想在这里尝试一下。