using System; using System.Collections; using System.Collections.Generic; using GameFramework.Event; using UnityEngine; using GameFramework.DataTable; using DG.Tweening; namespace MetaClient { public class CustomManager : MonoBehaviour { public static CustomManager Instance; public RoleCustomData bodyData; public const float valueFloating = 1f; public CustomRoleController testPlayer = null; public Dictionary bones = new Dictionary(); private void Awake() { Instance = this; } public void Init() { GameEntry.Event.Subscribe(CustomRoleBodyEventArgs.EventId, UpdateBody); CreateCustomBodyData(); } /// /// 生成玩家自定义数据信息 /// public void CreateCustomBodyData() { testPlayer = GameObject.Find("123123").GetComponent(); bodyData = new RoleCustomData(); //初始化身体部分 IDataTable drCB = GameEntry.DataTable.GetDataTable(); DRCustomBody[] dtCB = drCB.GetAllDataRows(); for (int i = 0; i < dtCB.Length; i++) { var partData = new PartData( dtCB[i].Id, dtCB[i].BoneName.Split(','), dtCB[i].ScaleRangeMin, dtCB[i].ScaleRangeMax, dtCB[i].ScaleChange, dtCB[i].PositionChange, dtCB[i].RotationChange, dtCB[i].IsBlend, dtCB[i].BiggerBlendName, dtCB[i].SmallerBlendName, dtCB[i].DefaultBlendValue, dtCB[i].ExtraKeepBoneName, dtCB[i].IsKeepChildrenScale ); bodyData.bodyList.Add(dtCB[i].Id, partData); } //初始化脸部分 IDataTable drCF = GameEntry.DataTable.GetDataTable(); DRCustomFace[] dtCF = drCF.GetAllDataRows(); for (int i = 0; i < dtCF.Length; i++) { var partData = new PartData( dtCF[i].Id, dtCF[i].BoneName==""?null:dtCF[i].BoneName.Split(','), dtCF[i].ScaleRangeMin, dtCF[i].ScaleRangeMax, dtCF[i].ScaleChange, dtCF[i].PositionChange, dtCF[i].RotationChange, dtCF[i].IsBlend, dtCF[i].BiggerBlendName, dtCF[i].SmallerBlendName, dtCF[i].DefaultBlendValue, "", true, dtCF[i].IsMirror ); bodyData.faceList.Add(dtCF[i].Id, partData); } bones.Clear(); foreach (var item in bodyData.bodyList) { var key = item.Key; var value = item.Value; if(!value.useBlend) { for (int i = 0; i < value.bones.Length; i++) { var transform = CustomRoleUtility.GetChild(testPlayer.transform, value.bones[i]); if (!transform) { continue; } if (!bones.ContainsKey(value.bones[i])) { bones.Add(value.bones[i], transform); } } } } foreach (var item in bodyData.faceList) { var key = item.Key; var value = item.Value; if (!value.useBlend) { for (int i = 0; i < value.bones.Length; i++) { var transform = CustomRoleUtility.GetChild(testPlayer.transform, value.bones[i]); if (!transform) { continue; } if (!bones.ContainsKey(value.bones[i])) { bones.Add(value.bones[i], transform); } } } } Debug.Log( "一共有"+ bones.Count + "部位的骨骼自定义修改"); //获取自定义骨骼的初始数据 foreach (var item in bones) { var key = item.Key; var value = item.Value; bodyData.orginLocalScaleList.Add(key, value.localScale); bodyData.orginChangeScaleList.Add(key, new Vector3(1,1,1)); bodyData.ChangedScaleParamsList.Add(key, new Vector3(1,1,1)); bodyData.orginLocalPositionList.Add(key, value.localPosition); bodyData.orginLocalRotationList.Add(key, value.localEulerAngles); } } public void Clear() { GameEntry.Event.Unsubscribe(CustomRoleBodyEventArgs.EventId, UpdateBody); } private void UpdateBody(object sender, GameEventArgs e) { CustomRoleBodyEventArgs crf = (CustomRoleBodyEventArgs)e; if (crf != null) { PartData partData = null; switch (crf.Type) { case ECustomStyple.None: break; case ECustomStyple.NieLian: partData = bodyData.faceList[crf.Part]; break; case ECustomStyple.Body: partData = bodyData.bodyList[crf.Part]; break; case ECustomStyple.Cloth: break; case ECustomStyple.Ornament: break; default: break; } //更新数据 if(partData == null) { Debug.LogError("ID :" + crf.Part + " 无数据"); return; } if(partData.useBlend) { ChangeBlendShape(partData, crf.ChangeValue); } else { ChangeBone(partData, crf.ChangeValue); } } } /// /// 变形器改变 /// /// /// private void ChangeBlendShape(PartData partData, Vector3 changeValue) { partData.floatValue = changeValue.x; Debug.Log(partData.id + " : "+ partData.floatValue); if(partData.oneBlend) { RefreshRoleBlendShape(partData.biggerBlendName, partData.floatValue * 100); return; } if(partData.floatValue == 0.5f) { RefreshRoleBlendShape(partData.biggerBlendName, 0); RefreshRoleBlendShape(partData.smallerBlendName, 0); } if(partData.floatValue > 0.5f) { RefreshRoleBlendShape(partData.biggerBlendName, (partData.floatValue - 0.5f) * 2 * 100); RefreshRoleBlendShape(partData.smallerBlendName, 0); } if(partData.floatValue < 0.5f) { RefreshRoleBlendShape(partData.biggerBlendName, 0); RefreshRoleBlendShape(partData.smallerBlendName, (0.5f - partData.floatValue) * 2 * 100); } } /// /// 骨骼改变 /// /// /// private void ChangeBone(PartData partData,Vector3 changeValue) { if (partData.scaleChange != Vector3.zero) { if (changeValue.x == 0.5f) { partData.floatValue = 1; } if (changeValue.x < 0.5f) { partData.floatValue = partData.minChangeValue + (1 - partData.minChangeValue) * (changeValue.x / 0.5f); } if (changeValue.x > 0.5f) { partData.floatValue = 1 + (partData.maxChangeValue - 1) * ((changeValue.x - 0.5f) / 0.5f); } Debug.Log(partData.id + " : " + changeValue + " 转换后 :" + partData.floatValue); for (int i = 0; i < partData.bones.Length; i++) { //var orginBoneScale = bodyData.orginWorldScaleList[partData.bones[i]]; var orginBoneScale = bodyData.orginLocalScaleList[partData.bones[i]]; var changeScale = bodyData.orginChangeScaleList[partData.bones[i]]; var bone = bones[partData.bones[i]]; var nowBone = bones[partData.bones[i]]; //var scale = nowBone.lossyScale; var scale = nowBone.localScale; var nowScale = new Vector3(scale.x / orginBoneScale.x, scale.y / orginBoneScale.y, scale.z / orginBoneScale.z); //var newBoneScale = new Vector3(orginBoneScale.x * (partData.scaleChange.x == 1 ? partData.floatValue : 1) * nowScale.x, orginBoneScale.y * (partData.scaleChange.y == 1 ? partData.floatValue : 1) * nowScale.y, orginBoneScale.z * (partData.scaleChange.z == 1 ? partData.floatValue : 1) * nowScale.y); var newBoneScale = new Vector3( partData.scaleChange.x == 1 ? partData.floatValue * orginBoneScale.x * changeScale.x : scale.x, partData.scaleChange.y == 1 ? partData.floatValue * orginBoneScale.y * changeScale.y : scale.y, partData.scaleChange.z == 1 ? partData.floatValue * orginBoneScale.z * changeScale.z : scale.z ); bodyData.changedScaleList[partData.bones[i]] = newBoneScale; bodyData.ChangedScaleParamsList[partData.bones[i]] = new Vector3(newBoneScale.x / orginBoneScale.x / changeScale.x, newBoneScale.y / orginBoneScale.y / changeScale.y, newBoneScale.z / orginBoneScale.z / changeScale.z); Debug.Log(partData.bones[i] + " : " + bodyData.ChangedScaleParamsList[partData.bones[i]]); if(!partData.keepChild) { continue; } //子物体 var count = nowBone.childCount; for (int j = 0; j < count; j++) { var childname = nowBone.GetChild(j).name; if (bones.ContainsKey(childname)) { var orginChildBoneScale = bodyData.orginLocalScaleList[childname]; var beforeChildScaleParams = bodyData.ChangedScaleParamsList[childname]; bodyData.orginChangeScaleList[childname] = new Vector3(orginBoneScale.x / newBoneScale.x, orginBoneScale.y / newBoneScale.y, orginBoneScale.z / newBoneScale.z); bodyData.changedScaleList[childname] = new Vector3( beforeChildScaleParams.x * bodyData.orginChangeScaleList[childname].x * orginChildBoneScale.x, beforeChildScaleParams.y * bodyData.orginChangeScaleList[childname].y * orginChildBoneScale.y, beforeChildScaleParams.z * bodyData.orginChangeScaleList[childname].z * orginChildBoneScale.z ); Debug.Log(childname + " : " + bodyData.ChangedScaleParamsList[childname]); } } if (partData.extraBones == null || partData.extraBones.Length == 0) { continue; } for (int j = 0; j < partData.extraBones.Length; j++) { var childname = partData.extraBones[j]; if (bones.ContainsKey(childname)) { var orginChildBoneScale = bodyData.orginLocalScaleList[childname]; var beforeChildScaleParams = bodyData.ChangedScaleParamsList[childname]; bodyData.orginChangeScaleList[childname] = new Vector3(orginBoneScale.x / newBoneScale.x, orginBoneScale.y / newBoneScale.y, orginBoneScale.z / newBoneScale.z); bodyData.changedScaleList[childname] = new Vector3( beforeChildScaleParams.x * bodyData.orginChangeScaleList[childname].x * orginChildBoneScale.x, beforeChildScaleParams.y * bodyData.orginChangeScaleList[childname].y * orginChildBoneScale.y, beforeChildScaleParams.z * bodyData.orginChangeScaleList[childname].z * orginChildBoneScale.z ); Debug.Log(childname + " : " + bodyData.ChangedScaleParamsList[childname]); } } } } //修改位置 if (partData.positionChange != Vector3.zero) { if (changeValue.x == 0.5f) { partData.floatValue = 0; } if (changeValue.x < 0.5f) { partData.floatValue = partData.minChangeValue * (0.5f - changeValue.x) * 2; } if (changeValue.x > 0.5f) { partData.floatValue = partData.maxChangeValue * (changeValue.x - 0.5f) * 2; } Debug.Log(partData.id + " : " + changeValue + " 转换后 :" + partData.floatValue); for (int i = 0; i < partData.bones.Length; i++) { var nowBone = bones[partData.bones[i]]; var orginBonePosition = bodyData.orginLocalPositionList[partData.bones[i]]; var newBonePosition = new Vector3( partData.positionChange.x == 1 ? partData.floatValue + orginBonePosition.x : orginBonePosition.x, partData.positionChange.y == 1 ? partData.floatValue + orginBonePosition.y : orginBonePosition.y, partData.positionChange.z == 1 ? partData.floatValue + orginBonePosition.z : orginBonePosition.z ); bodyData.changedPositionList[partData.bones[i]] = newBonePosition; } } //修改旋转 if (partData.rotationChange != Vector3.zero) { if (changeValue.x == 0.5f) { partData.floatValue = 0; } if (changeValue.x < 0.5f) { partData.floatValue = partData.minChangeValue * (0.5f - changeValue.x) * 2; } if (changeValue.x > 0.5f) { partData.floatValue = partData.maxChangeValue * (changeValue.x - 0.5f) * 2; } Debug.Log(partData.id + " : " + changeValue + " 转换后 :" + partData.floatValue); for (int i = 0; i < partData.bones.Length; i++) { var changeVal = 1; if(i == 1 && partData.isMirror) { changeVal = -1; } var nowBone = bones[partData.bones[i]]; var rotate = nowBone.localEulerAngles; var orginBoneRot = bodyData.orginLocalRotationList[partData.bones[i]]; var newBoneRot = new Vector3( partData.rotationChange.x == 1 ? changeVal * partData.floatValue + orginBoneRot.x : rotate.x, partData.rotationChange.y == 1 ? changeVal * partData.floatValue + orginBoneRot.y : rotate.y, partData.rotationChange.z == 1 ? changeVal * partData.floatValue + orginBoneRot.z : rotate.z ); bodyData.changedRotationList[partData.bones[i]] = newBoneRot; } } RefreshRoleBone(); } public float GetBodyBoneValue(ECustomStyple styple,int id) { PartData partData = null; switch (styple) { case ECustomStyple.None: break; case ECustomStyple.NieLian: partData = bodyData.faceList[id]; break; case ECustomStyple.Body: partData = bodyData.bodyList[id]; break; case ECustomStyple.Cloth: break; case ECustomStyple.Ornament: break; default: break; } if(partData == null) { return 0; } Debug.Log(partData.id + " :" + partData.useBlend + " : " + partData.floatValue); if (partData.useBlend) { return partData.floatValue; } Debug.Log(id +": GetBodyBoneValue + " + partData.scaleChange + " " + partData.floatValue); if (partData.scaleChange.x == 1 || partData.scaleChange.y == 1 || partData.scaleChange.z == 1) { if (partData.floatValue == 1) { return 0.5f; } if (partData.floatValue < 1) { return (partData.floatValue - partData.minChangeValue) / (1 - partData.minChangeValue)/2; } if (partData.floatValue > 1) { return 0.5f + (partData.floatValue - 1) / (partData.maxChangeValue - 1)/2; } } if (partData.positionChange.x == 1 || partData.positionChange.y == 1 || partData.positionChange.z == 1) { if (partData.floatValue == 0) { return 0.5f; } if (partData.floatValue < 0) { return partData.floatValue / partData.maxChangeValue/2; } if (partData.floatValue > 0) { return 0.5f + partData.floatValue / partData.maxChangeValue / 2; } } if (partData.rotationChange.x == 1 || partData.rotationChange.y == 1 || partData.rotationChange.z == 1) { if (partData.floatValue == 0) { return 0.5f; } if (partData.floatValue < 0) { return partData.floatValue / partData.maxChangeValue / 2; } if (partData.floatValue > 0) { return 0.5f + partData.floatValue / partData.maxChangeValue / 2; } } return 0.5f; } /// /// 重置角色数据 /// public void ResetRole() { if(testPlayer == null) { Debug.LogError("没有模型存在"); return; } Debug.Log("重置模型"); foreach (var item in bodyData.bodyList) { var key = item.Key; var value = item.Value; if(value.useBlend) { value.floatValue = 0; } else { value.floatValue = 1; } } //重置缩放 foreach (var item in bodyData.orginLocalScaleList) { var key = item.Key; var value = item.Value; testPlayer.SetBoneScale(key, value); //testPlayer.SetBoneWorldScale(key, value); if (bodyData.changedScaleList.ContainsKey(key)) { bodyData.changedScaleList[key] = bodyData.orginLocalScaleList[key]; } bodyData.orginChangeScaleList[key] = new Vector3(1, 1, 1); bodyData.ChangedScaleParamsList[key] = new Vector3(1, 1, 1); } //重置旋转 foreach (var item in bodyData.orginLocalRotationList) { var key = item.Key; var value = item.Value; testPlayer.SetBoneRot(key, value); } //重置位置 foreach (var item in bodyData.orginLocalPositionList) { var key = item.Key; var value = item.Value; testPlayer.SetBonePos(key, value); } testPlayer.ResetAllBlendShape(); } public void RefreshRoleBlendShape(string name,float value) { if (testPlayer == null) { Debug.LogError("没有模型存在"); return; } testPlayer.SetBlendShape(name, value); } public void RefreshRoleBone() { if (testPlayer == null) { Debug.LogError("没有模型存在"); return; } Debug.Log("刷新模型"); //刷新缩放 //foreach (var item in bodyData.orginLocalScaleList) //{ // var key = item.Key; // var value = item.Value; // if(bodyData.changedScaleList.ContainsKey(key)) // { // testPlayer.SetBoneScale(key, bodyData.changedScaleList[key]); // } // else // { // testPlayer.SetBoneScale(key, value); // } //} foreach (var item in bodyData.changedScaleList) { var key = item.Key; var value = item.Value; //testPlayer.SetBoneWorldScale(key, value, bodyData.orginLocalScaleList[key], bodyData.orginWorldScaleList[key]); testPlayer.SetBoneScale(key, value); } foreach (var item in bodyData.changedPositionList) { var key = item.Key; var value = item.Value; testPlayer.SetBonePos(key, value); } foreach (var item in bodyData.changedRotationList) { var key = item.Key; var value = item.Value; testPlayer.SetBoneRot(key, value); } } /// /// 旋转模特 /// public void RotateModel(float offset) { if (testPlayer == null) { Debug.LogError("没有模型存在"); return; } testPlayer.RotateSelf(offset); } public void ChangeCustomCamera(ECustomStyple type) { switch (type) { case ECustomStyple.None: break; case ECustomStyple.NieLian: Camera.main.transform.DOMove(new Vector3(-0.26f, 0.4f, -5), 0.3f); Camera.main.DOOrthoSize(0.25f, 0.3f); break; case ECustomStyple.Body: case ECustomStyple.Cloth: Camera.main.transform.DOMove(new Vector3(0, -0.24f, -5), 0.3f); Camera.main.DOOrthoSize(1.5f, 0.3f); break; case ECustomStyple.Ornament: break; default: break; } } public void ChangeClothing(int id) { Debug.Log("换衣服咯 : " + id); IDataTable drCloth = GameEntry.DataTable.GetDataTable(); DRClothing clothdata = drCloth.GetDataRow(id); if(clothdata == null) { Debug.LogError("cloth " + id + "不存在"); return; } var path = clothdata.Prefab; GameEntry.Resource.LoadAsset(AssetUtility.GetMeshAsset(path), new GameFramework.Resource.LoadAssetCallbacks( (assetName, asset, duration, userData) => { GameObject go = GameObject.Instantiate((GameObject)asset, testPlayer.transform); var component = testPlayer.GetComponent(); component.ChangeCloth((ClothType)clothdata.ParentStyple, go); } )); } /// /// 保存 /// public void Save() { } /// /// 加载 /// public void Reload() { } } }