AR开发实战EasyAR项目之AR试衣间(人物换装)
2018-07-19 本文已影响765人
TonyWan_AR
一、主要框架视图
二、关键代码
AvatarButton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AvatarButton : MonoBehaviour {
public void OnValueChanged(bool isOn) {
if (isOn) {
if (gameObject.name == "boy" || gameObject.name == "girl") {
AvatarSys._instance.SexChange();
return;
}
string[] names = gameObject.name.Split('-');
AvatarSys._instance.OnChangePeople(names[0],names[1]);
switch (names[0]) {
case "pants":
PlayAnimation("item_pants");
break;
case "shoes":
PlayAnimation("item_boots");
break;
case "top":
PlayAnimation("item_shirt");
break;
default:
break;
}
}
}
public void PlayAnimation(string animName) { //换装动画名称
Animation anim = GameObject.FindWithTag("Player").GetComponent<Animation>();
if (!anim.IsPlaying(animName)) {
anim.Play(animName);
anim.PlayQueued("idle1");
}
}
public void LoadScenes() {
SceneManager.LoadScene(1);
}
}
AvatarSys
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AvatarSys : MonoBehaviour {
public static AvatarSys _instance;
private Transform girlSourceTrans;//资源model
private GameObject girlTarget; //骨架物体,换装的人
private Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> girlData = new Dictionary<string, Dictionary<string, SkinnedMeshRenderer>>();
//小女孩所有的资源信息 //部位的名字,部位编号,部位对应的skm
Transform[] girlHips; //小女孩骨骼信息
private Dictionary<string, SkinnedMeshRenderer> girlSmr = new Dictionary<string, SkinnedMeshRenderer>();// 换装骨骼身上的skm信息
//部位的名字,部位对应的skm
private string[,] girlStr = new string[,] { {"eyes","1"},{"hair","1"},{"top","1"},{"pants","1"},{"shoes","1"},{"face","1"}};
//初始化信息
private Transform boySourceTrans;//资源model
private GameObject boyTarget; //骨架物体,换装的人
private Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> boyData = new Dictionary<string, Dictionary<string, SkinnedMeshRenderer>>();
//小女孩所有的资源信息 //部位的名字,部位编号,部位对应的skm
Transform[] boyHips; //小女孩骨骼信息
private Dictionary<string, SkinnedMeshRenderer> boySmr = new Dictionary<string, SkinnedMeshRenderer>();// 换装骨骼身上的skm信息
//部位的名字,部位对应的skm
private string[,] boyStr = new string[,] { { "eyes", "1" }, { "hair", "1" }, { "top", "1" }, { "pants", "1" }, { "shoes", "1" }, { "face", "1" } };
//初始化信息
public int nowCount = 0; // 0代表小女孩,1 男孩
public GameObject girlPanel;
public GameObject boyPanel;
void Awake() {
_instance = this;
DontDestroyOnLoad(this); //不删除游戏物体
}
void Start() {
GirlAvatar();
BoyAvatar();
boyTarget.AddComponent<SpinWithMouse>();
girlTarget.AddComponent<SpinWithMouse>();
boyTarget.SetActive(false);
}
public void GirlAvatar() {
InstantiateGirl();
SaveData(girlSourceTrans,girlData,girlTarget,girlSmr);
InitAvatarGirl();
}
public void BoyAvatar() {
InstantiateBoy();
SaveData(boySourceTrans,boyData,boyTarget,boySmr);
InitAvatarBoy();
}
void InstantiateGirl() {
GameObject go = Instantiate(Resources.Load("FemaleModel")) as GameObject; //加载资源物体
girlSourceTrans = go.transform;
go.SetActive(false);
girlTarget = Instantiate(Resources.Load("FemaleTarget")) as GameObject;
girlHips = girlTarget.GetComponentsInChildren<Transform>();
}
void InstantiateBoy()
{
GameObject go = Instantiate(Resources.Load("MaleModel")) as GameObject; //加载资源物体
boySourceTrans = go.transform;
go.SetActive(false);
boyTarget = Instantiate(Resources.Load("MaleTarget")) as GameObject;
boyHips = boyTarget.GetComponentsInChildren<Transform>();
}
void SaveData(Transform souceTrans,Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> data,GameObject target,
Dictionary<string, SkinnedMeshRenderer> smr) {
data.Clear();
smr.Clear();
if (souceTrans == null)
return;
SkinnedMeshRenderer[] parts = souceTrans.GetComponentsInChildren<SkinnedMeshRenderer>();// 遍历所有子物体有SkinnedMeshRenderer,进行存储
foreach (var part in parts) {
string[] names = part.name.Split('-');
if (!data.ContainsKey(names[0])) { //每次遍历到一个新的部位
//骨骼下边生成对应的skm
GameObject partGo = new GameObject();
partGo.name = names[0];
partGo.transform.parent = target.transform;
smr.Add(names[0],partGo.AddComponent<SkinnedMeshRenderer>()); //把骨骼target身上的skm信息存储,部位只记录一次
data.Add(names[0],new Dictionary<string,SkinnedMeshRenderer>());
}
data[names[0]].Add(names[1],part); //存储所有的skm信息到数据里边
}
}
void ChangeMesh(string part, string num, Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> data,
Transform[] hips, Dictionary<string, SkinnedMeshRenderer> smr,string[,] str)
{ //传入部位,编号,从data里边拿取对应的skm
SkinnedMeshRenderer skm = data[part][num];//要更换的部位
List<Transform> bones = new List<Transform>();
foreach (var trans in skm.bones) {
foreach(var bone in hips){
if (bone.name == trans.name) {
bones.Add(bone);
break;
}
}
}
//换装实现
smr[part].bones = bones.ToArray();//绑定骨骼
smr[part].materials = skm.materials;//替换材质
smr[part].sharedMesh = skm.sharedMesh;//更换mesh
SaveData(part,num,str);
}
void InitAvatarGirl() { //初始化骨架让他有mesh 材质 骨骼信息
int length = girlStr.GetLength(0);//获得行数
for (int i = 0; i < length; i++) {
ChangeMesh(girlStr[i,0],girlStr[i,1],girlData,girlHips,girlSmr,girlStr); //穿上衣服
}
}
void InitAvatarBoy()
{ //初始化骨架让他有mesh 材质 骨骼信息
int length = girlStr.GetLength(0);//获得行数
for (int i = 0; i < length; i++)
{
ChangeMesh(boyStr[i, 0], boyStr[i, 1], boyData, boyHips, boySmr,boyStr); //穿上衣服
}
}
public void OnChangePeople(string part,string num){
if (nowCount == 0)
{ //girl
ChangeMesh(part, num, girlData, girlHips, girlSmr,girlStr);
}
else {
ChangeMesh(part, num, boyData, boyHips, boySmr,boyStr);
}
}
public void SexChange() { //性别转换,人物隐藏,面板隐藏
if (nowCount == 0)
{
nowCount = 1;
boyTarget.SetActive(true);
girlTarget.SetActive(false);
boyPanel.SetActive(true);
girlPanel.SetActive(false);
}
else {
nowCount = 0;
boyTarget.SetActive(false);
girlTarget.SetActive(true);
boyPanel.SetActive(false);
girlPanel.SetActive(true);
}
}
void SaveData(string part,string num,string[,] str) { //更改数据
int length = str.GetLength(0);//获得行数
for (int i = 0; i < length; i++)
{
if (str[i, 0] == part) {
str[i, 1] = num;
}
}
}
}
loadAvatar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class loadAvatar : MonoBehaviour {
// Use this for initialization
void Start () {
if (AvatarSys._instance.nowCount == 0)
{
AvatarSys._instance.GirlAvatar();
}
else {
AvatarSys._instance.BoyAvatar();
}
}
// Update is called once per frame
void Update () {
}
}
SpinWithMouse
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinWithMouse : MonoBehaviour {
private bool isClick = false;
private Vector3 nowPos;
private Vector3 oldPos;
public float length = 5;
void OnMouseUp() { //鼠标抬起
isClick = false;
}
void OnMouseDown() { //鼠标按下
isClick = true;
}
void Update() {
nowPos = Input.mousePosition;
if (isClick) { //鼠标按下不松手
Vector3 offset = nowPos - oldPos;
if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y) && Mathf.Abs(offset.x) > length) { //进行旋转
transform.Rotate(Vector3.up,-offset.x);
}
}
oldPos = Input.mousePosition;
}
}