效果预览

基本效果

鄙人不才,实现的方法较为粗暴,如果有更好的方案还望大神指教一二。

准备工作

巧妇难为无米之炊,制作换装系统首先得有“装备”才可以。如果大家会美术可以自己画,注意所有的素材的遮挡顺序和运动细节需要一致,当然如果用程序控制遮罩来实现更为优秀的遮挡管理我觉得也是可行的。

给大家看看我用的素材集合(自制)截图:

有了这些替换用的素材就可以开始实现换装了。

Plyer的建立

我将人物拆成了各个不同的身体部件,它们每一个都是一个SpriteRander,手部因为比较复杂所以拆的尤其的多。(这里的DrawCall可能会很高,用Shader来合并素材应该能节省性能,可惜我不会(*^_^*))

建立好的人物

建立好Player之后,请手动确保渲染顺序(OrderInLayer)的正确性。

接下来要给人物建立好不同的动画,譬如走路跳跃攻击。这一步关于Unity本身动画状态机的使用相信大家都会的。

素材库的建立

使用类似的脚本来存储图片:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;//用以存储身体图片的类
public class Doll_body : MonoBehaviour
{//图片的存储结构体//使用此注解使其能显示在Unity的编辑面板上[System.Serializable]public struct DollBody{public Sprite bodyNormal;public Sprite bodyFront;public Sprite leftHandNormal;public Sprite leftHandBack;public Sprite leftHandFront;public Sprite leftHand_SwordAttackStab_prepare;public Sprite leftHand_SwordAttackStab_attack;public Sprite leftHand_SwordAttackCleave_up;public Sprite leftHand_SwordAttackCleave_mid;public Sprite leftHand_bowAttack_01;public Sprite leftHand_bowAttack_02;public Sprite leftHand_bowAttack_03;public Sprite rightHandFront;public Sprite rightHandNormal;public Sprite rightHand_holdBow;}//不同的装备public DollBody piBody;public DollBody xunlinBody;public DollBody zibiBody;
}

创建一个空物体名为“换装管理器 ”,给它加上上述组件,在拖动图片赋值,切换场景时注意不要销毁此物体。(因为是像素风游戏,所以图集比较小,大游戏的话也许得用更高效的素材库了,具体怎么实现没有思路)

使用脚本时的界面

创建下面这个脚本,同样作为“换装管理器 ”的组件,用来给各个图片组起个名字,方便调用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DollDictionary : MonoBehaviour
{public static Dictionary<string,Doll_foot.DollFoot> footDictionary = new Dictionary<string, Doll_foot.DollFoot>();public static Dictionary<string,Doll_body.DollBody> bodyDictionary = new Dictionary<string, Doll_body.DollBody> ();public static Dictionary<string,Doll_hair.DollHair> hairDictionary = new Dictionary<string, Doll_hair.DollHair>();public static Dictionary<string,Doll_helmet.DollHelmet> helmetDictionary =new  Dictionary<string, Doll_helmet.DollHelmet>();public static Dictionary<string,Doll_Skin.DollSkin> skinDictionary = new  Dictionary<string, Doll_Skin.DollSkin>();void Awake () {Doll_foot tempDollFoot = this.GetComponent<Doll_foot> ();footDictionary.Add ("皮质腿甲", tempDollFoot.piFoot);footDictionary.Add ("巡林腿甲", tempDollFoot.xunlinFoot);footDictionary.Add("紧身裤", tempDollFoot.jinshengFoot);Doll_body tempDollBody = this.GetComponent<Doll_body> () ;bodyDictionary.Add ("皮质胸甲", tempDollBody.piBody);bodyDictionary.Add ("巡林胸甲", tempDollBody.xunlinBody);bodyDictionary.Add("自闭胸甲", tempDollBody.zibiBody);Doll_hair tempDollHair = this.GetComponent<Doll_hair> ();hairDictionary.Add ("普通", tempDollHair.putongHair);hairDictionary.Add ("半遮", tempDollHair.banzheHair);hairDictionary.Add ("丸子", tempDollHair.wanziHair);hairDictionary.Add ("莫辛甘", tempDollHair.moxingganHair);hairDictionary.Add("飞机头", tempDollHair.feijiHair);Doll_helmet tempDollHelmet = this.GetComponent<Doll_helmet> ();helmetDictionary.Add ("兜帽", tempDollHelmet.doumaoHelmet);helmetDictionary.Add ("巫师帽", tempDollHelmet.fashiHelmet);helmetDictionary.Add ("铁头盔", tempDollHelmet.metalHelmet);helmetDictionary.Add ("皮帽", tempDollHelmet.piHelmett);Doll_Skin tempDollSkin = this.GetComponent<Doll_Skin> ();skinDictionary.Add ("皮肤A", tempDollSkin.SkinTyprA);skinDictionary.Add ("皮肤B", tempDollSkin.SkinTyprB);skinDictionary.Add ("皮肤C", tempDollSkin.SkinTyprC);}
}

添加动画事件

给Player附上下面这个脚本,用以在特定的时候改变人物的图片。

其中SetBody(PlayerAttribute),用以改变人物的图集,PlayerAttribute是我自己定义的人物状态类,当里面的字段改变时会自动出发一个事件来调用SetBody更改图集。

setBodyXXX(),用以在动画中触发改变图片,众所周知,动画就是一系列变化的图片,这样便实现了动态。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SwarpSprites : MonoBehaviour
{[SerializeField]SpriteRenderer helmetRender,hairRender,skinHeadRender,bodyRender,footRender;Sprite walkMid,walkFront,walkBack,footStand,footFall;Sprite bodyNormal,bodyFront;Sprite skinHeadNormal,skinHeadUp,skinHeadDown;Sprite helmetNormal,helmetUp,helmetDown;Sprite hairNormal,hairUp,hairDown;public SwarpHandSprites swarpHandSprites;void Start () {setAll ();}public void setAll(){PlayerAttribute playerAttribute = this.GetComponent<PlayerAttribute> ();setFoot (playerAttribute);setBody (playerAttribute);setSkin (playerAttribute);setHelmet (playerAttribute);setHair (playerAttribute);}public void setBody(PlayerAttribute playerAttribute){Doll_body.DollBody bodySet = DollDictionary.bodyDictionary [playerAttribute.bodyType];bodyNormal = bodySet.bodyNormal;bodyFront = bodySet.bodyFront;swarpHandSprites.bodyLeftHandNormal = bodySet.leftHandNormal;swarpHandSprites.bodyLeftHandFront = bodySet.leftHandFront;swarpHandSprites.bodyLeftHandBack = bodySet.leftHandBack;swarpHandSprites.bodyLeftHand_SwordAttackStab_prepare = bodySet.leftHand_SwordAttackStab_prepare;swarpHandSprites.bodyLeftHand_SwordAttack_attack = bodySet.leftHand_SwordAttackStab_attack;swarpHandSprites.bodyLeftHand_SwardAttackCleave_mid = bodySet.leftHand_SwordAttackCleave_mid;swarpHandSprites.bodyLeftHand_SwardAttackCleave_Up = bodySet.leftHand_SwordAttackCleave_up;swarpHandSprites.bodyLeftHand_bowAttack01 = bodySet.leftHand_bowAttack_01;swarpHandSprites.bodyLeftHand_bowAttack02 = bodySet.leftHand_bowAttack_02;swarpHandSprites.bodyLeftHand_bowAttack03 = bodySet.leftHand_bowAttack_03;swarpHandSprites.bodyRightHandNormal = bodySet.rightHandNormal;swarpHandSprites.bodyRightHandFront = bodySet.rightHandFront;swarpHandSprites.bodyRightHand_holdBow = bodySet.rightHand_holdBow;}//设置足部动画void setWalkMid(){ footRender.sprite = walkMid; }void setWalkFront(){ footRender.sprite = walkFront; }void setWalkBack(){ footRender.sprite = walkBack; }void setFootStand(){ footRender.sprite = footStand; }void setFootFallDown(){ footRender.sprite = footFall; }//设置身体动画public void setBodyNormal(){ bodyRender.sprite = bodyNormal; }void setBodyFront(){ bodyRender.sprite = bodyFront; }//还有很多......}

在合适的关键帧调用切换图片的函数

以上,就实现了一个简单的纸娃娃,最耗时的,其实是绘画各种替换素材的过程。

Unity2D:简单人物纸娃娃换装实现(一) 服装的变换相关推荐

  1. [Unity3D]人物模型的换装

    更多教程请访问: http://dingxiaowei.cn/ 写一写今天的学习心得,保持每天不断的学习,今天写的是人物的换装的技术实现. 人物的换装是游戏开发的一个基本的技术,初来公司,老板就要我学 ...

  2. Unity3D换装详解

    游戏内的角色,能够像纸娃娃换装那样子让玩家可以为自己的角色改变外观,一直是相当受欢迎的功能:一般而言,我们建好的 3D 模型,如果要将其中一个部位换成另外一个形状,最直接的就是将该物件部位的 Mesh ...

  3. Unity3D游戏开发之换装方法

    游戏内的角色,能够像纸娃娃换装那样子让玩家可以为自己的角色改变外观,一直是相当受欢迎的功能:一般而言,我们建好的 3D 模型,如果要将其中一个部位换成另外一个形状,最直接的就是将该物件部位的 Mesh ...

  4. Unity3D人物换装

    这个Demo主要是基于SkinnedMeshRenderer,对其中元素做修改达到改变模型外形与装饰的效果. //换贴图,直接把这段代码加在要换贴图的模型上即可; private var eyeind ...

  5. unity 3d换装之 SkinMeshRenderer

    http://www.cnblogs.com/shamoyuu/p/6505561.html 关注公众号 风色年代(itfantasycc) 200G Unity资料合集送上~ 一.换装原理 游戏角色 ...

  6. 论文阅读9 | COCAS:一个大规模换装的行人重识别数据集

    论文:COCAS: A Large-Scale Clothes Changing Person Dataset for Re-identification 出处:CVPR 2020 文章目录 1. 创 ...

  7. unity之游戏角色换装实现

    http://www.cnblogs.com/shamoyuu/p/6505561.html 一.换装原理 游戏角色换装分为以下几步: 1.替换蒙皮网格 2.刷新骨骼 3.替换材质 上面这种是比较简单 ...

  8. Cocos Creator 的 web/原生多平台 Spine 换装方案解析,附 Demo 源码

    引言:Spine 换装是游戏开发中的一种常见实现方案,本次,羽毛先生将介绍自己对整体换装和局部换装实现方案的探索与选择. 运行环境 Cocos Creator 3.5.2 web/native 需求 ...

  9. 技巧| Unity中Avatar换装实现

    转自: http://blog.uwa4d.com/archives/avartar.html Avatar换装是MMO游戏不可缺少的一部分,一个人物模型通常可拆分为头.身体.手臂.腿.武器等部分,如 ...

  10. Unity中Avatar换装实现

    Avatar换装是MMO游戏不可缺少的一部分,一个人物模型通常可拆分为头.身体.手臂.腿.武器等部分,如何将这些部分组合到一起呢?本文将阐述如何将在Unity中实现人物模型的换装功能. 这是侑虎科技第 ...

最新文章

  1. Xilinx RAM IP核的使用
  2. VMware workstation 创建共享盘
  3. 十、LINQ查询之延迟执行
  4. 三维空间刚体运动4-4:四元数多点连续解析解插值方法:Spicv
  5. (81)FPGA复位激励(task)
  6. Linux学习总结(37)——CentOS7下Firewall防火墙配置用法详解
  7. vue 方法获取返回值_Vue项目中Api的组织和返回数据处理的操作
  8. Android源码kernel编译
  9. Android中Bitmap,byte[],Drawable相互转化
  10. 解决html标签中有多个空格
  11. 【POJ 3666】Making the Grade【线性DP】
  12. The Elements of Style 4ed ---英文写作指南(一)
  13. myqq框架 python插件
  14. 解决chm文件打开后跳到某一主页方法
  15. python计算活了多少天计算器_年龄计算器-实际岁数计算器-周岁计算器-虚岁计算器-生日天数计算-活了多少天计算-虚岁怎么算...
  16. 一张图片放两个二维码_两个方法教你找到一张图片的高清版
  17. pdf文件如何在安卓手机端不用下载在线预览
  18. linux系统做成iso镜像文件,如何在Linux系统中制作可启动img/iso镜像文件
  19. Bokeh Graph
  20. 存储过程中的in out in out 三种类型的参数

热门文章

  1. python 自动化办公 随机生成题库文档
  2. 整理下关于Visual Foxpro的技术
  3. CAD2004软件从下载安装到学习CAD教程(后台菜单自助更多)
  4. cad转shp 奥维_CAD转化为Shp
  5. 什么是Csrss.exe进程?此进程有何作用?
  6. 世界足坛的一些著名德比
  7. 如何使用PDF编辑器裁剪PDF页面
  8. 【word论文排版教程3】制作样式和列表
  9. 淘晶驰串口屏下载工程慢怎么办
  10. 软件测试【个人简历】展示模板