425 lines
12 KiB
C#
425 lines
12 KiB
C#
using Metaverse;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine;
|
||
//using System.Text.RegularExpressions;
|
||
|
||
public class Boys : MonoBehaviour
|
||
{
|
||
#region 基础参数
|
||
/// <summary>
|
||
/// 姓名
|
||
/// </summary>
|
||
public string name;
|
||
/// <summary>
|
||
/// 性别
|
||
/// </summary>
|
||
public bool gender;
|
||
/// <summary>
|
||
/// 年龄
|
||
/// </summary>
|
||
public int Age;
|
||
/// <summary>
|
||
/// 肤色
|
||
/// </summary>
|
||
public string colourOfSkin;
|
||
/// <summary>
|
||
/// 体型
|
||
/// </summary>
|
||
//public int shape;
|
||
/// <summary>
|
||
/// 音量
|
||
/// </summary>
|
||
public float volume;
|
||
|
||
public Dictionary<string, AudiosLibrary> audios_library = new Dictionary<string, AudiosLibrary>();
|
||
#endregion
|
||
|
||
#region 面部表情参数
|
||
[SerializeField] Animator an;//动画机
|
||
[SerializeField] int min;//最小值
|
||
[SerializeField] int max;//最大值
|
||
[SerializeField] SkinnedMeshRenderer skinnedMeshRenderer;//面部表情的组件
|
||
#endregion
|
||
|
||
#region 换装参数
|
||
[SerializeField] private AvatarOutlookDataConfig outlookConfig;//上衣参数
|
||
[SerializeField] private AvatarHairDataConfig hairConfig;//头发
|
||
[SerializeField] private AvatarGlassesDataConfig glassesConfig;//眼镜装饰
|
||
[SerializeField] private AvatarBeardDataConfig beardConfig;//胡子
|
||
|
||
[SerializeField] private SkinnedMeshRenderer head, body, top, bottom, footwear, hair, glasses, beard;
|
||
private Material currentHeadMat;
|
||
private Material cacheHeadMat;
|
||
|
||
[SerializeField] private SkinnedMeshRenderer headwuman, bodywuman, topwuman, bottomwuman, footwearwuman, hairwuman, glasseswuman, beardwuman;
|
||
[SerializeField] private SkinnedMeshRenderer headman, bodyman, topman, bottomman, footwearman, hairman, glassesman, beardman;
|
||
#endregion
|
||
|
||
|
||
public static Boys ins;
|
||
[SerializeField] AudioSource au;
|
||
[SerializeField] string str;
|
||
[SerializeField] bool isok = false;
|
||
[SerializeField] int indexs;
|
||
[SerializeField] float clipCount;
|
||
[SerializeField] List<string> strings;
|
||
[SerializeField] List<string> Tempstrings;
|
||
[SerializeField] List<int> cliplenth;
|
||
[SerializeField] TextToSpeechHuman textTo;
|
||
[SerializeField] string[] outManlookConfigStr;
|
||
[SerializeField] string[] outWuManlookConfigStr;
|
||
Regex regex = new Regex("\\$(.*?)\\$");
|
||
public void Awake()
|
||
{
|
||
ins = this;
|
||
AudiosLibrary tempau = new AudiosLibrary();
|
||
tempau.AudiosMan = "x4_lingfeichen_assist";
|
||
tempau.AudiosVolume = 100;
|
||
tempau.AudiosHigth = 10;
|
||
tempau.AudiosSpeed = 50;
|
||
audios_library.Add("重音", tempau);
|
||
}
|
||
private void Start()
|
||
{
|
||
//BodyShow("敬礼");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理字符串,提取出$$中的字符并删除
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
/// <returns></returns>
|
||
public string showStr(string str)
|
||
{
|
||
|
||
strings = new List<string>();
|
||
this.str = str;
|
||
MatchCollection matches = regex.Matches(str);
|
||
foreach (Match match in matches)
|
||
{
|
||
string result = match.Groups[1].Value;
|
||
string replaced = result.Replace("$", "");
|
||
strings.Add(replaced);
|
||
}
|
||
return RemoveDollarSignsAndReplaceNewlines(str);
|
||
}
|
||
/// <summary>
|
||
/// 处理字符串中的$$和$$中的字符
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
string RemoveDollarSignsAndReplaceNewlines(string input)
|
||
{
|
||
Tempstrings = new List<string>();
|
||
Regex regex = new Regex("\\$(.*?)\\$");
|
||
var temp = regex.Split(input);
|
||
string tempstrs = "";
|
||
for (int i = 0; i < temp.Length; i++)
|
||
{
|
||
if (i % 2 == 0)
|
||
{
|
||
tempstrs += temp[i];
|
||
Tempstrings.Add(temp[i]);
|
||
}
|
||
}
|
||
return tempstrs;
|
||
}
|
||
public void showClips(float sum)
|
||
{
|
||
int sumint = 0;
|
||
cliplenth = new List<int>();
|
||
clipCount = str.Length / sum;
|
||
for (int i = 0; i < Tempstrings.Count; i++)
|
||
{
|
||
sumint += Tempstrings[i].Length;
|
||
cliplenth.Add((sumint / 5 /*(int)Math.Round(clipCount, MidpointRounding.AwayFromZero)*/) + 6);
|
||
}
|
||
isok = true;
|
||
}
|
||
|
||
int haircount = 0;
|
||
int biaoqinqcoount = 6;
|
||
private void Update()
|
||
{
|
||
/*
|
||
//换装
|
||
if (Input.GetKeyDown(KeyCode.Q))
|
||
{
|
||
indexs++;
|
||
if (indexs > 14)
|
||
indexs = 0;
|
||
Replacement(indexs);
|
||
}
|
||
|
||
//发型
|
||
if (Input.GetKeyDown(KeyCode.W))
|
||
{
|
||
haircount++;
|
||
if (haircount > 4)
|
||
haircount = 0;
|
||
ChangingHair(haircount);
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.E))
|
||
{
|
||
biaoqinqcoount++;
|
||
if (biaoqinqcoount > 15)
|
||
biaoqinqcoount = 6;
|
||
Show(biaoqinqcoount);
|
||
}
|
||
|
||
//if (Input.GetKeyDown(KeyCode.E))
|
||
//{
|
||
// indexs++;
|
||
//}
|
||
if (Input.GetKeyDown(KeyCode.S))
|
||
{
|
||
show(false);
|
||
}
|
||
*/
|
||
if (strings.Count != 0)
|
||
{
|
||
isok = true;
|
||
}
|
||
else
|
||
{
|
||
isok = false;
|
||
}
|
||
if (isok)
|
||
{
|
||
if ((int)au.time == cliplenth[0])
|
||
{
|
||
switch (strings[0])
|
||
{
|
||
case "摇头":
|
||
headShow("摇头");
|
||
break;
|
||
case "左偏头":
|
||
headShow("左偏头");
|
||
break;
|
||
case "侧头":
|
||
headShow("侧头");
|
||
break;
|
||
case "右偏头":
|
||
headShow("右偏头");
|
||
break;
|
||
case "点头":
|
||
headShow("点头");
|
||
break;
|
||
|
||
case "敬礼":
|
||
BodyShow("敬礼");
|
||
break;
|
||
case "拳击":
|
||
BodyShow("拳击");
|
||
break;
|
||
case "鼓掌":
|
||
BodyShow("鼓掌");
|
||
break;
|
||
|
||
case "鞠躬":
|
||
legShow("鞠躬");
|
||
break;
|
||
case "跪下":
|
||
legShow("跪下");
|
||
break;
|
||
case "后仰":
|
||
legShow("后仰");
|
||
break;
|
||
case "跳跃":
|
||
legShow("跳跃");
|
||
break;
|
||
default:
|
||
Debug.Log("动画库中没有该名字:" + strings[0]);
|
||
break;
|
||
}
|
||
strings.RemoveAt(0);
|
||
cliplenth.RemoveAt(0);
|
||
}
|
||
Debug.Log((int)au.time);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 改变说话音量
|
||
/// </summary>
|
||
/// <param name="sum"></param>
|
||
public void SetVolume(int sum = 100)
|
||
{
|
||
textTo.volume = sum;
|
||
}
|
||
/// <summary>
|
||
/// 读取男性已有衣服列表
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string[] GetManOutlookConfig()
|
||
{
|
||
return outManlookConfigStr.Length == 0 ? null : outManlookConfigStr;
|
||
}
|
||
/// <summary>
|
||
/// 读取女性已有衣服列表
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string[] GetWuManOutlookConfig()
|
||
{
|
||
return outWuManlookConfigStr.Length == 0 ? null : outWuManlookConfigStr;
|
||
}
|
||
/// <summary>
|
||
/// true是男,false是女
|
||
/// </summary>
|
||
/// <param name="man"></param>
|
||
private void show(bool man)
|
||
{
|
||
if (man)
|
||
{
|
||
head = headman;
|
||
body = bodyman;
|
||
top = topman;
|
||
bottom = bottomman;
|
||
footwear = footwearman;
|
||
hair = hairman;
|
||
glasses = glassesman;
|
||
}
|
||
else
|
||
{
|
||
head = headwuman;
|
||
body = bodywuman;
|
||
top = topwuman;
|
||
bottom = bottomwuman;
|
||
footwear = footwearwuman;
|
||
hair = hairwuman;
|
||
glasses = glasseswuman;
|
||
}
|
||
}
|
||
#region 动画
|
||
/// <summary>
|
||
/// 头动画(摇头、左摇头、侧头、右偏头、点头)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void headShow(string name, int index = 1)
|
||
{
|
||
//an.SetFloat("head1", index);
|
||
an.Play(name, index, 0);
|
||
}
|
||
/// <summary>
|
||
/// 手动画(敬礼、拳击、鼓掌)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void BodyShow(string name, int index = 2)
|
||
{
|
||
an.Play(name, index, 0);
|
||
}
|
||
/// <summary>
|
||
/// 腿动画+身体动画(鞠躬、跪下、后仰、跳跃)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void legShow(string name, int index = 3)
|
||
{
|
||
an.Play(name, index, 0);
|
||
}
|
||
#endregion
|
||
|
||
#region 面部表情
|
||
/// <summary>
|
||
/// 面部表情(6-15)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void Show(int index)
|
||
{
|
||
int temp = Math.Clamp(index, 6, 15);
|
||
StopCoroutine(init(temp));
|
||
StartCoroutine(init(temp));
|
||
}
|
||
IEnumerator init(int index)
|
||
{
|
||
for (int i = min; i < max; i++)
|
||
{
|
||
skinnedMeshRenderer.SetBlendShapeWeight(i, 0);
|
||
}
|
||
yield return new WaitForSeconds(0.1f);
|
||
for (int i = 30; i < 101; i++)
|
||
{
|
||
yield return new WaitForSeconds(0.01f);
|
||
skinnedMeshRenderer.SetBlendShapeWeight(index, i);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 换装部分
|
||
/// <summary>
|
||
/// 换衣服(范围0-14)
|
||
/// </summary>
|
||
/// <param name="index1"></param>
|
||
public void Replacement(int index1 = 0)
|
||
{
|
||
int temp = Math.Clamp(index1, 0, 14);
|
||
int index = temp;
|
||
head.sharedMesh = outlookConfig.data[index].headMesh;
|
||
head.sharedMaterial = currentHeadMat != null ? currentHeadMat : outlookConfig.data[index].headMaterial;
|
||
cacheHeadMat = outlookConfig.data[index].headMaterial;
|
||
|
||
body.sharedMesh = outlookConfig.data[index].bodyMesh;
|
||
body.sharedMaterial = outlookConfig.data[index].bodyMaterial;
|
||
|
||
top.sharedMesh = outlookConfig.data[index].topMesh;
|
||
top.sharedMaterial = outlookConfig.data[index].topMaterial;
|
||
|
||
bottom.sharedMesh = outlookConfig.data[index].bottomMesh;
|
||
bottom.sharedMaterial = outlookConfig.data[index].bottomMaterial;
|
||
|
||
footwear.sharedMesh = outlookConfig.data[index].footwearMesh;
|
||
footwear.sharedMaterial = outlookConfig.data[index].footwearMaterial;
|
||
}
|
||
/// <summary>
|
||
/// 换头发(范围0-4)
|
||
/// </summary>
|
||
/// <param name="index1"></param>
|
||
public void ChangingHair(int index1 = 0)
|
||
{
|
||
int temp = Math.Clamp(index1, 0, 4);
|
||
int index = temp;
|
||
hair.sharedMesh = hairConfig.data[index].hairMesh;
|
||
hair.sharedMaterial = hairConfig.data[index].hairMaterial;
|
||
}
|
||
/// <summary>
|
||
/// 换眼镜(范围0-5)
|
||
/// </summary>
|
||
/// <param name="index1"></param>
|
||
public void ChangeGlasses(int index1 = 0)
|
||
{
|
||
int temp = Math.Clamp(index1, 0, 5);
|
||
int index = temp;
|
||
glasses.sharedMesh = glassesConfig.data[index].glassesMesh;
|
||
glasses.sharedMaterial = glassesConfig.data[index].glassesMaterial;
|
||
}
|
||
/// <summary>
|
||
/// 换脸(范围0-4)
|
||
/// </summary>
|
||
/// <param name="index1"></param>
|
||
public void faceTransplant(int index1)
|
||
{
|
||
int temp = Math.Clamp(index1, 0, 4);
|
||
int index = temp;
|
||
var data = beardConfig.data[index];
|
||
|
||
Debug.Log(data.type);
|
||
switch (data.type)
|
||
{
|
||
case AvatarBeardData.Type.Mesh:
|
||
beard.sharedMesh = beardConfig.data[index].beardMesh;
|
||
beard.sharedMaterial = beardConfig.data[index].beardMaterial;
|
||
currentHeadMat = null;
|
||
if (cacheHeadMat != null) head.sharedMaterial = cacheHeadMat;
|
||
break;
|
||
case AvatarBeardData.Type.Texture:
|
||
head.sharedMaterial = beardConfig.data[index].beardMaterial;
|
||
currentHeadMat = beardConfig.data[index].beardMaterial;
|
||
beard.sharedMesh = null;
|
||
beard.sharedMaterial = null;
|
||
break;
|
||
}
|
||
}
|
||
#endregion
|
||
} |