赞
踩
unity换装方式一般分为三种,第一种是:直接更换物体的prefb,第二种:更换Material,第三种:更改mesh,再合并网格,做到与骨骼绑定
第一种:换模型
第二种:更换材质或者贴图
第一第二方式,如下图所示,不做过多介绍
第三种:换蒙皮
可以发现红框标注部分,骨骼虽然是同一种,但是所用网格是不同的,那么为了实现这种样式的换装,就需要做两种不一样的mesh,同时要保证两个mesh属于同一个骨骼,最后通过代码合并网格,代码如下
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- /// <summary>
- /// 角色皮肤组件
- /// </summary>
- public class RoleSkinComponent : MonoBehaviour
- {
- /// <summary>
- /// 根骨骼
- /// </summary>
- [SerializeField]
- private Transform m_RootBone;
-
- /// <summary>
- /// 角色身上的SkinnedMeshRenderer 一个角色只有一个SkinnedMeshRenderer
- /// </summary>
- [SerializeField]
- private SkinnedMeshRenderer m_CurrSkinnedMeshRenderer;
-
- /// <summary>
- /// 部件
- /// </summary>
- [SerializeField]
- private SkinnedMeshRendererPart[] Parts;
-
- /// <summary>
- /// 组件数组长度
- /// </summary>
- private int m_PartsLen;
-
-
- /// <summary>
- /// 角色当前穿戴的部件列表
- /// </summary>
- private List<SkinnedMeshRenderer> m_CurrPartList;
-
- /// <summary>
- /// 合并网格对象集合
- /// </summary>
- private List<CombineInstance> m_CombineInstances;
-
- /// <summary>
- /// 材质集合
- /// </summary>
- private List<Material> m_Materials;
-
- /// <summary>
- /// SkinnedMeshRenderer对应的骨骼信息
- /// </summary>
- private List<Transform> m_Bones;
-
- /// <summary>
- /// 角色身上的骨骼数组
- /// </summary>
- private Transform[] m_BoneTransforms; //角色身上的骨骼数组
-
- private void Awake()
- {
- m_CurrPartList = new List<SkinnedMeshRenderer>();
- m_CombineInstances = new List<CombineInstance>();
- m_Materials = new List<Material>();
- m_Bones = new List<Transform>();
- }
-
- private void OnDestroy()
- {
- m_CurrPartList.Clear();
- m_CurrPartList = null;
-
- m_CombineInstances.Clear();
- m_CombineInstances = null;
-
- m_Materials.Clear();
- m_Materials = null;
-
- m_Bones.Clear();
- m_Bones = null;
-
- m_RootBone = null;
- m_CurrSkinnedMeshRenderer = null;
- }
-
- void Start()
- {
- m_PartsLen = Parts.Length;
-
- m_BoneTransforms = m_RootBone.GetComponentsInChildren<Transform>(); //获取角色跟骨骼
- m_CurrSkinnedMeshRenderer.sharedMesh = new Mesh();
-
- LoadPart(new List<int> { 1, 2 });
- }
-
- /// <summary>
- /// 加载部件
- /// </summary>
- /// <param name="parts"></param>
- public void LoadPart(List<int> parts)
- {
- m_CombineInstances.Clear();
- m_Materials.Clear();
- m_Bones.Clear();
- m_CurrPartList.Clear();
-
- int len = parts.Count;
- for (int i = 0; i < len; i++)
- {
- SkinnedMeshRenderer skinnedMeshRenderer = GetPartByNo(parts[i]);
- if (skinnedMeshRenderer != null)
- {
- m_CurrPartList.Add(skinnedMeshRenderer);
- }
- }
-
- for (int i = 0; i < m_CurrPartList.Count; i++)
- {
- SkinnedMeshRenderer skinnedMeshRenderer = m_CurrPartList[i];
- m_Materials.AddRange(skinnedMeshRenderer.materials);
-
- //添加合并网格
- for (int sub = 0; sub < skinnedMeshRenderer.sharedMesh.subMeshCount; sub++)
- {
- CombineInstance ci = new CombineInstance();
- ci.mesh = skinnedMeshRenderer.sharedMesh;
- ci.subMeshIndex = sub;
- m_CombineInstances.Add(ci);
- }
-
- //======================此处是加载SkinnedMeshRenderer对应的骨骼 也就是bones的数量实际上会大于玩家身上的骨骼数量============================
- for (int sub = 0; sub < skinnedMeshRenderer.bones.Length; sub++)
- {
- int lenBoneTrans = m_BoneTransforms.Length;
- for (int m = 0; m < lenBoneTrans; m++)
- {
- Transform transform = m_BoneTransforms[m];
- if (transform.name != skinnedMeshRenderer.bones[sub].name) continue;
- m_Bones.Add(transform);
- break;
- }
- }
- }
-
- m_CurrSkinnedMeshRenderer.sharedMesh.CombineMeshes(m_CombineInstances.ToArray(), false, false); //合并模型
- m_CurrSkinnedMeshRenderer.bones = m_Bones.ToArray(); //赋予骨骼
- m_CurrSkinnedMeshRenderer.materials = m_Materials.ToArray(); //赋予材质
- }
-
- #region GetPartByNo 根据编号获取部件
- /// <summary>
- /// 根据编号获取部件
- /// </summary>
- /// <param name="no">编号</param>
- /// <returns></returns>
- private SkinnedMeshRenderer GetPartByNo(int no)
- {
- for (int i = 0; i < m_PartsLen; i++)
- {
- SkinnedMeshRendererPart skinnedMeshRendererPart = Parts[i];
- if (skinnedMeshRendererPart.No == no)
- {
- return skinnedMeshRendererPart.Part;
- }
- }
- return null;
- }
- #endregion
-
- [Serializable]
- /// <summary>
- /// 皮肤配置
- /// </summary>
- public class SkinnedMeshRendererPart
- {
- /// <summary>
- /// 编号
- /// </summary>
- public int No;
-
- /// <summary>
- /// 皮肤部件
- /// </summary>
- public SkinnedMeshRenderer Part;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。