CustomManager.cs 10 KB

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