using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class AvatarTool
{
    /// <summary>
    /// 应用装备
    /// </summary>
    /// <param name="targetRole">目标角色物体</param>
    /// <param name="targetPart">被改变的部位</param>
    /// <param name="partName">装备预制体名称</param>
    /// <param name="partSavorRolePrefab">预制体所在的对象预制物体</param>
    public static void ChangeAvatar(GameObject targetRole, GameObject targetPart, string partName, GameObject partSavorRolePrefab)
    {
        //Transform t = partSavorRolePrefab.transform.Find(partName);
        Transform t = FindTargetObj(partSavorRolePrefab, partName);

        if (t == null) return;
        ChangeAvatar(targetRole, targetPart, t.gameObject);
    }

    /// <summary>
    /// 寻找到目标物体
    /// </summary>
    public static Transform FindTargetObj(GameObject obj, string name)
    {
        Transform o = null;
        Transform[] transforms = obj.transform.GetComponentsInChildren<Transform>();
        foreach (Transform t in transforms)
        {
            if (t.name == name)
            {
                o = t;
                break;
            }
        }
        return o;
    }

    /// <summary>
    /// 根据是否带有SkinnedMeshRenderer决定改变外观的方式
    /// </summary>
    /// <param name="targetRole"></param>
    /// <param name="targetPart"></param>
    /// <param name="part"></param>
    public static void ChangeAvatar(GameObject targetRole, GameObject targetPart, GameObject part)
    {
        if (targetPart.GetComponent<SkinnedMeshRenderer>())
        {
            Transform[] hips = targetRole.GetComponentsInChildren<Transform>();//角色的骨骼物体
            ChangeMesh(targetPart, part, hips);
        }
        else
        {
            ChangeMesh(targetPart, part);
        }

    }

    /// <summary>
    /// 改变mesh,这里主要使用蒙皮骨骼换装
    /// </summary>
    /// <param name="targetPart">需要被改变的玩家身上的部位，比如：鞋子</param>
    /// <param name="part">用于改变的部位，比如：鞋子01</param>
    /// <param name="hips">玩家身上的骨骼，targetPart和part必须在相同的一套或者同名（模型不同，但是骨骼完全一样）的骨骼下</param>
    public static void ChangeMesh(GameObject targetPart, GameObject part, Transform[] hips)
    {
        SkinnedMeshRenderer smr = part.GetComponent<SkinnedMeshRenderer>();//获取预制物体下相关部位的SkinnedMeshRenderer

        //获取角色物体下与预制物体相关更换的Mesh部位下作用于该Mesh，再场景中与其同名的骨骼列表
        List<Transform> bones = new List<Transform>();
        foreach (Transform bone in smr.bones)
        {
            foreach (Transform hip in hips)
            {
                if (hip.name != bone.name)
                {
                    continue;
                }
                bones.Add(hip);
                break;
            }
        }
        targetPart.GetComponent<SkinnedMeshRenderer>().sharedMesh = smr.sharedMesh;//更改mesh
        targetPart.GetComponent<SkinnedMeshRenderer>().bones = bones.ToArray();//更换（刷新）骨骼列表
        targetPart.GetComponent<SkinnedMeshRenderer>().materials = smr.sharedMaterials;//更换材质
    }

    /// <summary>
    /// 改变Mesh,这里主要使用MeshRenderer
    /// </summary>
    /// <param name="targetPart">玩家身上的部位</param>
    /// <param name="part"></param>
    public static void ChangeMesh(GameObject targetPart, GameObject part)
    {
        targetPart.GetComponent<MeshFilter>().mesh = part.GetComponent<MeshFilter>().sharedMesh;
        targetPart.GetComponent<MeshRenderer>().materials = part.GetComponent<MeshRenderer>().sharedMaterials;
    }
}