RolePreviewWindow.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditor.Callbacks;
  7. using UnityEditor.Compilation;
  8. using UnityEditor.Experimental.UIElements.GraphView;
  9. using LuaInterface;
  10. using UnityEditor.Experimental.GraphView;
  11. namespace RO.Editor
  12. {
  13. public class RolePreviewWindow : EditorWindow, ISearchWindowProvider
  14. {
  15. [MenuItem("RO_Tool/主角预览")]
  16. private static RolePreviewWindow OpenWindow()
  17. {
  18. var window = EditorWindow.GetWindow<RolePreviewWindow>("主角预览");
  19. window.minSize = new Vector2(260, 280);
  20. return window;
  21. }
  22. [DidReloadScripts]
  23. private static void OnScriptReload()
  24. {
  25. var windows = Resources.FindObjectsOfTypeAll(typeof(RolePreviewWindow));
  26. foreach (var window in windows)
  27. {
  28. try
  29. {
  30. ((RolePreviewWindow)window).OnDisable();
  31. ((RolePreviewWindow)window).OnEnable();
  32. }
  33. catch
  34. {
  35. DestroyImmediate(window);
  36. }
  37. }
  38. }
  39. public enum SearchTreeType
  40. {
  41. None,
  42. RoleCfg,
  43. FashionCfg,
  44. }
  45. public enum SexType
  46. {
  47. Man = 2,
  48. Woman = 1,
  49. }
  50. public enum HandWeaponType
  51. {
  52. Right = 1,
  53. Left = 2,
  54. Double = 3,
  55. }
  56. public enum FashionSlotType
  57. {
  58. HeadTop = 1, // 头上
  59. HeadMiddle = 2, // 头中
  60. HeadBottom = 3, // 头下
  61. Cloth = 4, // 衣服
  62. Weapon = 5, // 武器
  63. BodyBack = 6, // 背部
  64. HairStyle = 7, // 发型
  65. Pupil = 8, // 瞳孔
  66. }
  67. public enum SkinSlotType
  68. {
  69. HeadTop = 1, // 头上
  70. HeadMiddle = 2, // 头中
  71. HeadBottom = 3, // 头下
  72. Cloth = 4, // 衣服
  73. LeftHand = 5, // 左手
  74. RightHand = 6, // 右手
  75. BodyBack = 7, // 背部
  76. HairStyle = 8, // 发型
  77. Pupil = 9, // 瞳孔
  78. HairColor = 101, // 头发颜色
  79. Face = 1000, // 脸型
  80. }
  81. public enum RoleInEnvType
  82. {
  83. Battle = 1,
  84. GuildLobby = 2,
  85. CreateRole = 3,
  86. RoleMainView = 4,
  87. Transfer = 5,
  88. }
  89. public struct RoleCfgData
  90. {
  91. public int id;
  92. public string name;
  93. public int avatarId;
  94. public SexType sex;
  95. public int leftWeaponId;
  96. public int rightWeaponId;
  97. public int clothId;
  98. public int hairId;
  99. public int eyeId;
  100. public int headTopId;
  101. public int headMiddleId;
  102. public int headBottomId;
  103. public int bodyBackId;
  104. public string battleCtrl;
  105. public string cityCtrl;
  106. public string createShowCtrl;
  107. public string roleShowCtrl;
  108. public string transferShowCtrl;
  109. }
  110. public struct FashionCfgData
  111. {
  112. public int id;
  113. public string name;
  114. public int avatarId;
  115. public int extAvatarId;
  116. public bool hideHair;
  117. public FashionSlotType fashionSlotType;
  118. }
  119. public struct RoleAvatarCfgData
  120. {
  121. public int id;
  122. public string avatarPrefab;
  123. public Vector3? position;
  124. public Quaternion? quaternion;
  125. public Vector3? scale;
  126. }
  127. public struct AvatarCfgData
  128. {
  129. public int id;
  130. public string avatarPrefab;
  131. }
  132. public class PopupData
  133. {
  134. private List<string> m_NameLs = new List<string>();
  135. private List<int> m_IdLs = new List<int>();
  136. public string[] names;
  137. public int[] ids;
  138. public void Add(string name, int id)
  139. {
  140. m_NameLs.Add(name);
  141. m_IdLs.Add(id);
  142. }
  143. public void Normalize()
  144. {
  145. names = m_NameLs.ToArray();
  146. ids = m_IdLs.ToArray();
  147. m_NameLs.Clear();
  148. m_IdLs.Clear();
  149. }
  150. }
  151. private const string c_GlobalCfgPath = "Assets/Content/Config/GlobalCfg.csv";
  152. private const string c_RoleCfgPath = "Assets/Lua/Config/RoleCfg.lua";
  153. private const string c_FashionCfgPath = "Assets/Lua/Config/FashionCfg.lua";
  154. private const string c_RoleAvatarCfgPath = "Assets/Lua/Config/RoleAvatarCfg.lua";
  155. private const string c_AvatarCfgPath = "Assets/Lua/Config/AvatarCfg.lua";
  156. private const string c_LanguagePackagePath = "Assets/Content/Config/LanguagePackage_cn.csv";
  157. private bool m_Inited = false;
  158. private string m_DefaultMaleBaseAvatarPrefab = "";
  159. private string m_DefaultFemaleBaseAvatarPrefab = "";
  160. private int m_DefaultMaleFaceAvatarId = -1;
  161. private int m_DefaultFemaleFaceAvatarId = -1;
  162. private Dictionary<int, RoleCfgData> m_RoleCfgMap = new Dictionary<int, RoleCfgData>();
  163. private Dictionary<int, FashionCfgData> m_FashionCfgMap = new Dictionary<int, FashionCfgData>();
  164. private Dictionary<int, RoleAvatarCfgData> m_RoleAvatarCfgMap = new Dictionary<int, RoleAvatarCfgData>();
  165. private Dictionary<int, AvatarCfgData> m_AvatarCfgMap = new Dictionary<int, AvatarCfgData>();
  166. private Dictionary<string, string> m_LanguageMap = new Dictionary<string, string>();
  167. private List<SearchTreeEntry> m_RoleCfgSearchTree = new List<SearchTreeEntry>();
  168. private Dictionary<FashionSlotType, List<SearchTreeEntry>> m_FashionCfgSearchTree = new Dictionary<FashionSlotType, List<SearchTreeEntry>>();
  169. private SearchTreeType m_SearchTreeType = SearchTreeType.None;
  170. private FashionSlotType m_SearchTreeFashionSlotType = FashionSlotType.HeadTop;
  171. private int m_CurrentRoleCfgId = -1;
  172. private RoleInEnvType m_CurrentRoleInEnvType = RoleInEnvType.Battle;
  173. private Dictionary<FashionSlotType, int> m_EquipFashionMap = new Dictionary<FashionSlotType, int>();
  174. private Dictionary<FashionSlotType, string> m_FashionSlotTypeNameMap = new Dictionary<FashionSlotType, string>()
  175. {
  176. [FashionSlotType.HeadTop] = "头上",
  177. [FashionSlotType.HeadMiddle] = "头中",
  178. [FashionSlotType.HeadBottom] = "头下",
  179. [FashionSlotType.Cloth] = "衣服",
  180. [FashionSlotType.Weapon] = "武器",
  181. [FashionSlotType.BodyBack] = "背部",
  182. [FashionSlotType.HairStyle] = "发型",
  183. [FashionSlotType.Pupil] = "瞳孔",
  184. };
  185. private Dictionary<FashionSlotType, SkinSlotType> m_FashionSlotToSkinSlotMap = new Dictionary<FashionSlotType, SkinSlotType>()
  186. {
  187. [FashionSlotType.HeadTop] = SkinSlotType.HeadTop,
  188. [FashionSlotType.HeadMiddle] = SkinSlotType.HeadMiddle,
  189. [FashionSlotType.HeadBottom] = SkinSlotType.HeadBottom,
  190. [FashionSlotType.Cloth] = SkinSlotType.Cloth,
  191. [FashionSlotType.BodyBack] = SkinSlotType.BodyBack,
  192. [FashionSlotType.HairStyle] = SkinSlotType.HairStyle,
  193. [FashionSlotType.Pupil] = SkinSlotType.Pupil,
  194. };
  195. private Dictionary<SkinSlotType, string> m_SkinSlotBindBone = new Dictionary<SkinSlotType, string>()
  196. {
  197. [SkinSlotType.HeadTop] = "Head_Point",
  198. [SkinSlotType.HeadMiddle] = "Head_Point",
  199. [SkinSlotType.HeadBottom] = "Head_Point",
  200. [SkinSlotType.Cloth] = "Bip001 Spine1",
  201. [SkinSlotType.LeftHand] = "LHand_Point",
  202. [SkinSlotType.RightHand] = "RHand_Point",
  203. [SkinSlotType.BodyBack] = "Back_Point",
  204. [SkinSlotType.HairStyle] = "Bip001 Head",
  205. [SkinSlotType.Pupil] = "Bip001 Head",
  206. };
  207. private GameObject m_CurrentGo;
  208. private Dictionary<string, Transform> m_CurrentGoBoneMap = new Dictionary<string, Transform>();
  209. private Dictionary<SkinSlotType, List<Renderer>> m_BaseSkinSlotTypeMap = new Dictionary<SkinSlotType, List<Renderer>>();
  210. private Dictionary<SkinSlotType, List<Renderer>> m_ExtSkinSlotTypeMap = new Dictionary<SkinSlotType, List<Renderer>>();
  211. private void OnEnable()
  212. {
  213. InitConfig();
  214. CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted;
  215. CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
  216. }
  217. private void OnDisable()
  218. {
  219. CompilationPipeline.assemblyCompilationStarted -= OnAssemblyCompilationStarted;
  220. CompilationPipeline.assemblyCompilationFinished -= OnAssemblyCompilationFinished;
  221. DisposeModel();
  222. m_CurrentRoleCfgId = -1;
  223. m_CurrentRoleInEnvType = RoleInEnvType.Battle;
  224. m_Inited = false;
  225. }
  226. private void OnFocus()
  227. {
  228. if (m_CurrentGo)
  229. {
  230. Selection.activeObject = m_CurrentGo;
  231. }
  232. }
  233. private void OnAssemblyCompilationStarted(string path)
  234. {
  235. if (path.EndsWith("Assembly-CSharp-Editor.dll"))
  236. DisposeModel();
  237. }
  238. private void OnAssemblyCompilationFinished(string path, CompilerMessage[] messages)
  239. {
  240. if (path.EndsWith("Assembly-CSharp-Editor.dll"))
  241. CreateModel();
  242. }
  243. private void OnGUI()
  244. {
  245. if (!m_Inited)
  246. {
  247. GUILayout.FlexibleSpace();
  248. EditorGUILayout.HelpBox("配置初始化失败,请检查相关配置", MessageType.Error);
  249. GUILayout.FlexibleSpace();
  250. return;
  251. }
  252. EditorGUILayout.Space();
  253. EditorGUILayout.BeginHorizontal();
  254. EditorGUILayout.LabelField("当前角色的使用环境", GUILayout.MaxWidth(100));
  255. var newRoleInEnvType = (RoleInEnvType)EditorGUILayout.EnumPopup(m_CurrentRoleInEnvType);
  256. if (newRoleInEnvType != m_CurrentRoleInEnvType)
  257. {
  258. m_CurrentRoleInEnvType = newRoleInEnvType;
  259. RefreshAnimator();
  260. }
  261. EditorGUILayout.EndHorizontal();
  262. EditorGUILayout.Space();
  263. EditorGUILayout.BeginHorizontal();
  264. EditorGUILayout.LabelField("选择角色基础模型", GUILayout.MaxWidth(100));
  265. string roleCfgName = "无";
  266. if (m_RoleCfgMap.ContainsKey(m_CurrentRoleCfgId))
  267. {
  268. roleCfgName = m_RoleCfgMap[m_CurrentRoleCfgId].name;
  269. }
  270. GUIContent content = EditorGUIUtility.TrTempContent(roleCfgName);
  271. GUIStyle style = "MiniPullDown";
  272. Rect rect = GUILayoutUtility.GetRect(content, style);
  273. if (EditorGUI.DropdownButton(rect, content, FocusType.Passive, style))
  274. {
  275. OpenSearchWindw(EditorGUIUtility.GUIToScreenPoint(new Vector2(rect.xMin, rect.yMax)), SearchTreeType.RoleCfg);
  276. }
  277. EditorGUILayout.EndHorizontal();
  278. EditorGUI.BeginDisabledGroup(!m_RoleCfgMap.ContainsKey(m_CurrentRoleCfgId));
  279. EditorGUILayout.Separator();
  280. for (FashionSlotType fashionSlotType = FashionSlotType.HeadTop; fashionSlotType <= FashionSlotType.Pupil; fashionSlotType++)
  281. {
  282. EditorGUILayout.BeginHorizontal();
  283. EditorGUILayout.LabelField("选择 " + m_FashionSlotTypeNameMap[fashionSlotType] + " 时装", GUILayout.MaxWidth(100));
  284. string fashionCfgName = "无";
  285. if (m_EquipFashionMap.ContainsKey(fashionSlotType))
  286. {
  287. int id = m_EquipFashionMap[fashionSlotType];
  288. if (m_FashionCfgMap.ContainsKey(id))
  289. {
  290. fashionCfgName = m_FashionCfgMap[id].name;
  291. }
  292. }
  293. content = EditorGUIUtility.TrTempContent(fashionCfgName);
  294. rect = GUILayoutUtility.GetRect(content, style);
  295. if (EditorGUI.DropdownButton(rect, content, FocusType.Passive, style))
  296. {
  297. OpenSearchWindw(EditorGUIUtility.GUIToScreenPoint(new Vector2(rect.xMin, rect.yMax)), SearchTreeType.FashionCfg, fashionSlotType);
  298. }
  299. EditorGUILayout.EndHorizontal();
  300. EditorGUILayout.Space();
  301. }
  302. EditorGUILayout.Separator();
  303. EditorGUI.EndDisabledGroup();
  304. }
  305. #region Init Config
  306. private void InitConfig()
  307. {
  308. ShowNotification(EditorGUIUtility.TrTempContent("读取配置中"));
  309. m_Inited = false;
  310. m_DefaultMaleFaceAvatarId = -1;
  311. m_DefaultFemaleFaceAvatarId = -1;
  312. m_RoleCfgMap.Clear();
  313. m_FashionCfgMap.Clear();
  314. m_RoleAvatarCfgMap.Clear();
  315. m_AvatarCfgMap.Clear();
  316. m_LanguageMap.Clear();
  317. try
  318. {
  319. TextAsset textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(c_LanguagePackagePath);
  320. string text = textAsset.text;
  321. string[] lines = text.Split('\n');
  322. for (int i = 3, iMax = lines.Length; i < iMax; i++)
  323. {
  324. string line = lines[i];
  325. if (string.IsNullOrEmpty(line)) continue;
  326. string[] values = line.Split(',');
  327. if (values.Length < 2) continue;
  328. if (string.IsNullOrEmpty(values[0])) continue;
  329. if (m_LanguageMap.ContainsKey(values[0]))
  330. {
  331. Debug.LogError(c_LanguagePackagePath + "存在相同的Id :" + values[0]);
  332. continue;
  333. }
  334. m_LanguageMap.Add(values[0], values[1]);
  335. }
  336. textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(c_GlobalCfgPath);
  337. text = textAsset.text;
  338. lines = text.Split('\n');
  339. for (int i = 3, iMax = lines.Length; i < iMax; i++)
  340. {
  341. string line = lines[i];
  342. if (string.IsNullOrEmpty(line)) continue;
  343. string[] values = line.Split(',');
  344. if (values.Length < 5) continue;
  345. if (string.IsNullOrEmpty(values[0])) continue;
  346. switch (values[0])
  347. {
  348. case "42":
  349. int.TryParse(values[2], out m_DefaultMaleFaceAvatarId);
  350. break;
  351. case "43":
  352. int.TryParse(values[2], out m_DefaultFemaleFaceAvatarId);
  353. break;
  354. case "44":
  355. m_DefaultMaleBaseAvatarPrefab = values[3];
  356. break;
  357. case "45":
  358. m_DefaultFemaleBaseAvatarPrefab = values[3];
  359. break;
  360. }
  361. }
  362. using (LuaState luaState = new LuaState())
  363. {
  364. try
  365. {
  366. luaState.Start();
  367. // LuaForEach<int, LuaTable>(luaState, c_GlobalCfgPath, InitFaceConfig);
  368. LuaForEach<int, LuaTable>(luaState, c_RoleCfgPath, InitRoleConfig);
  369. LuaForEach<int, LuaTable>(luaState, c_FashionCfgPath, InitFashionConfig);
  370. LuaForEach<int, LuaTable>(luaState, c_RoleAvatarCfgPath, InitRoleAvatarConfig);
  371. LuaForEach<int, LuaTable>(luaState, c_AvatarCfgPath, InitAvatarConfig);
  372. m_Inited = true;
  373. }
  374. catch (Exception e)
  375. {
  376. Debug.LogException(e);
  377. }
  378. }
  379. }
  380. catch (Exception e)
  381. {
  382. Debug.LogException(e);
  383. }
  384. m_RoleCfgSearchTree.Clear();
  385. List<int> m_ManIds = new List<int>();
  386. List<int> m_WomanIds = new List<int>();
  387. foreach (var item in m_RoleCfgMap)
  388. {
  389. if (item.Value.sex == SexType.Man)
  390. {
  391. m_ManIds.Add(item.Key);
  392. }
  393. else
  394. {
  395. m_WomanIds.Add(item.Key);
  396. }
  397. }
  398. m_ManIds.Sort();
  399. m_WomanIds.Sort();
  400. int level = 0;
  401. SearchTreeGroupEntry rootEntry = new SearchTreeGroupEntry(new GUIContent("性别"), level);
  402. m_RoleCfgSearchTree.Add(rootEntry);
  403. level++;
  404. SearchTreeGroupEntry manEntry = new SearchTreeGroupEntry(new GUIContent("男"), level);
  405. manEntry.userData = SexType.Man;
  406. m_RoleCfgSearchTree.Add(manEntry);
  407. level++;
  408. for (int i = 0, iMax = m_ManIds.Count; i < iMax; i++)
  409. {
  410. int id = m_ManIds[i];
  411. SearchTreeEntry itemEntry = new SearchTreeEntry(new GUIContent(m_RoleCfgMap[id].name));
  412. itemEntry.level = level;
  413. itemEntry.userData = m_RoleCfgMap[id];
  414. m_RoleCfgSearchTree.Add(itemEntry);
  415. }
  416. level--;
  417. SearchTreeGroupEntry womanEntry = new SearchTreeGroupEntry(new GUIContent("女"), level);
  418. level++;
  419. m_RoleCfgSearchTree.Add(womanEntry);
  420. for (int i = 0, iMax = m_WomanIds.Count; i < iMax; i++)
  421. {
  422. int id = m_WomanIds[i];
  423. SearchTreeEntry itemEntry = new SearchTreeEntry(new GUIContent(m_RoleCfgMap[id].name));
  424. itemEntry.userData = m_RoleCfgMap[id];
  425. itemEntry.level = level;
  426. m_RoleCfgSearchTree.Add(itemEntry);
  427. }
  428. level--;
  429. level--;
  430. Dictionary<FashionSlotType, List<int>> dic = new Dictionary<FashionSlotType, List<int>>();
  431. foreach (var item in m_FashionCfgMap)
  432. {
  433. List<int> ls;
  434. if (!dic.TryGetValue(item.Value.fashionSlotType, out ls))
  435. {
  436. ls = new List<int>();
  437. dic.Add(item.Value.fashionSlotType, ls);
  438. }
  439. ls.Add(item.Key);
  440. }
  441. level = 0;
  442. m_FashionCfgSearchTree.Clear();
  443. for (FashionSlotType fashionSlotType = FashionSlotType.HeadTop; fashionSlotType <= FashionSlotType.Pupil; fashionSlotType++)
  444. {
  445. List<SearchTreeEntry> ls = new List<SearchTreeEntry>();
  446. SearchTreeGroupEntry groupEntry = new SearchTreeGroupEntry(new GUIContent(m_FashionSlotTypeNameMap[fashionSlotType]), level);
  447. groupEntry.userData = fashionSlotType;
  448. ls.Add(groupEntry);
  449. level++;
  450. SearchTreeEntry nullEntry = new SearchTreeEntry(new GUIContent("空"));
  451. nullEntry.level = level;
  452. nullEntry.userData = fashionSlotType;
  453. ls.Add(nullEntry);
  454. if (dic.ContainsKey(fashionSlotType))
  455. {
  456. List<int> ids = dic[fashionSlotType];
  457. ids.Sort();
  458. for (int i = 0, iMax = ids.Count; i < iMax; i++)
  459. {
  460. int id = ids[i];
  461. SearchTreeEntry itemEntry = new SearchTreeEntry(new GUIContent(m_FashionCfgMap[id].name));
  462. itemEntry.userData = m_FashionCfgMap[id];
  463. itemEntry.level = level;
  464. ls.Add(itemEntry);
  465. }
  466. }
  467. m_FashionCfgSearchTree.Add(fashionSlotType, ls);
  468. level--;
  469. }
  470. RemoveNotification();
  471. }
  472. private void InitFaceConfig(int key, LuaTable value)
  473. {
  474. if (key == 42)
  475. {
  476. m_DefaultMaleFaceAvatarId = value.RawGet<string, int>("IVal");
  477. }
  478. else if (key == 43)
  479. {
  480. m_DefaultFemaleFaceAvatarId = value.RawGet<string, int>("IVal");
  481. }
  482. else if (key == 44)
  483. {
  484. m_DefaultMaleBaseAvatarPrefab = value.RawGet<string, string>("SVal");
  485. }
  486. else if (key == 45)
  487. {
  488. m_DefaultFemaleBaseAvatarPrefab = value.RawGet<string, string>("SVal");
  489. }
  490. }
  491. private void InitRoleConfig(int key, LuaTable value)
  492. {
  493. if (m_RoleCfgMap.ContainsKey(key))
  494. {
  495. Debug.LogError(c_RoleCfgPath + "存在相同的Id :" + key);
  496. return;
  497. }
  498. RoleCfgData roleCfgData = new RoleCfgData()
  499. {
  500. id = key,
  501. name = value.RawGet<string, string>("Name"),
  502. avatarId = value.RawGet<string, int>("AvatarId"),
  503. sex = (SexType)value.RawGet<string, int>("Sex"),
  504. leftWeaponId = value.RawGet<string, int>("LeftWeapon"),
  505. rightWeaponId = value.RawGet<string, int>("RightWeapon"),
  506. clothId = value.RawGet<string, int>("ClothId"),
  507. hairId = value.RawGet<string, int>("Hair"),
  508. eyeId = value.RawGet<string, int>("Eye"),
  509. headTopId = value.RawGet<string, int>("HeadTop"),
  510. headMiddleId = value.RawGet<string, int>("HeadMiddle"),
  511. headBottomId = value.RawGet<string, int>("HeadBottom"),
  512. bodyBackId = value.RawGet<string, int>("BodyBackId"),
  513. battleCtrl = value.RawGet<string, string>("BattleCtrl"),
  514. cityCtrl = value.RawGet<string, string>("CityCtrl"),
  515. createShowCtrl = value.RawGet<string, string>("CreateShowCtrl"),
  516. roleShowCtrl = value.RawGet<string, string>("RoleShowCtrl"),
  517. transferShowCtrl = value.RawGet<string, string>("TransferShowCtrl"),
  518. };
  519. if (m_LanguageMap.ContainsKey(roleCfgData.name))
  520. {
  521. roleCfgData.name = roleCfgData.id + " (" + m_LanguageMap[roleCfgData.name] + ")";
  522. }
  523. else
  524. {
  525. roleCfgData.name = roleCfgData.id + " (" + roleCfgData.name + ")";
  526. }
  527. m_RoleCfgMap.Add(key, roleCfgData);
  528. }
  529. private void InitFashionConfig(int key, LuaTable value)
  530. {
  531. if (m_FashionCfgMap.ContainsKey(key))
  532. {
  533. Debug.LogError(c_FashionCfgPath + "存在相同的Id :" + key);
  534. return;
  535. }
  536. FashionCfgData fashionCfgData = new FashionCfgData()
  537. {
  538. id = key,
  539. name = value.RawGet<string, string>("FashionName"),
  540. avatarId = value.RawGet<string, int>("RoleAvatarID"),
  541. extAvatarId = value.RawGet<string, int>("ExtRoleAvatarID"),
  542. hideHair = value.RawGet<string, bool>("FashionHideHair"),
  543. fashionSlotType = (FashionSlotType)value.RawGet<string, int>("FashionLocation"),
  544. };
  545. if (m_LanguageMap.ContainsKey(fashionCfgData.name))
  546. {
  547. fashionCfgData.name = fashionCfgData.id + " (" + m_LanguageMap[fashionCfgData.name] + ")";
  548. }
  549. else
  550. {
  551. fashionCfgData.name = fashionCfgData.id + " (" + fashionCfgData.name + ")";
  552. }
  553. m_FashionCfgMap.Add(key, fashionCfgData);
  554. }
  555. private void InitRoleAvatarConfig(int key, LuaTable value)
  556. {
  557. if (m_RoleAvatarCfgMap.ContainsKey(key))
  558. {
  559. Debug.LogError(c_RoleAvatarCfgPath + "存在相同的Id :" + key);
  560. return;
  561. }
  562. RoleAvatarCfgData roleAvatarCfgData = new RoleAvatarCfgData()
  563. {
  564. id = key,
  565. avatarPrefab = value.RawGet<string, string>("AvatarPrefab"),
  566. };
  567. LuaTable childValue = value.RawGet<string, LuaTable>("ModelPosition");
  568. if (childValue != null)
  569. roleAvatarCfgData.position = LuaTableToVector3(childValue);
  570. childValue = value.RawGet<string, LuaTable>("ModelRotation");
  571. if (childValue != null)
  572. roleAvatarCfgData.quaternion = Quaternion.Euler(LuaTableToVector3(childValue));
  573. childValue = value.RawGet<string, LuaTable>("ModelScale");
  574. if (childValue != null)
  575. roleAvatarCfgData.scale = LuaTableToVector3(childValue);
  576. m_RoleAvatarCfgMap.Add(key, roleAvatarCfgData);
  577. }
  578. private void InitAvatarConfig(int key, LuaTable value)
  579. {
  580. if (m_AvatarCfgMap.ContainsKey(key))
  581. {
  582. Debug.LogError(c_AvatarCfgPath + "存在相同的Id :" + key);
  583. return;
  584. }
  585. AvatarCfgData avatarCfgData = new AvatarCfgData()
  586. {
  587. id = key,
  588. avatarPrefab = value.RawGet<string, string>("AvatarPrefab"),
  589. };
  590. m_AvatarCfgMap.Add(key, avatarCfgData);
  591. }
  592. private void InitLanguagePackage(string key, LuaTable value)
  593. {
  594. if (m_LanguageMap.ContainsKey(key))
  595. {
  596. Debug.LogError(c_LanguagePackagePath + "存在相同的Id :" + key);
  597. return;
  598. }
  599. string language = value.RawGet<string, string>("Language");
  600. m_LanguageMap.Add(key, language);
  601. }
  602. private void LuaForEach<K, V>(LuaState luaState, string luaFile, Action<K, V> foreachFun)
  603. {
  604. using (LuaTable luaTable = luaState.DoFile<LuaTable>(luaFile))
  605. {
  606. try
  607. {
  608. using (LuaDictTable<K, V> luaDictTable = luaTable.ToDictTable<K, V>())
  609. {
  610. try
  611. {
  612. foreach (var item in luaDictTable)
  613. {
  614. foreachFun(item.Key, item.Value);
  615. }
  616. }
  617. catch (Exception e)
  618. {
  619. Debug.LogException(e);
  620. }
  621. }
  622. }
  623. catch (Exception e)
  624. {
  625. Debug.LogException(e);
  626. }
  627. }
  628. }
  629. private Vector3 LuaTableToVector3(LuaTable luaArrayTable)
  630. {
  631. Vector3 value = Vector3.zero;
  632. value.x = ((luaArrayTable[1] != null) ? (luaArrayTable.RawGetIndex<float>(1)) : 0);
  633. value.y = ((luaArrayTable[2] != null) ? (luaArrayTable.RawGetIndex<float>(2)) : 0);
  634. value.z = ((luaArrayTable[3] != null) ? (luaArrayTable.RawGetIndex<float>(3)) : 0);
  635. return value;
  636. }
  637. #endregion
  638. #region Search Windw
  639. private void OpenSearchWindw(Vector2 alginPos, SearchTreeType searchTreeType, FashionSlotType fashionSlotType = FashionSlotType.HeadTop)
  640. {
  641. m_SearchTreeType = searchTreeType;
  642. m_SearchTreeFashionSlotType = fashionSlotType;
  643. if (m_SearchTreeType == SearchTreeType.None) return;
  644. SearchWindowContext context = new SearchWindowContext(alginPos + new Vector2(120, 16));
  645. SearchWindow.Open(context, this);
  646. }
  647. #region ISearchWindowProvider
  648. public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
  649. {
  650. if (m_SearchTreeType == SearchTreeType.RoleCfg)
  651. {
  652. return m_RoleCfgSearchTree;
  653. }
  654. if (m_SearchTreeType == SearchTreeType.FashionCfg)
  655. {
  656. return m_FashionCfgSearchTree[m_SearchTreeFashionSlotType];
  657. }
  658. return null;
  659. }
  660. public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context)
  661. {
  662. if (m_SearchTreeType == SearchTreeType.RoleCfg)
  663. {
  664. if (SearchTreeEntry.userData == null)
  665. {
  666. if (m_CurrentRoleCfgId != -1)
  667. {
  668. m_CurrentRoleCfgId = -1;
  669. m_EquipFashionMap.Clear();
  670. CreateModel();
  671. }
  672. }
  673. else
  674. {
  675. RoleCfgData roleCfgData = (RoleCfgData)SearchTreeEntry.userData;
  676. int id = roleCfgData.id;
  677. if (m_CurrentRoleCfgId != id)
  678. {
  679. m_CurrentRoleCfgId = id;
  680. m_EquipFashionMap.Clear();
  681. CreateModel();
  682. }
  683. }
  684. }
  685. else if (m_SearchTreeType == SearchTreeType.FashionCfg)
  686. {
  687. if (SearchTreeEntry.userData is FashionSlotType)
  688. {
  689. m_EquipFashionMap.Remove((FashionSlotType)SearchTreeEntry.userData);
  690. RefreshModel();
  691. }
  692. else
  693. {
  694. FashionCfgData fashionCfgData = (FashionCfgData)SearchTreeEntry.userData;
  695. if (m_EquipFashionMap.ContainsKey(fashionCfgData.fashionSlotType))
  696. {
  697. if (m_EquipFashionMap[fashionCfgData.fashionSlotType] != fashionCfgData.id)
  698. {
  699. m_EquipFashionMap[fashionCfgData.fashionSlotType] = fashionCfgData.id;
  700. RefreshModel();
  701. }
  702. }
  703. else
  704. {
  705. m_EquipFashionMap.Add(fashionCfgData.fashionSlotType, fashionCfgData.id);
  706. RefreshModel();
  707. }
  708. }
  709. }
  710. m_SearchTreeType = SearchTreeType.None;
  711. Repaint();
  712. return true;
  713. }
  714. #endregion
  715. #endregion
  716. #region Model
  717. private void CreateModel()
  718. {
  719. DisposeModel();
  720. if (!m_RoleCfgMap.ContainsKey(m_CurrentRoleCfgId)) return;
  721. RoleCfgData roleCfgData = m_RoleCfgMap[m_CurrentRoleCfgId];
  722. string avatarPrefab = ((roleCfgData.sex == SexType.Man) ? m_DefaultMaleBaseAvatarPrefab : m_DefaultFemaleBaseAvatarPrefab);
  723. if (string.IsNullOrEmpty(avatarPrefab)) return;
  724. GameObject sourceGo = AssetDatabase.LoadAssetAtPath<GameObject>(Constants.ModelPath + "/" + avatarPrefab + ".prefab");
  725. if (!sourceGo) return;
  726. m_CurrentGo = Instantiate(sourceGo);
  727. m_CurrentGo.transform.SetParent(null);
  728. m_CurrentGo.transform.localPosition = Vector3.zero;
  729. m_CurrentGo.transform.localRotation = Quaternion.identity;
  730. m_CurrentGo.transform.localScale = Vector3.one;
  731. BuildBoneMap(m_CurrentGo.transform.Find("Bip001"));
  732. // 脸
  733. int faceAvatarId = -1;
  734. faceAvatarId = ((roleCfgData.sex == SexType.Man) ? m_DefaultMaleFaceAvatarId : m_DefaultFemaleFaceAvatarId);
  735. if (m_RoleAvatarCfgMap.ContainsKey(faceAvatarId))
  736. {
  737. var roleAvatarCfg = m_RoleAvatarCfgMap[faceAvatarId];
  738. GameObject faceGo = AssetDatabase.LoadAssetAtPath<GameObject>(Constants.ModelPath + "/" + roleAvatarCfg.avatarPrefab + ".prefab");
  739. m_BaseSkinSlotTypeMap.Add(SkinSlotType.Face, BindGo(SkinSlotType.Face, faceGo, roleAvatarCfg.position, roleAvatarCfg.quaternion, roleAvatarCfg.scale));
  740. }
  741. RefreshAnimator();
  742. RefreshModel();
  743. RefreshPreviewGo();
  744. }
  745. private void DisposeModel()
  746. {
  747. m_CurrentGoBoneMap.Clear();
  748. m_BaseSkinSlotTypeMap.Clear();
  749. m_ExtSkinSlotTypeMap.Clear();
  750. if (!m_CurrentGo) return;
  751. GameObject.DestroyImmediate(m_CurrentGo);
  752. m_CurrentGo = null;
  753. }
  754. private void RefreshAnimator()
  755. {
  756. if (!m_CurrentGo) return;
  757. Animator animator = m_CurrentGo.GetComponent<Animator>();
  758. if (!animator)
  759. {
  760. animator = m_CurrentGo.AddComponent<Animator>();
  761. }
  762. RoleCfgData roleCfgData = m_RoleCfgMap[m_CurrentRoleCfgId];
  763. string controllerPath = null;
  764. if (m_CurrentRoleInEnvType == RoleInEnvType.Battle)
  765. controllerPath = roleCfgData.battleCtrl;
  766. else if (m_CurrentRoleInEnvType == RoleInEnvType.GuildLobby)
  767. controllerPath = roleCfgData.cityCtrl;
  768. else if (m_CurrentRoleInEnvType == RoleInEnvType.CreateRole)
  769. controllerPath = roleCfgData.createShowCtrl;
  770. else if (m_CurrentRoleInEnvType == RoleInEnvType.RoleMainView)
  771. controllerPath = roleCfgData.roleShowCtrl;
  772. else if (m_CurrentRoleInEnvType == RoleInEnvType.Transfer)
  773. controllerPath = roleCfgData.transferShowCtrl;
  774. if (!string.IsNullOrEmpty(controllerPath))
  775. {
  776. var controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(Constants.AnimatorPath + "/" + controllerPath + ".controller");
  777. if (controller)
  778. {
  779. animator.runtimeAnimatorController = controller;
  780. return;
  781. }
  782. }
  783. animator.runtimeAnimatorController = null;
  784. }
  785. private void RefreshModel()
  786. {
  787. if (!m_CurrentGo) return;
  788. RoleCfgData roleCfgData = m_RoleCfgMap[m_CurrentRoleCfgId];
  789. // 找是否有互斥的数据 (目前是头发)
  790. bool isHideHair = false;
  791. if (m_EquipFashionMap.ContainsKey(FashionSlotType.HeadTop))
  792. {
  793. int fashionId = m_EquipFashionMap[FashionSlotType.HeadTop];
  794. if (m_FashionCfgMap.ContainsKey(fashionId))
  795. {
  796. isHideHair = m_FashionCfgMap[fashionId].hideHair;
  797. }
  798. }
  799. for (FashionSlotType fashionSlotType = FashionSlotType.HeadTop; fashionSlotType <= FashionSlotType.Pupil; fashionSlotType++)
  800. {
  801. int avatarId = 0;
  802. if (fashionSlotType == FashionSlotType.Weapon)
  803. {
  804. RemoveSkinSlotType(m_BaseSkinSlotTypeMap, SkinSlotType.LeftHand);
  805. RemoveSkinSlotType(m_BaseSkinSlotTypeMap, SkinSlotType.RightHand);
  806. if (roleCfgData.leftWeaponId != 0)
  807. {
  808. if (m_EquipFashionMap.ContainsKey(fashionSlotType))
  809. {
  810. int fashionId = m_EquipFashionMap[fashionSlotType];
  811. if (m_FashionCfgMap.ContainsKey(fashionId))
  812. {
  813. avatarId = m_FashionCfgMap[fashionId].avatarId;
  814. }
  815. }
  816. if (avatarId == 0)
  817. {
  818. avatarId = roleCfgData.leftWeaponId;
  819. }
  820. if (m_RoleAvatarCfgMap.ContainsKey(avatarId))
  821. {
  822. var roleAvatarCfg = m_RoleAvatarCfgMap[avatarId];
  823. GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(Constants.ModelPath + "/" + roleAvatarCfg.avatarPrefab + ".prefab");
  824. m_BaseSkinSlotTypeMap.Add(SkinSlotType.LeftHand, BindGo(SkinSlotType.LeftHand, go, roleAvatarCfg.position, roleAvatarCfg.quaternion, roleAvatarCfg.scale));
  825. }
  826. }
  827. avatarId = 0;
  828. if (roleCfgData.rightWeaponId != 0)
  829. {
  830. if (m_EquipFashionMap.ContainsKey(fashionSlotType))
  831. {
  832. int fashionId = m_EquipFashionMap[fashionSlotType];
  833. if (m_FashionCfgMap.ContainsKey(fashionId))
  834. {
  835. avatarId = m_FashionCfgMap[fashionId].extAvatarId;
  836. }
  837. }
  838. if (avatarId == 0)
  839. {
  840. avatarId = roleCfgData.rightWeaponId;
  841. }
  842. if (m_RoleAvatarCfgMap.ContainsKey(avatarId))
  843. {
  844. var roleAvatarCfg = m_RoleAvatarCfgMap[avatarId];
  845. GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(Constants.ModelPath + "/" + roleAvatarCfg.avatarPrefab + ".prefab");
  846. m_BaseSkinSlotTypeMap.Add(SkinSlotType.RightHand, BindGo(SkinSlotType.RightHand, go, roleAvatarCfg.position, roleAvatarCfg.quaternion, roleAvatarCfg.scale));
  847. }
  848. }
  849. }
  850. else
  851. {
  852. if (m_EquipFashionMap.ContainsKey(fashionSlotType))
  853. {
  854. int fashionId = m_EquipFashionMap[fashionSlotType];
  855. if (m_FashionCfgMap.ContainsKey(fashionId))
  856. {
  857. avatarId = m_FashionCfgMap[fashionId].avatarId;
  858. }
  859. }
  860. if (avatarId == 0)
  861. {
  862. avatarId = GetDefaultAvatarId(roleCfgData, fashionSlotType);
  863. }
  864. SkinSlotType skinSlotType = m_FashionSlotToSkinSlotMap[fashionSlotType];
  865. if (skinSlotType == SkinSlotType.HairStyle && isHideHair)
  866. {
  867. avatarId = 0;
  868. }
  869. RemoveSkinSlotType(m_BaseSkinSlotTypeMap, skinSlotType);
  870. if (m_RoleAvatarCfgMap.ContainsKey(avatarId))
  871. {
  872. var roleAvatarCfg = m_RoleAvatarCfgMap[avatarId];
  873. GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(Constants.ModelPath + "/" + roleAvatarCfg.avatarPrefab + ".prefab");
  874. m_BaseSkinSlotTypeMap.Add(skinSlotType, BindGo(skinSlotType, go, roleAvatarCfg.position, roleAvatarCfg.quaternion, roleAvatarCfg.scale));
  875. }
  876. }
  877. }
  878. }
  879. private void RemoveSkinSlotType(Dictionary<SkinSlotType, List<Renderer>> map, SkinSlotType skinSlotType)
  880. {
  881. if (map.ContainsKey(skinSlotType))
  882. {
  883. if (map[skinSlotType] != null)
  884. {
  885. foreach (var item in map[skinSlotType])
  886. {
  887. GameObject.DestroyImmediate(item.gameObject);
  888. }
  889. }
  890. map.Remove(skinSlotType);
  891. }
  892. }
  893. private int GetDefaultAvatarId(RoleCfgData roleCfgData, FashionSlotType fashionSlotType)
  894. {
  895. if (fashionSlotType == FashionSlotType.HeadTop)
  896. return roleCfgData.headTopId;
  897. else if (fashionSlotType == FashionSlotType.HeadMiddle)
  898. return roleCfgData.headMiddleId;
  899. else if (fashionSlotType == FashionSlotType.HeadBottom)
  900. return roleCfgData.headBottomId;
  901. // else if (fashionSlotType == FashionSlotType.Weapon)
  902. // return roleCfgData.leftWeaponId;
  903. else if (fashionSlotType == FashionSlotType.Cloth)
  904. return roleCfgData.clothId;
  905. else if (fashionSlotType == FashionSlotType.BodyBack)
  906. return roleCfgData.bodyBackId;
  907. else if (fashionSlotType == FashionSlotType.HairStyle)
  908. return roleCfgData.hairId;
  909. else if (fashionSlotType == FashionSlotType.Pupil)
  910. return roleCfgData.eyeId;
  911. return 0;
  912. }
  913. private List<Renderer> BindGo(SkinSlotType skinSlotType, GameObject go, Vector3? pos, Quaternion? quaternion, Vector3? scale)
  914. {
  915. if (!m_CurrentGo) return null;
  916. if (!go) return null;
  917. bool needDestroy = true;
  918. var sourceTrans = go.transform;
  919. go = GameObject.Instantiate(go);
  920. var trans = go.transform;
  921. Transform parentTrans = null;
  922. if (m_SkinSlotBindBone.ContainsKey(skinSlotType))
  923. {
  924. if (m_CurrentGoBoneMap.ContainsKey(m_SkinSlotBindBone[skinSlotType]))
  925. {
  926. parentTrans = m_CurrentGoBoneMap[m_SkinSlotBindBone[skinSlotType]];
  927. }
  928. else
  929. {
  930. parentTrans = m_CurrentGo.transform;
  931. }
  932. }
  933. else
  934. {
  935. parentTrans = m_CurrentGo.transform;
  936. }
  937. trans.SetParent(parentTrans);
  938. trans.localPosition = ((pos.HasValue) ? pos.Value : sourceTrans.localPosition);
  939. trans.localRotation = ((quaternion.HasValue) ? quaternion.Value : sourceTrans.localRotation);
  940. trans.localScale = ((scale.HasValue) ? scale.Value : sourceTrans.localScale);
  941. Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
  942. List<Renderer> ls = new List<Renderer>();
  943. foreach (var renderer in renderers)
  944. {
  945. var mrTrans = renderer.transform;
  946. if (mrTrans == trans)
  947. {
  948. needDestroy = false;
  949. }
  950. if (renderer is SkinnedMeshRenderer)
  951. {
  952. mrTrans.SetParent(m_CurrentGo.transform);
  953. ls.Add(BindSMR(renderer as SkinnedMeshRenderer));
  954. }
  955. else
  956. {
  957. mrTrans.SetParent(parentTrans);
  958. mrTrans.gameObject.layer = m_CurrentGo.layer;
  959. ls.Add(renderer);
  960. }
  961. }
  962. if (needDestroy)
  963. {
  964. GameObject.DestroyImmediate(go);
  965. }
  966. return ls;
  967. }
  968. private SkinnedMeshRenderer BindSMR(SkinnedMeshRenderer smr)
  969. {
  970. smr.transform.SetParent(m_CurrentGo.transform);
  971. smr.transform.localPosition = Vector3.zero;
  972. smr.transform.localRotation = Quaternion.identity;
  973. smr.transform.localScale = Vector3.one;
  974. smr.gameObject.layer = m_CurrentGo.layer;
  975. var rootBone = smr.rootBone;
  976. if (rootBone)
  977. {
  978. if (m_CurrentGoBoneMap.ContainsKey(rootBone.name))
  979. {
  980. smr.rootBone = m_CurrentGoBoneMap[rootBone.name];
  981. }
  982. else
  983. {
  984. smr.rootBone = null;
  985. }
  986. }
  987. Transform[] bones = smr.bones;
  988. for (int i = 0, iMax = bones.Length; i < iMax; i++)
  989. {
  990. if (bones[i])
  991. {
  992. if (m_CurrentGoBoneMap.ContainsKey(bones[i].name))
  993. {
  994. bones[i] = m_CurrentGoBoneMap[bones[i].name];
  995. }
  996. else
  997. {
  998. bones[i] = null;
  999. }
  1000. }
  1001. }
  1002. smr.bones = bones;
  1003. return smr;
  1004. }
  1005. private void BuildBoneMap(Transform root)
  1006. {
  1007. if (!root) return;
  1008. if (m_CurrentGoBoneMap.ContainsKey(root.name))
  1009. {
  1010. return;
  1011. }
  1012. m_CurrentGoBoneMap.Add(root.name, root);
  1013. for (int i = 0, iMax = root.childCount; i < iMax; i++)
  1014. {
  1015. BuildBoneMap(root.GetChild(i));
  1016. }
  1017. }
  1018. #endregion
  1019. #region Preview
  1020. private void RefreshPreviewGo()
  1021. {
  1022. if (!m_CurrentGo) return;
  1023. Selection.activeObject = m_CurrentGo;
  1024. }
  1025. #endregion
  1026. }
  1027. }