//换装
public static void BuildClothSuit(GameObject modelGo, XClothSuitBean suit)
{
if (suit != null)
{
if (suit.Helmet != null && suit.Helmet.TemplateID != 0)
{
XClothSuitBean tempSuit = XTemplate.GetTemplateClothSuit(suit.Helmet.TemplateID);
GameObject suitGo = UnityObjectBuilder.LoadPrefab(tempSuit.Prefab) as GameObject;
BuildClothing(modelGo, suitGo, XClothType.Helmet);
}
if (suit.Body != null && suit.Body.TemplateID != 0)
{
XClothSuitBean tempSuit = XTemplate.GetTemplateClothSuit(suit.Body.TemplateID);
GameObject suitGo = UnityObjectBuilder.LoadPrefab(tempSuit.Prefab) as GameObject;
BuildClothing(modelGo, suitGo, XClothType.Body);
}
if (suit.Arm != null && suit.Arm.TemplateID != 0)
{
XClothSuitBean tempSuit = XTemplate.GetTemplateClothSuit(suit.Arm.TemplateID);
GameObject suitGo = UnityObjectBuilder.LoadPrefab(tempSuit.Prefab) as GameObject;
BuildClothing(modelGo, suitGo, XClothType.Arm);
}
if (suit.Leg != null && suit.Leg.TemplateID != 0)
{
XClothSuitBean tempSuit = XTemplate.GetTemplateClothSuit(suit.Leg.TemplateID);
GameObject suitGo = UnityObjectBuilder.LoadPrefab(tempSuit.Prefab) as GameObject;
BuildClothing(modelGo, suitGo, XClothType.Leg);
}
}
}
#region 更换装备部件
private static void BuildClothing(GameObject charGo, GameObject setGo, XClothType type)
{
string partName = null;
switch (type)
{
case XClothType.Helmet:
partName = "Head";
break;
case XClothType.Body:
partName = "Body";
break;
case XClothType.Arm:
partName = "Arms";
break;
case XClothType.Leg:
partName = "Legs";
break;
}
if (partName == null)
{
Debug.Log("wrong type:" + type);
return;
}
SkinnedMeshRenderer newSetSkinnedMeshRenderer = setGo.transform.Find("Meshes").Find(partName).GetComponent<SkinnedMeshRenderer>();
SkinnedMeshRenderer baseSkinnedMeshRenderer = charGo.transform.Find("Meshes").Find(partName).GetComponent<SkinnedMeshRenderer>();
baseSkinnedMeshRenderer.material = newSetSkinnedMeshRenderer.sharedMaterial;
baseSkinnedMeshRenderer.sharedMesh = newSetSkinnedMeshRenderer.sharedMesh;
//目标物件的骨架
Transform[] hips = charGo.GetComponentsInChildren<Transform>();
List<Transform> bones = new List<Transform>();
//从资料中取得各部位指定编号的 SkinnedMeshRenderer
foreach (Transform bone in newSetSkinnedMeshRenderer.bones)
{
foreach (Transform hip in hips)
{
//
if (hip.name != bone.name) continue;
bones.Add(hip);
break;
}
}
baseSkinnedMeshRenderer.bones = bones.ToArray();
}
#endregion