Unity Metaverse(六)、关于Avatar换装系统的示例工程

时间:2022-09-28 07:53:07


???? 简介

鉴于之前发了一篇关于Avatar换装系统的解决方案的内容后,有朋友反馈对此比较感兴趣,希望能提供源码,因此我专门整理了一个示例项目,已经放在Github上开源,地址:Unity Avatar换装系统示例工程

Unity Metaverse(六)、关于Avatar换装系统的示例工程

该工程在之前的基础上增加了不少美术资源,当然它们依然都是来源于Ready Player Me,目前已经有衣服15套,然后增加了发型、眼镜、胡须的编辑,在RPM上各扒了几个资源,感兴趣的话大家可以自行拓展。

Unity Metaverse(六)、关于Avatar换装系统的示例工程

???? 配置表

配置表的创建菜单如下:

Unity Metaverse(六)、关于Avatar换装系统的示例工程
其中Outlook Config,即衣服的配置表涉及的内容最多,依次手动拖拽赋值较为繁琐,因此使用编辑器DragAndDrop类为其增加了快速拖拽赋值的功能:

Unity Metaverse(六)、关于Avatar换装系统的示例工程
实现并不复杂,因为之前我们使用Avatar Clothes Collector来扒衣服资源的时候已经为资源命好名了,因此只需要根据名称去匹配指定的资源即可,代码如下:

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

#if UNITY_EDITOR
using 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,有的是直接画在了头部的贴图中,因此分为MeshTexture两种类型:

Unity Metaverse(六)、关于Avatar换装系统的示例工程

[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中MeshMaterial,当类型为Texture时,替换Head部件的Skin Mesh Renderer中的Material