Unity Metaverse(六)、关于 Avatar 换装系统的示例工程
- 2022 年 9 月 27 日 江苏
 本文字数:3192 字
阅读完需:约 10 分钟

🎈 简介
鉴于之前发了一篇关于
Avatar换装系统的解决方案的内容后,有朋友反馈对此比较感兴趣,希望能提供源码,因此我专门整理了一个示例项目,已经放在Github上开源,地址:Unity Avatar换装系统示例工程。
 该工程在之前的基础上增加了不少美术资源,当然它们依然都是来源于Ready Player Me,目前已经有衣服 15 套,然后增加了发型、眼镜、胡须的编辑,在 RPM 上各扒了几个资源,感兴趣的话大家可以自行拓展。
 🎈 配置表
各配置表的创建菜单如下:
 其中Outlook Config,即衣服的配置表涉及的内容最多,依次手动拖拽赋值较为繁琐,因此使用编辑器 DragAndDrop 类为其增加了快速拖拽赋值的功能:
 实现并不复杂,因为之前我们使用Avatar Clothes Collector来扒衣服资源的时候已经为资源命好名了,因此只需要根据名称去匹配指定的资源即可,代码如下:
using System;using System.Linq;using UnityEngine;using System.Collections.Generic;
#if UNITY_EDITORusing UnityEditor;#endif
namespace Metaverse{    [Serializable]    public class AvatarOutlookData    {        public Sprite thumb;
        public Mesh headMesh;        public Material headMaterial;
        public Mesh bodyMesh;        public Material bodyMaterial;
        public Mesh topMesh;        public Material topMaterial;
        public Mesh bottomMesh;        public Material bottomMaterial;
        public Mesh footwearMesh;        public Material footwearMaterial;    }
    [CreateAssetMenu]    public class AvatarOutlookDataConfig : ScriptableObject    {        public List<AvatarOutlookData> data = new List<AvatarOutlookData>(0);    }
#if UNITY_EDITOR    [CustomEditor(typeof(AvatarOutlookDataConfig))]    public class AvatarOutlookDataConfigEditor : Editor    {        public override void OnInspectorGUI()        {            base.OnInspectorGUI();
            OnDragRectGUI();        }
        private void OnDragRectGUI()        {            GUILayout.Space(10f);            GUILayout.BeginHorizontal();            GUILayout.Label(GUIContent.none, GUILayout.ExpandWidth(true));            Rect lastRect = GUILayoutUtility.GetLastRect();            var dropRect = new Rect(lastRect.x + 2f, lastRect.y - 2f, lastRect.width - 20f, 20f);            bool containsMouse = dropRect.Contains(Event.current.mousePosition);            if (containsMouse)            {                switch (Event.current.type)                {                    case EventType.DragUpdated:                        bool flag = DragAndDrop.objectReferences.OfType<Mesh>().Any()                             || DragAndDrop.objectReferences.OfType<Material>().Any()                            || DragAndDrop.objectReferences.OfType<Sprite>().Any();                        DragAndDrop.visualMode = flag ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;                        Event.current.Use();                        Repaint();                        break;                    case EventType.DragPerform:                        IEnumerable<Mesh> meshes = DragAndDrop.objectReferences.OfType<Mesh>();                        IEnumerable<Material> materials = DragAndDrop.objectReferences.OfType<Material>();                        IEnumerable<Texture> textures = DragAndDrop.objectReferences.OfType<Texture>();
                        if (meshes.Any() || materials.Any() || textures.Any())                        {                            Undo.RecordObject(target, "Add");                            AvatarOutlookData data = new AvatarOutlookData();                            (target as AvatarOutlookDataConfig).data.Add(data);                            foreach (var mesh in meshes)                            {                                if (mesh.name == "mesh_head") data.headMesh = mesh;                                else if (mesh.name == "mesh_body") data.bodyMesh = mesh;                                else if (mesh.name == "mesh_top") data.topMesh = mesh;                                else if (mesh.name == "mesh_bottom") data.bottomMesh = mesh;                                else if (mesh.name == "mesh_footwear") data.footwearMesh = mesh;                            }
                            foreach (var material in materials)                            {                                if (material.name == "mat_head") data.headMaterial = material;                                else if (material.name == "mat_body") data.bodyMaterial = material;                                else if (material.name == "mat_top") data.topMaterial = material;                                else if (material.name == "mat_bottom") data.bottomMaterial = material;                                else if (material.name == "mat_footwear") data.footwearMaterial = material;                            }
                            foreach (var texture in textures)                            {                                string path = AssetDatabase.GetAssetPath(texture);                                TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;                                if (importer != null && importer.textureType == TextureImporterType.Sprite)                                {                                    data.thumb = AssetDatabase.LoadAssetAtPath<Sprite>(path);                                }                            }
                            EditorUtility.SetDirty(target);                        }                        Event.current.Use();                        Repaint();                        break;                }            }            Color color = GUI.color;            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, containsMouse ? .9f : .5f);            GUI.Box(dropRect, "Drop Here", new GUIStyle(GUI.skin.box) { fontSize = 10 });            GUI.color = color;            GUILayout.EndHorizontal();        }    }#endif}
注:当然正式开发工作中不建议使用这些配置表,应该去构建资源的Assets Bundle,实现资源的动态管理。
🎈 关于胡须
编辑胡须的时候不一定是替换 Mesh,有的是直接画在了头部的贴图中,因此分为Mesh与Texture两种类型:
 [Serializable]public class AvatarBeardData{    public Sprite thumb;
    public enum Type    {        Mesh,        Texture,    }
    public Type type = Type.Mesh;
    public Mesh beardMesh;
    public Material beardMaterial;}
当类型为 Mesh 时,替换Beard部件的Skin Mesh Renderer中的Mesh与Material,当类型为 Texture 时,替换Head部件的Skin Mesh Renderer中的Material。
版权声明: 本文为 InfoQ 作者【CoderZ】的原创文章。
原文链接:【http://xie.infoq.cn/article/069ed290a5d4d22dda8cd8abf】。文章转载请联系作者。
CoderZ
Code change the world. 2022.08.04 加入
一名Unity开发工程师,SKFramework开源框架作者,CSDN博客专家、Unity领域优质创作者,华为云云享专家,阿里云专家博主,从事于游戏、VRARMR、虚拟仿真、数字孪生、元宇宙等相关领域,微信公众号:当代野生程序猿。










    
评论