using UnityEngine;
using System.Collections.Generic;
using System.Collections.Generic;
/// <summary>
/// http://bbs.9ria.com/thread-185477-1-1.html
/// </summary>
/// http://bbs.9ria.com/thread-185477-1-1.html
/// </summary>
public class ChangeAvatar : MonoBehaviour
{
public enum AvatarPart
{
HEAD,
BODY,
WEAPON,
}
{
public enum AvatarPart
{
HEAD,
BODY,
WEAPON,
}
public GameObject mBaseModel; //基准骨骼
public GameObject mElementAvatar; //待替换的元素
public GameObject mElementAvatar; //待替换的元素
private Transform[] mBaseHips; //基准骨架。
private Dictionary<string, SkinnedMeshRenderer> mBaseSkins = new Dictionary<string, SkinnedMeshRenderer>(); //标准蒙皮网格信息
private Dictionary<string, SkinnedMeshRenderer> mElementSkins = new Dictionary<string, SkinnedMeshRenderer>(); //要替代的蒙皮网格信息
private Dictionary<string, SkinnedMeshRenderer> mElementSkins = new Dictionary<string, SkinnedMeshRenderer>(); //要替代的蒙皮网格信息
void Start()
{
ChangePart(mBaseModel, mElementAvatar);
}
{
ChangePart(mBaseModel, mElementAvatar);
}
private void ChangePart(string part)
{
SkinnedMeshRenderer smr = mElementSkins[part].GetComponent<SkinnedMeshRenderer>(); //从资料中取得各部位指定编号的 SkinnedMeshRenderer
List<Transform> bones = new List<Transform>();//取得相对应名称的骨架物件来建立新的骨架列表
{
SkinnedMeshRenderer smr = mElementSkins[part].GetComponent<SkinnedMeshRenderer>(); //从资料中取得各部位指定编号的 SkinnedMeshRenderer
List<Transform> bones = new List<Transform>();//取得相对应名称的骨架物件来建立新的骨架列表
foreach (Transform bone in smr.bones)
{
foreach (Transform hip in mBaseHips)
{
if (hip.name != bone.name) continue;
bones.Add(hip);
break;
}
}
{
foreach (Transform hip in mBaseHips)
{
if (hip.name != bone.name) continue;
bones.Add(hip);
break;
}
}
mBaseSkins[part].sharedMesh = smr.sharedMesh;
mBaseSkins[part].bones = bones.ToArray();
mBaseSkins[part].materials = smr.materials;
GameObject.Destroy(smr.gameObject);
}
mBaseSkins[part].bones = bones.ToArray();
mBaseSkins[part].materials = smr.materials;
GameObject.Destroy(smr.gameObject);
}
void ChangePart(GameObject baseModel, GameObject element)
{
LoadBaseBips(baseModel);
LoadAvatarData(element);
{
LoadBaseBips(baseModel);
LoadAvatarData(element);
foreach (string partName in mElementSkins.Keys)
{
List<Transform> bones = new List<Transform>();
SkinnedMeshRenderer elementSkin = mElementSkins[partName];
{
List<Transform> bones = new List<Transform>();
SkinnedMeshRenderer elementSkin = mElementSkins[partName];
foreach (Transform bone in elementSkin.bones)
{
foreach (Transform hip in mBaseHips)
{
if (hip.name != bone.name) continue;
bones.Add(hip);
break;
}
}
{
foreach (Transform hip in mBaseHips)
{
if (hip.name != bone.name) continue;
bones.Add(hip);
break;
}
}
mBaseSkins[partName].sharedMesh = elementSkin.sharedMesh;
mBaseSkins[partName].sharedMaterials = elementSkin.sharedMaterials;
mBaseSkins[partName].bones = bones.ToArray();
GameObject.DestroyObject(elementSkin.gameObject);
}
}
mBaseSkins[partName].sharedMaterials = elementSkin.sharedMaterials;
mBaseSkins[partName].bones = bones.ToArray();
GameObject.DestroyObject(elementSkin.gameObject);
}
}
private void LoadBaseBips(GameObject baseModel)
{
mBaseHips = baseModel.GetComponentsInChildren<Transform>(); //初始化骨骼信息。
{
mBaseHips = baseModel.GetComponentsInChildren<Transform>(); //初始化骨骼信息。
foreach (SkinnedMeshRenderer part in baseModel.GetComponentsInChildren<SkinnedMeshRenderer>()) //初始化蒙皮信息
{
string partName = SimplifyName(part.name);
if (!mBaseSkins.ContainsKey(partName))
{
mBaseSkins.Add(partName, part);
}
}
}
{
string partName = SimplifyName(part.name);
if (!mBaseSkins.ContainsKey(partName))
{
mBaseSkins.Add(partName, part);
}
}
}
private void LoadAvatarData(GameObject elemetAvatar)
{
SkinnedMeshRenderer[] parts = elemetAvatar.GetComponentsInChildren<SkinnedMeshRenderer>(); //从来源模型资源取出各部位的 SkinnedMeshRenderer
{
SkinnedMeshRenderer[] parts = elemetAvatar.GetComponentsInChildren<SkinnedMeshRenderer>(); //从来源模型资源取出各部位的 SkinnedMeshRenderer
foreach (SkinnedMeshRenderer part in parts)
{
string partName = SimplifyName(part.name);
if (!mElementSkins.ContainsKey(partName))
{
mElementSkins.Add(partName, part);
}
}
}
{
string partName = SimplifyName(part.name);
if (!mElementSkins.ContainsKey(partName))
{
mElementSkins.Add(partName, part);
}
}
}
private string SimplifyName(string strName)
{
string[] nameArray = strName.Split('_');
string partName = nameArray[nameArray.Length - 1];
return partName;
}
}
{
string[] nameArray = strName.Split('_');
string partName = nameArray[nameArray.Length - 1];
return partName;
}
}