CustomManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using GameFramework.Event;
  5. using UnityEngine;
  6. using HumanoidEditor;
  7. using GameFramework.DataTable;
  8. namespace MetaClient
  9. {
  10. public class CustomManager : MonoBehaviour
  11. {
  12. public static CustomManager Instance;
  13. public RoleCustomData bodyData;
  14. public const float valueFloating = 1f;
  15. public CustomRoleController testPlayer = null;
  16. public Dictionary<string, Transform> bones = new Dictionary<string, Transform>();
  17. private void Awake()
  18. {
  19. Instance = this;
  20. }
  21. public void Init()
  22. {
  23. GameEntry.Event.Subscribe(CustomRoleBodyEventArgs.EventId, UpdateBody);
  24. CreateCustomBodyData();
  25. }
  26. /// <summary>
  27. /// 生成玩家自定义数据信息
  28. /// </summary>
  29. public void CreateCustomBodyData()
  30. {
  31. testPlayer = GameObject.Find("标准女").GetComponent<CustomRoleController>();
  32. bodyData = new RoleCustomData();
  33. //初始化
  34. IDataTable<DRCustomBody> drCB = GameEntry.DataTable.GetDataTable<DRCustomBody>();
  35. DRCustomBody[] dtCB = drCB.GetAllDataRows();
  36. for (int i = 0; i < dtCB.Length; i++)
  37. {
  38. var partData = new PartData(
  39. dtCB[i].Id,
  40. dtCB[i].BoneName.Split(','),
  41. dtCB[i].ScaleRangeMin,
  42. dtCB[i].ScaleRangeMax,
  43. dtCB[i].ScaleChange,
  44. dtCB[i].PositionChange,
  45. dtCB[i].RotationChange
  46. );
  47. bodyData.bodyList.Add(dtCB[i].Id, partData);
  48. }
  49. bones.Clear();
  50. foreach (var item in bodyData.bodyList)
  51. {
  52. var key = item.Key;
  53. var value = item.Value;
  54. for (int i = 0; i < value.bones.Length; i++)
  55. {
  56. var transform = CustomRoleUtility.GetChild(testPlayer.transform, value.bones[i]);
  57. if(!transform)
  58. {
  59. continue;
  60. }
  61. if(!bones.ContainsKey(value.bones[i]))
  62. {
  63. bones.Add(value.bones[i], transform);
  64. }
  65. }
  66. }
  67. Debug.Log( "一共有"+ bones.Count + "部位的骨骼自定义修改");
  68. //获取自定义骨骼的初始数据
  69. foreach (var item in bones)
  70. {
  71. var key = item.Key;
  72. var value = item.Value;
  73. bodyData.orginLocalScaleList.Add(key, value.localScale);
  74. }
  75. ////初始缩放
  76. //testPlayer.GetNormalAniBonesScale(ref bodyData.orginLocalScaleList);
  77. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.Chest);
  78. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperArmUp);
  79. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperArmDown);
  80. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperLegUp);
  81. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperLegDown);
  82. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.LowerLegUp);
  83. //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.LowerLegDown);
  84. ////初始位置
  85. //testPlayer.GetExtraAniBonesPosition(ref bodyData.orginLocalPositionList, EditableBodyPart.Chest);
  86. ////初始旋转
  87. //testPlayer.GetExtraAniBonesRotate(ref bodyData.orginLocalRotationList, EditableBodyPart.Chest);
  88. }
  89. public void Clear()
  90. {
  91. GameEntry.Event.Unsubscribe(CustomRoleBodyEventArgs.EventId, UpdateBody);
  92. }
  93. private void UpdateBody(object sender, GameEventArgs e)
  94. {
  95. CustomRoleBodyEventArgs crf = (CustomRoleBodyEventArgs)e;
  96. if (crf != null)
  97. {
  98. //更新数据
  99. var partData = bodyData.bodyList[crf.Part];
  100. if(partData == null)
  101. {
  102. Debug.LogError("ID :" + crf.Part + " 无数据");
  103. return;
  104. }
  105. if(crf.ChangeValue.x == 0.5f)
  106. {
  107. partData.floatValue = 1;
  108. }
  109. if(crf.ChangeValue.x < 0.5f)
  110. {
  111. partData.floatValue = partData.minChangeValue + (1 - partData.minChangeValue) * (crf.ChangeValue.x/0.5f);
  112. }
  113. if(crf.ChangeValue.x > 0.5f)
  114. {
  115. partData.floatValue = 1 + (partData.maxChangeValue - 1) * ((crf.ChangeValue.x - 0.5f)/0.5f);
  116. }
  117. Debug.Log(crf.Part + " : " + crf.ChangeValue + " 转换后 :" + partData.floatValue);
  118. if (partData.scaleChange != Vector3.zero)
  119. {
  120. for (int i = 0; i < partData.bones.Length; i++)
  121. {
  122. var orginBoneScale = bodyData.orginLocalScaleList[partData.bones[i]];
  123. var bone = bones[partData.bones[i]];
  124. var nowBone = bones[partData.bones[i]];
  125. var nowScale = new Vector3(nowBone.localScale.x/orginBoneScale.x, nowBone.localScale.y / orginBoneScale.y, nowBone.localScale.z / orginBoneScale.z);
  126. //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);
  127. var newBoneScale = new Vector3(
  128. partData.scaleChange.x == 1 ? partData.floatValue * orginBoneScale.x : nowBone.localScale.x,
  129. partData.scaleChange.y == 1 ? partData.floatValue * orginBoneScale.y : nowBone.localScale.y,
  130. partData.scaleChange.z == 1 ? partData.floatValue * orginBoneScale.z : nowBone.localScale.z
  131. );
  132. Debug.Log("newBoneScale : " + newBoneScale);
  133. bodyData.changedScaleList[partData.bones[i]] = newBoneScale;
  134. }
  135. }
  136. RefreshRole();
  137. }
  138. }
  139. public float GetBodyBoneValue(int id)
  140. {
  141. Debug.Log(id +": GetBodyBoneValue + " + bodyData.bodyList[id].scaleChange + " " + bodyData.bodyList[id].floatValue);
  142. if (bodyData.bodyList[id].scaleChange.x == 1 || bodyData.bodyList[id].scaleChange.y == 1 || bodyData.bodyList[id].scaleChange.z == 1)
  143. {
  144. if (bodyData.bodyList[id].floatValue == 1)
  145. {
  146. return 0.5f;
  147. }
  148. if (bodyData.bodyList[id].floatValue < 1)
  149. {
  150. return (bodyData.bodyList[id].floatValue - bodyData.bodyList[id].minChangeValue) / (1 - bodyData.bodyList[id].minChangeValue)/2;
  151. }
  152. if (bodyData.bodyList[id].floatValue > 1)
  153. {
  154. return 0.5f + (bodyData.bodyList[id].floatValue - 1) / (bodyData.bodyList[id].maxChangeValue - 1)/2;
  155. }
  156. }
  157. return 0.5f;
  158. }
  159. /// <summary>
  160. /// 重置角色数据
  161. /// </summary>
  162. public void ResetRole()
  163. {
  164. if(testPlayer == null)
  165. {
  166. Debug.LogError("没有模型存在");
  167. return;
  168. }
  169. Debug.Log("重置模型");
  170. foreach (var item in bodyData.bodyList)
  171. {
  172. var key = item.Key;
  173. var value = item.Value;
  174. value.floatValue = 1;
  175. }
  176. //重置缩放
  177. foreach (var item in bodyData.orginLocalScaleList)
  178. {
  179. var key = item.Key;
  180. var value = item.Value;
  181. //testPlayer.SetBoneScale(key, value);
  182. testPlayer.SetBoneWorldScale(key, value);
  183. }
  184. //重置旋转
  185. foreach (var item in bodyData.orginLocalRotationList)
  186. {
  187. var key = item.Key;
  188. var value = item.Value;
  189. testPlayer.SetBoneRot(key, value);
  190. }
  191. //重置位置
  192. foreach (var item in bodyData.orginLocalPositionList)
  193. {
  194. var key = item.Key;
  195. var value = item.Value;
  196. testPlayer.SetBonePos(key, value);
  197. }
  198. }
  199. public void RefreshRole()
  200. {
  201. if (testPlayer == null)
  202. {
  203. Debug.LogError("没有模型存在");
  204. return;
  205. }
  206. Debug.Log("刷新模型");
  207. //刷新缩放
  208. //foreach (var item in bodyData.orginLocalScaleList)
  209. //{
  210. // var key = item.Key;
  211. // var value = item.Value;
  212. // if(bodyData.changedScaleList.ContainsKey(key))
  213. // {
  214. // testPlayer.SetBoneScale(key, bodyData.changedScaleList[key]);
  215. // }
  216. // else
  217. // {
  218. // testPlayer.SetBoneScale(key, value);
  219. // }
  220. //}
  221. foreach (var item in bodyData.changedScaleList)
  222. {
  223. var key = item.Key;
  224. var value = item.Value;
  225. testPlayer.SetBoneScale(key, value);
  226. }
  227. }
  228. }
  229. }