MCC_Tool.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Threading;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.IO;
  8. namespace SoftKitty.MasterCharacterCreator
  9. {
  10. public class MCC_Tool : EditorWindow
  11. {
  12. private int _id = 0;
  13. private int _boneNameId = 0;
  14. private OutfitSlots _boneNameSlot;
  15. private Sex _boneNameSex;
  16. private CharacterDataSetting _dataScript;
  17. private bool _dataFold = false;
  18. private bool _outfitList = false;
  19. private bool _maleFold = false;
  20. private bool _femaleFold = false;
  21. private bool[] _maleSlotFold=new bool[1];
  22. private bool[] _femaleSlotFold = new bool[1];
  23. private Vector2 _scroll;
  24. private TextAsset ByteText;
  25. Color _titleColor = new Color(0.3F, 0.5F, 1F);
  26. Color _buttonColor = new Color(0F, 0.8F, 0.3F);
  27. private Transform FindAllChild(Transform _mother, string _name)
  28. {
  29. Transform result = null;
  30. Transform[] allMyChild = _mother.GetComponentsInChildren<Transform>(true);
  31. foreach (Transform obj in allMyChild)
  32. {
  33. if (obj.name == _name)
  34. {
  35. result = obj.transform;
  36. }
  37. }
  38. return result;
  39. }
  40. void GetDataScript()
  41. {
  42. GameObject _data = Resources.Load<GameObject>("MasterCharacterCreator/Core/CharacterData");
  43. _dataScript = _data.GetComponent<CharacterDataSetting>();
  44. }
  45. private void LoadSkinnedMesh(Transform _root, SkinnedMeshRenderer _renderer, string _meshPath, string _matPath, string[] boneNames)
  46. {
  47. Dictionary<string, Transform> BoneDictionary = new Dictionary<string, Transform>();
  48. BoneDictionary.Clear();
  49. foreach (Transform trans in _root.GetComponentsInChildren<Transform>())
  50. {
  51. if (!BoneDictionary.ContainsKey(trans.name))
  52. {
  53. BoneDictionary.Add(trans.name, trans);
  54. }
  55. }
  56. if (_meshPath.Length > 0 && _matPath.Length > 0)
  57. {
  58. _renderer.enabled = false;
  59. _renderer.sharedMesh = Resources.Load<Mesh>(_meshPath);
  60. _renderer.material = Resources.Load<Material>(_matPath);
  61. Transform[] bones = new Transform[boneNames.Length];
  62. for (int i = 0; i < bones.Length; i++)
  63. {
  64. bones[i] = BoneDictionary[boneNames[i]];
  65. }
  66. _renderer.bones = bones;
  67. _renderer.enabled = true;
  68. Bounds _bound = _renderer.localBounds;
  69. _bound.center = Vector3.zero;
  70. _bound.extents = new Vector3(0.8F, 1.8F, 0.8F);
  71. _renderer.localBounds = _bound;
  72. }
  73. else
  74. {
  75. _renderer.enabled = false;
  76. }
  77. }
  78. private void DevFunctions()
  79. {
  80. GUILayout.BeginHorizontal();
  81. if (GUILayout.Button("Add Bones", GUILayout.Width(150))) {
  82. List<Transform> _bones = new List<Transform>();
  83. foreach (Transform trans in Selection.activeGameObject.GetComponentsInChildren<Transform>())
  84. {
  85. if (trans.tag == "Bone")
  86. {
  87. _bones.Add(trans);
  88. }
  89. }
  90. Selection.activeGameObject.GetComponent<CharacterBoneControl>().Bones = _bones.ToArray();
  91. }
  92. GUILayout.EndHorizontal();
  93. GUILayout.BeginHorizontal();
  94. if (GUILayout.Button("Copy Bone Map", GUILayout.Width(150)))
  95. {
  96. SkinnedMeshRenderer _target = Selection.activeGameObject.GetComponent<SkinnedMeshRenderer>();
  97. Dictionary<string, Transform> Bones = new Dictionary<string, Transform>();
  98. foreach (var obj in Selection.activeGameObject.transform.parent.GetComponentsInChildren<Transform>())
  99. {
  100. if (!Bones.ContainsKey(obj.name)) Bones.Add(obj.name, obj);
  101. }
  102. Transform[] _newBones = new Transform[_target.bones.Length];
  103. for (int i = 0; i < _newBones.Length; i++)
  104. {
  105. if (Bones.ContainsKey(_target.bones[i].name))
  106. {
  107. _newBones[i] = Bones[_target.bones[i].name];
  108. }
  109. else
  110. {
  111. Debug.LogError("Could not find bone ["+_target.bones[i].name+"] !");
  112. return;
  113. }
  114. }
  115. _target.bones = _newBones;
  116. _target.rootBone = Bones[_target.rootBone.name];
  117. Debug.Log(Selection.activeGameObject.name+" > Bone map copied!");
  118. }
  119. GUILayout.EndHorizontal();
  120. EditorGUILayout.Separator();
  121. GUILayout.BeginHorizontal();
  122. DataObj = (CharacterDataSetting)EditorGUILayout.ObjectField(DataObj, typeof(CharacterDataSetting), true);
  123. GUILayout.EndHorizontal();
  124. GUILayout.BeginHorizontal();
  125. _boneNameSex = (Sex)EditorGUILayout.EnumPopup(_boneNameSex, GUILayout.Width(70));
  126. _boneNameSlot = (OutfitSlots)EditorGUILayout.EnumPopup(_boneNameSlot, GUILayout.Width(80));
  127. GUILayout.Label("ID:", GUILayout.Width(30));
  128. _boneNameId = EditorGUILayout.IntField(_boneNameId, GUILayout.Width(50));
  129. GUI.backgroundColor = Color.green;
  130. if (GUILayout.Button("Setup Bone Names"))
  131. {
  132. SkinnedMeshRenderer _renderer = Selection.activeGameObject.GetComponent<SkinnedMeshRenderer>();
  133. string[] _boneNames = new string[_renderer.bones.Length];
  134. for (int u = 0; u < _boneNames.Length; u++)
  135. {
  136. _boneNames[u] = _renderer.bones[u].name;
  137. }
  138. if (_boneNameSex == Sex.Male)
  139. {
  140. switch (_boneNameSlot)
  141. {
  142. case OutfitSlots.Armor:
  143. DataObj.MaleArmorSetting[_boneNameId].BoneNames = _boneNames;
  144. break;
  145. case OutfitSlots.Pants:
  146. DataObj.MalePantsSetting[_boneNameId].BoneNames = _boneNames;
  147. break;
  148. case OutfitSlots.Gauntlet:
  149. DataObj.MaleGloveSetting[_boneNameId].BoneNames = _boneNames;
  150. break;
  151. case OutfitSlots.Boot:
  152. DataObj.MaleBootSetting[_boneNameId].BoneNames = _boneNames;
  153. break;
  154. }
  155. }
  156. else
  157. {
  158. switch (_boneNameSlot)
  159. {
  160. case OutfitSlots.Armor:
  161. DataObj.FemaleArmorSetting[_boneNameId].BoneNames = _boneNames;
  162. break;
  163. case OutfitSlots.Pants:
  164. DataObj.FemalePantsSetting[_boneNameId].BoneNames = _boneNames;
  165. break;
  166. case OutfitSlots.Gauntlet:
  167. DataObj.FemaleGloveSetting[_boneNameId].BoneNames = _boneNames;
  168. break;
  169. case OutfitSlots.Boot:
  170. DataObj.FemaleBootSetting[_boneNameId].BoneNames = _boneNames;
  171. break;
  172. }
  173. }
  174. }
  175. GUI.backgroundColor = Color.white;
  176. GUILayout.EndHorizontal();
  177. EditorGUILayout.Separator();
  178. }
  179. CharacterDataSetting DataObj;
  180. void OnGUI()
  181. {
  182. GUIStyle box = GUI.skin.button;
  183. GUIStyle header = new GUIStyle();
  184. header.fontSize = 20;
  185. header.alignment = TextAnchor.MiddleCenter;
  186. if (_dataScript == null)GetDataScript();
  187. if (_maleSlotFold.Length != System.Enum.GetValues(typeof(OutfitSlots)).Length)
  188. {
  189. _maleSlotFold = new bool[System.Enum.GetValues(typeof(OutfitSlots)).Length];
  190. for (int i=0;i<_maleSlotFold.Length;i++) _maleSlotFold[i] = false;
  191. }
  192. if (_femaleSlotFold.Length != System.Enum.GetValues(typeof(OutfitSlots)).Length)
  193. {
  194. _femaleSlotFold = new bool[System.Enum.GetValues(typeof(OutfitSlots)).Length];
  195. for (int i = 0; i < _femaleSlotFold.Length; i++) _femaleSlotFold[i] = false;
  196. }
  197. _scroll=GUILayout.BeginScrollView(_scroll);
  198. //DevFunctions(); //For package developer
  199. //GUILayout.BeginHorizontal();
  200. //if (GUILayout.Button("Assign Body Normal"))
  201. //{
  202. // string[] guids = AssetDatabase.FindAssets("t:Material", new[] { "Assets/SoftKitty/MasterCharacterCreator" });
  203. // foreach (string guid2 in guids)
  204. // {
  205. // string _path = AssetDatabase.GUIDToAssetPath(guid2);
  206. // Material _mat = (Material)AssetDatabase.LoadAssetAtPath(_path, typeof(Material));
  207. // if (_mat.shader.name.Contains("CharacterSkin"))
  208. // {
  209. // if (_mat.GetTexture("_NormalMap") != null && _mat.GetTexture("_BodyNormal") == null)
  210. // {
  211. // Debug.Log(_path);
  212. // _mat.SetTexture("_BodyNormal", _mat.GetTexture("_NormalMap"));
  213. // }
  214. // }
  215. // }
  216. //}
  217. //GUILayout.EndHorizontal();
  218. GUILayout.BeginHorizontal();
  219. GUI.backgroundColor = _titleColor;
  220. if (GUILayout.Button("Select Character Data Setting Prefab"))
  221. {
  222. Selection.activeObject = _dataScript.gameObject;
  223. }
  224. GUI.backgroundColor = Color.white;
  225. GUILayout.EndHorizontal();
  226. EditorGUILayout.Separator();
  227. GUILayout.BeginHorizontal();
  228. _outfitList = EditorGUILayout.Foldout(_outfitList, "[Outfits List]");
  229. GUILayout.EndHorizontal();
  230. if (_outfitList) {
  231. GUILayout.BeginHorizontal();
  232. GUILayout.Space(20);
  233. _maleFold = EditorGUILayout.Foldout(_maleFold, "Male");
  234. GUILayout.EndHorizontal();
  235. if (_maleFold) {
  236. for (int i = 0; i < _maleSlotFold.Length; i++) {
  237. GUILayout.BeginHorizontal();
  238. GUILayout.Space(40);
  239. _maleSlotFold[i] = EditorGUILayout.Foldout(_maleSlotFold[i], ((OutfitSlots)i).ToString());
  240. GUILayout.EndHorizontal();
  241. if (_maleSlotFold[i]) {
  242. OutfitInfo[] _infos = _dataScript.GetOutfitSettings(Sex.Male, (OutfitSlots)i);
  243. for (int u = 0; u < _infos.Length; u++)
  244. {
  245. OutfitInfo _info=_infos[u];
  246. GUILayout.BeginHorizontal();
  247. GUILayout.Space(60);
  248. GUILayout.Box(_info.Icon,new GUILayoutOption[2] { GUILayout.Width(32), GUILayout.Height(32) });
  249. GUILayout.Label(_info.DisplayName, new GUILayoutOption[2] { GUILayout.Width(150), GUILayout.Height(32) });
  250. GUI.backgroundColor = _buttonColor;
  251. if (GUILayout.Button("Preview", new GUILayoutOption[2] { GUILayout.Width(70), GUILayout.Height(32) })) {
  252. if (MeshPreview.instance != null) MeshPreview.instance.Close();
  253. if (i == 5 || i == 6)
  254. {
  255. MeshPreview.ShowPreview("", "", "Assets/SoftKitty/MasterCharacterCreator/Resources/" + _info.MeshPath+ ".prefab");
  256. }
  257. else
  258. {
  259. string _path = AssetDatabase.GetAssetPath(_dataScript.gameObject).Replace("CharacterData", "PreviewModel");
  260. MeshPreview.ShowPreview(_info.MeshPath, _info.MaterialPath, _path);
  261. }
  262. }
  263. GUI.backgroundColor = Color.white;
  264. GUILayout.EndHorizontal();
  265. }
  266. }
  267. }
  268. }
  269. GUILayout.BeginHorizontal();
  270. GUILayout.Space(20);
  271. _femaleFold = EditorGUILayout.Foldout(_femaleFold, "Female");
  272. GUILayout.EndHorizontal();
  273. if (_femaleFold)
  274. {
  275. for (int i = 0; i < _femaleSlotFold.Length; i++)
  276. {
  277. GUILayout.BeginHorizontal();
  278. GUILayout.Space(40);
  279. _femaleSlotFold[i] = EditorGUILayout.Foldout(_femaleSlotFold[i], ((OutfitSlots)i).ToString());
  280. GUILayout.EndHorizontal();
  281. if (_femaleSlotFold[i])
  282. {
  283. OutfitInfo[] _infos = _dataScript.GetOutfitSettings(Sex.Female, (OutfitSlots)i);
  284. for (int u = 0; u < _infos.Length; u++)
  285. {
  286. OutfitInfo _info = _infos[u];
  287. GUILayout.BeginHorizontal();
  288. GUILayout.Space(60);
  289. GUILayout.Box(_info.Icon, new GUILayoutOption[2] { GUILayout.Width(32), GUILayout.Height(32) });
  290. GUILayout.Label(_info.DisplayName, new GUILayoutOption[2] { GUILayout.Width(150), GUILayout.Height(32) });
  291. GUI.backgroundColor = _buttonColor;
  292. if (GUILayout.Button("Preview", new GUILayoutOption[2] { GUILayout.Width(70), GUILayout.Height(32) }))
  293. {
  294. if (MeshPreview.instance != null) MeshPreview.instance.Close();
  295. if (i == 5 || i == 6)
  296. {
  297. MeshPreview.ShowPreview("", "", "Assets/SoftKitty/MasterCharacterCreator/Resources/" + _info.MeshPath + ".prefab");
  298. }
  299. else
  300. {
  301. string _path = AssetDatabase.GetAssetPath(_dataScript.gameObject).Replace("CharacterData", "PreviewModel");
  302. MeshPreview.ShowPreview(_info.MeshPath, _info.MaterialPath, _path);
  303. }
  304. }
  305. GUI.backgroundColor = Color.white;
  306. GUILayout.EndHorizontal();
  307. }
  308. }
  309. }
  310. }
  311. }
  312. EditorGUILayout.Separator();
  313. GUILayout.BeginHorizontal();
  314. _dataFold = EditorGUILayout.Foldout(_dataFold, "[v1.5+ Save Data Tool]");
  315. GUILayout.EndHorizontal();
  316. if (_dataFold)
  317. {
  318. GUILayout.BeginHorizontal();
  319. GUILayout.Space(20);
  320. GUILayout.Label("[Convert old data to v1.5+]", GUILayout.Width(200));
  321. GUILayout.EndHorizontal();
  322. GUILayout.BeginHorizontal();
  323. GUILayout.Space(20);
  324. GUILayout.Label("Select the old save:", GUILayout.Width(120));
  325. ByteText = (TextAsset)EditorGUILayout.ObjectField(ByteText, typeof(TextAsset), false, GUILayout.Width(150));
  326. GUILayout.EndHorizontal();
  327. GUILayout.BeginHorizontal();
  328. GUILayout.Space(20);
  329. GUI.backgroundColor = ByteText!=null? new Color(0.2F, 0.5F, 1F, 1F):Color.gray;
  330. if (GUILayout.Button("Convert Save Data from v1.0~v1.2", GUILayout.Width(270)))
  331. {
  332. try
  333. {
  334. string _path = AssetDatabase.GetAssetPath(ByteText);
  335. _path = _path.Substring(6, _path.Length - 6);
  336. BlurPrintType _type = (BlurPrintType)ByteText.bytes[0];
  337. CharacterAppearance _data = new CharacterAppearance(ByteText.bytes, CharacterAppearance.DataVersions.Before_v1_3);
  338. File.WriteAllBytes(Application.dataPath + _path, _data.ToBytes(_type));
  339. AssetDatabase.Refresh();
  340. EditorUtility.DisplayDialog("Save Data","Successfully converted the save data.","OK");
  341. }
  342. catch
  343. {
  344. EditorUtility.DisplayDialog("Save Data", "Invalid save data", "OK");
  345. }
  346. }
  347. GUI.backgroundColor = Color.white;
  348. GUILayout.EndHorizontal();
  349. GUILayout.BeginHorizontal();
  350. GUILayout.Space(20);
  351. GUI.backgroundColor = ByteText != null ? new Color(0.2F, 0.5F, 1F, 1F) : Color.gray;
  352. if (GUILayout.Button("Convert Save Data from v1.3~v.1.4", GUILayout.Width(270)))
  353. {
  354. try
  355. {
  356. string _path = AssetDatabase.GetAssetPath(ByteText);
  357. _path = _path.Substring(6, _path.Length - 6);
  358. BlurPrintType _type = (BlurPrintType)ByteText.bytes[0];
  359. CharacterAppearance _data = new CharacterAppearance(ByteText.bytes, CharacterAppearance.DataVersions.Before_v1_5);
  360. File.WriteAllBytes(Application.dataPath + _path, _data.ToBytes(_type));
  361. AssetDatabase.Refresh();
  362. EditorUtility.DisplayDialog("Save Data", "Successfully converted the save data.", "OK");
  363. }
  364. catch
  365. {
  366. EditorUtility.DisplayDialog("Save Data", "Invalid save data", "OK");
  367. }
  368. }
  369. GUI.backgroundColor = Color.white;
  370. GUILayout.EndHorizontal();
  371. GUILayout.BeginHorizontal();
  372. GUILayout.Space(20);
  373. EditorGUILayout.HelpBox("DO NOT use this for save data from v1.3+", MessageType.Warning);
  374. GUILayout.EndHorizontal();
  375. }
  376. GUILayout.EndScrollView();
  377. }
  378. }
  379. }