CustomManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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("5").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].BiggerBlendName,
  47. dtCB[i].SmallerBlendName,
  48. dtCB[i].DefaultBlendValue
  49. );
  50. bodyData.bodyList.Add(dtCB[i].Id, partData);
  51. }
  52. bones.Clear();
  53. foreach (var item in bodyData.bodyList)
  54. {
  55. var key = item.Key;
  56. var value = item.Value;
  57. if(!value.useBlend)
  58. {
  59. for (int i = 0; i < value.bones.Length; i++)
  60. {
  61. var transform = CustomRoleUtility.GetChild(testPlayer.transform, value.bones[i]);
  62. if (!transform)
  63. {
  64. continue;
  65. }
  66. if (!bones.ContainsKey(value.bones[i]))
  67. {
  68. bones.Add(value.bones[i], transform);
  69. }
  70. }
  71. }
  72. }
  73. Debug.Log( "一共有"+ bones.Count + "部位的骨骼自定义修改");
  74. //获取自定义骨骼的初始数据
  75. foreach (var item in bones)
  76. {
  77. var key = item.Key;
  78. var value = item.Value;
  79. bodyData.orginLocalScaleList.Add(key, value.localScale);
  80. }
  81. }
  82. public void Clear()
  83. {
  84. GameEntry.Event.Unsubscribe(CustomRoleBodyEventArgs.EventId, UpdateBody);
  85. }
  86. private void UpdateBody(object sender, GameEventArgs e)
  87. {
  88. CustomRoleBodyEventArgs crf = (CustomRoleBodyEventArgs)e;
  89. if (crf != null)
  90. {
  91. //更新数据
  92. var partData = bodyData.bodyList[crf.Part];
  93. if(partData == null)
  94. {
  95. Debug.LogError("ID :" + crf.Part + " 无数据");
  96. return;
  97. }
  98. if(partData.useBlend)
  99. {
  100. ChangeBlendShape(partData, crf.ChangeValue);
  101. }
  102. else
  103. {
  104. ChangeBone(partData, crf.ChangeValue);
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 变形器改变
  110. /// </summary>
  111. /// <param name="partData"></param>
  112. /// <param name="changeValue"></param>
  113. private void ChangeBlendShape(PartData partData, Vector3 changeValue)
  114. {
  115. partData.floatValue = changeValue.x;
  116. if(partData.floatValue == 0.5f)
  117. {
  118. RefreshRoleBlendShape(partData.biggerBlendName, 0);
  119. RefreshRoleBlendShape(partData.smallerBlendName, 0);
  120. }
  121. if(partData.floatValue > 0.5f)
  122. {
  123. RefreshRoleBlendShape(partData.biggerBlendName, (partData.floatValue - 0.5f) * 2 * 100);
  124. RefreshRoleBlendShape(partData.smallerBlendName, 0);
  125. }
  126. if(partData.floatValue < 0.5f)
  127. {
  128. RefreshRoleBlendShape(partData.biggerBlendName, 0);
  129. RefreshRoleBlendShape(partData.smallerBlendName, (0.5f - partData.floatValue) * 2 * 100);
  130. }
  131. }
  132. /// <summary>
  133. /// 骨骼改变
  134. /// </summary>
  135. /// <param name="partData"></param>
  136. /// <param name="changeValue"></param>
  137. private void ChangeBone(PartData partData,Vector3 changeValue)
  138. {
  139. if (changeValue.x == 0.5f)
  140. {
  141. partData.floatValue = 1;
  142. }
  143. if (changeValue.x < 0.5f)
  144. {
  145. partData.floatValue = partData.minChangeValue + (1 - partData.minChangeValue) * (changeValue.x / 0.5f);
  146. }
  147. if (changeValue.x > 0.5f)
  148. {
  149. partData.floatValue = 1 + (partData.maxChangeValue - 1) * ((changeValue.x - 0.5f) / 0.5f);
  150. }
  151. Debug.Log(partData.id + " : " + changeValue + " 转换后 :" + partData.floatValue);
  152. if (partData.scaleChange != Vector3.zero)
  153. {
  154. for (int i = 0; i < partData.bones.Length; i++)
  155. {
  156. var orginBoneScale = bodyData.orginLocalScaleList[partData.bones[i]];
  157. var bone = bones[partData.bones[i]];
  158. var nowBone = bones[partData.bones[i]];
  159. var nowScale = new Vector3(nowBone.localScale.x / orginBoneScale.x, nowBone.localScale.y / orginBoneScale.y, nowBone.localScale.z / orginBoneScale.z);
  160. //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);
  161. var newBoneScale = new Vector3(
  162. partData.scaleChange.x == 1 ? partData.floatValue * orginBoneScale.x : nowBone.localScale.x,
  163. partData.scaleChange.y == 1 ? partData.floatValue * orginBoneScale.y : nowBone.localScale.y,
  164. partData.scaleChange.z == 1 ? partData.floatValue * orginBoneScale.z : nowBone.localScale.z
  165. );
  166. Debug.Log("newBoneScale : " + newBoneScale);
  167. bodyData.changedScaleList[partData.bones[i]] = newBoneScale;
  168. }
  169. }
  170. RefreshRoleBone();
  171. }
  172. public float GetBodyBoneValue(int id)
  173. {
  174. var partData = bodyData.bodyList[id];
  175. if(partData.useBlend)
  176. {
  177. return partData.floatValue;
  178. }
  179. Debug.Log(id +": GetBodyBoneValue + " + partData.scaleChange + " " + partData.floatValue);
  180. if (partData.scaleChange.x == 1 || partData.scaleChange.y == 1 || partData.scaleChange.z == 1)
  181. {
  182. if (partData.floatValue == 1)
  183. {
  184. return 0.5f;
  185. }
  186. if (partData.floatValue < 1)
  187. {
  188. return (partData.floatValue - partData.minChangeValue) / (1 - partData.minChangeValue)/2;
  189. }
  190. if (partData.floatValue > 1)
  191. {
  192. return 0.5f + (partData.floatValue - 1) / (partData.maxChangeValue - 1)/2;
  193. }
  194. }
  195. return 0.5f;
  196. }
  197. /// <summary>
  198. /// 重置角色数据
  199. /// </summary>
  200. public void ResetRole()
  201. {
  202. if(testPlayer == null)
  203. {
  204. Debug.LogError("没有模型存在");
  205. return;
  206. }
  207. Debug.Log("重置模型");
  208. foreach (var item in bodyData.bodyList)
  209. {
  210. var key = item.Key;
  211. var value = item.Value;
  212. if(value.useBlend)
  213. {
  214. value.floatValue = 0;
  215. }
  216. else
  217. {
  218. value.floatValue = 1;
  219. }
  220. }
  221. //重置缩放
  222. foreach (var item in bodyData.orginLocalScaleList)
  223. {
  224. var key = item.Key;
  225. var value = item.Value;
  226. //testPlayer.SetBoneScale(key, value);
  227. testPlayer.SetBoneWorldScale(key, value);
  228. if(bodyData.changedScaleList.ContainsKey(key))
  229. {
  230. bodyData.changedScaleList[key] = bodyData.orginLocalScaleList[key];
  231. }
  232. }
  233. //重置旋转
  234. foreach (var item in bodyData.orginLocalRotationList)
  235. {
  236. var key = item.Key;
  237. var value = item.Value;
  238. testPlayer.SetBoneRot(key, value);
  239. }
  240. //重置位置
  241. foreach (var item in bodyData.orginLocalPositionList)
  242. {
  243. var key = item.Key;
  244. var value = item.Value;
  245. testPlayer.SetBonePos(key, value);
  246. }
  247. testPlayer.ResetAllBlendShape();
  248. }
  249. public void RefreshRoleBlendShape(string name,float value)
  250. {
  251. if (testPlayer == null)
  252. {
  253. Debug.LogError("没有模型存在");
  254. return;
  255. }
  256. testPlayer.SetBlendShape(name, value);
  257. }
  258. public void RefreshRoleBone()
  259. {
  260. if (testPlayer == null)
  261. {
  262. Debug.LogError("没有模型存在");
  263. return;
  264. }
  265. Debug.Log("刷新模型");
  266. //刷新缩放
  267. //foreach (var item in bodyData.orginLocalScaleList)
  268. //{
  269. // var key = item.Key;
  270. // var value = item.Value;
  271. // if(bodyData.changedScaleList.ContainsKey(key))
  272. // {
  273. // testPlayer.SetBoneScale(key, bodyData.changedScaleList[key]);
  274. // }
  275. // else
  276. // {
  277. // testPlayer.SetBoneScale(key, value);
  278. // }
  279. //}
  280. foreach (var item in bodyData.changedScaleList)
  281. {
  282. var key = item.Key;
  283. var value = item.Value;
  284. testPlayer.SetBoneScale(key, value);
  285. }
  286. }
  287. /// <summary>
  288. /// 旋转模特
  289. /// </summary>
  290. public void RotateModel(float offset)
  291. {
  292. }
  293. }
  294. }