IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity Metaverse(六)、关于Avatar换装系统的示例工程 -> 正文阅读

[游戏开发]Unity Metaverse(六)、关于Avatar换装系统的示例工程


🎈 简介

鉴于之前发了一篇关于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_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两种类型:

Mesh Beard & Texture Beard

[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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-09-30 01:19:47  更:2022-09-30 01:20:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年6日历 -2024/6/16 15:57:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码