FaceBuildSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using TMPro;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.U2D;
  9. using UnityEngine.UI;
  10. using YooAsset;
  11. using static UnityEngine.EventSystems.EventTrigger;
  12. public class FaceBuildSystem : MonoBehaviour
  13. {
  14. private ReferenceCollector re;
  15. private Button EFinish;
  16. private RectTransform EModel;
  17. private RectTransform EGShowClothName;
  18. private RectTransform ESNickName;
  19. private GameObject EColorText;
  20. private ScrollRect EColor;
  21. private ScrollRect EG_TypeFilter;
  22. private ScrollRect EBagContent;
  23. private Toggle Edress;
  24. private Toggle Ebody;
  25. private GameObject EItem_TypeFilter;
  26. private RectTransform EGTypeContent;
  27. private RectTransform EGColorItem;
  28. private RectTransform EGColorFilter;
  29. private TextMeshProUGUI EClothName;
  30. private SpriteAtlas ColorStyle;
  31. private SpriteAtlas ItemIcon;
  32. /// <summary>所有的物品</summary>
  33. public Dictionary<int, ItemPackage_View> itemPackage = new Dictionary<int, ItemPackage_View>();
  34. /// <summary>所有的物品信息</summary>
  35. public Dictionary<int, Good> standardPackageItems =new Dictionary<int, Good>();
  36. /// <summary>物品种类</summary>
  37. public Dictionary<int, List<GameObject>> TypeItemClassGroup = new Dictionary<int, List<GameObject>>();
  38. /// <summary>空格子</summary>
  39. public List<GameObject> Cells = new List<GameObject>();
  40. /// <summary>取消穿戴的装备</summary>
  41. public List<int> Unworn = new List<int>();
  42. public List<string> ColorList = new List<string>();
  43. public List<GameObject> ColorObjList = new List<GameObject>();
  44. public int ColorType;
  45. public int BodyType;
  46. public int ClothType;
  47. public Dictionary<int, List<Good>> ItemTypeData = new Dictionary<int, List<Good>>();
  48. public List<Good> ImageGoods = new List<Good>();
  49. public Dictionary<int, GameObject> ClassType = new Dictionary<int, GameObject>();
  50. private GameObject itempackage;
  51. private async void Start()
  52. {
  53. re = GetComponent<ReferenceCollector>();
  54. EFinish = re.Get<GameObject>("EFinish").GetComponent<Button>();
  55. EModel = re.Get<GameObject>("EModel").GetComponent<RectTransform>();
  56. EGShowClothName = re.Get<GameObject>("EGShowClothName").GetComponent<RectTransform>();
  57. ESNickName = re.Get<GameObject>("ESNickName").GetComponent<RectTransform>();
  58. EColorText = re.Get<GameObject>("EColorText");
  59. EColor = re.Get<GameObject>("EColor").GetComponent<ScrollRect>();
  60. EG_TypeFilter = re.Get<GameObject>("EG_TypeFilter").GetComponent<ScrollRect>();
  61. EBagContent = re.Get<GameObject>("EBagContent").GetComponent<ScrollRect>();
  62. Edress = re.Get<GameObject>("Edress").GetComponent<Toggle>();
  63. Ebody = re.Get<GameObject>("Ebody").GetComponent<Toggle>();
  64. EItem_TypeFilter = re.Get<GameObject>("EItem_TypeFilter");
  65. EGTypeContent = re.Get<GameObject>("EGTypeContent").GetComponent<RectTransform>();
  66. EGColorItem = re.Get<GameObject>("EGColorItem").GetComponent<RectTransform>();
  67. EGColorFilter = re.Get<GameObject>("EGColorFilter").GetComponent<RectTransform>();
  68. EClothName = re.Get<GameObject>("EClothName").GetComponent<TextMeshProUGUI>();
  69. EModel.GetComponent<RawImage>().texture = GameObject.Find("UICamera").GetComponent<Camera>().targetTexture;
  70. ColorStyle =await YooAssetManager.Instance.LoadAsset<SpriteAtlas>("colorItems");
  71. ItemIcon = await YooAssetManager.Instance.LoadAsset<SpriteAtlas>("ItemIcon");
  72. itempackage = await YooAssetManager.Instance.LoadAsset<GameObject>("Item_Package");
  73. List<Good> goods = JsonMapper.ToObject<List<Good>>(Resources.Load<TextAsset>("goodsData").text);
  74. for (int i = 0; i < goods.Count; i++)
  75. {
  76. goods[i].assetsType = goods[i].dressType;
  77. goods[i].dressType = (goods[i].id / 10000).ToString();
  78. }
  79. for (int i = 0; i < goods.Count; i++)
  80. {
  81. //if (goods[i].type == 1)
  82. //{
  83. //}
  84. ImageGoods.Add(goods[i]);
  85. }
  86. ItemTypeData = new Dictionary<int, List<Good>>();
  87. for (int i = 0; i < 17; i++)
  88. {
  89. TypeItemClassGroup.Add(i + 1, new List<GameObject>());
  90. }
  91. for (int i = 0; i < ImageGoods.Count; i++)
  92. {
  93. int type = ImageGoods[i].id / 10000;
  94. if (!ItemTypeData.ContainsKey(type))
  95. {
  96. ItemTypeData.Add(type, new List<Good>());
  97. }
  98. ItemTypeData[type].Add(ImageGoods[i]);
  99. if (!TypeItemClassGroup.ContainsKey(type))
  100. {
  101. TypeItemClassGroup.Add(type, new List<GameObject>());
  102. }
  103. }
  104. //foreach (var item in ItemTypeData)
  105. // {
  106. // Debug.Log(item.Key + "************" + item.Value.Count);
  107. // }
  108. AddCreatCell();
  109. CreatTypeItem();
  110. CreatColorItem();
  111. Init();
  112. Ebody.isOn = true;
  113. }
  114. private void Init()
  115. {
  116. //身体
  117. Ebody.onValueChanged.AddListener((x) =>
  118. {
  119. foreach (var item in ClassType)
  120. {
  121. if (item.Key < 10)
  122. {
  123. item.Value.SetActive(x);
  124. }
  125. }
  126. if (x)
  127. {
  128. Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = Color.black;
  129. Filter(true);
  130. ClassType[1].GetComponent<Toggle>().isOn = true;
  131. }
  132. else
  133. {
  134. Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
  135. }
  136. });
  137. //服饰
  138. Edress.onValueChanged.AddListener((x) =>
  139. {
  140. foreach (var item in ClassType)
  141. {
  142. if (item.Key > 9)
  143. {
  144. item.Value.SetActive(x);
  145. }
  146. }
  147. if (x)
  148. {
  149. Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = Color.black;
  150. Filter(true);
  151. ClassType[10].GetComponent<Toggle>().isOn = true;
  152. }
  153. else
  154. {
  155. Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
  156. }
  157. });
  158. }
  159. private void AddCreatCell()
  160. {
  161. int count = 15;
  162. GameObject obj = itempackage;
  163. for (int i = 0; i < count; i++)
  164. {
  165. GameObject go = GameObject.Instantiate(obj);
  166. Cells.Add(go);
  167. go.transform.SetParent(EBagContent.content, false);
  168. go.transform.SetAsLastSibling();
  169. go.GetComponent<Toggle>().enabled = false;
  170. go.SetActive(false);
  171. for (int j = 0; j < go.transform.childCount; j++)
  172. {
  173. go.transform.GetChild(j).gameObject.SetActive(false);
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// 创建身体部位类别
  179. /// </summary>
  180. /// <param name="self"></param>
  181. private void CreatTypeItem()
  182. {
  183. var classdir = JsonMapper.ToObject<Dictionary<string,ItemClassConfig>>(Resources.Load<TextAsset>("ItemClass").text);
  184. EItem_TypeFilter.gameObject.SetActive(false);
  185. Toggle toggle = null;
  186. foreach (var item in classdir)
  187. {
  188. GameObject go = GameObject.Instantiate(EItem_TypeFilter.gameObject);
  189. go.SetActive(true);
  190. go.GetComponent<Toggle>().group = EGTypeContent.GetComponent<ToggleGroup>();
  191. go.transform.SetParent(EGTypeContent.transform);
  192. go.transform.localPosition = Vector3.zero;
  193. go.transform.localScale = Vector3.one;
  194. var text = go.transform.Find("Image/Text").GetComponent<TextMeshProUGUI>();
  195. text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
  196. LanguageMatchManager.Instance.changeLanguage += () =>
  197. {
  198. text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
  199. };
  200. go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
  201. {
  202. if (x)
  203. {
  204. text.color = Color.black;
  205. if (item.Value.type == 1)
  206. {
  207. ClothType = item.Value.Id;
  208. }
  209. else
  210. {
  211. BodyType = item.Value.Id;
  212. }
  213. Filter(true, item.Value.Id);
  214. }
  215. else
  216. {
  217. text.color = new Color(1, 1, 1, 0.4f);
  218. }
  219. });
  220. if (item.Value.Id == 1)
  221. {
  222. go.GetComponent<Toggle>().isOn = true;
  223. }
  224. ClassType.Add(item.Value.Id, go);
  225. }
  226. }
  227. /// <summary>
  228. /// 创建颜色种类
  229. /// </summary>
  230. /// <param name="self"></param>
  231. private void CreatColorItem()
  232. {
  233. for (int i = 0; i <ColorObjList.Count; i++)
  234. {
  235. GameObject.Destroy(ColorObjList[i]);
  236. }
  237. ColorObjList.Clear();
  238. ColorList = ColorList.Distinct().ToList();
  239. if (ColorList.Count != 0)
  240. {
  241. ColorType = int.Parse(ColorList[0]);
  242. }
  243. var colordir = JsonMapper.ToObject<Dictionary<string, ItemColorConfig>>(Resources.Load<TextAsset>("ItemColor").text);
  244. EGColorItem.gameObject.SetActive(false);
  245. foreach (var item in colordir)
  246. {
  247. string color =ColorList.Find(x => x == item.Value.Id.ToString());
  248. if (string.IsNullOrEmpty(color)) continue;
  249. GameObject go = GameObject.Instantiate(EGColorItem.gameObject);
  250. go.SetActive(true);
  251. go.GetComponent<Toggle>().group =EGColorFilter.GetComponent<ToggleGroup>();
  252. go.transform.SetParent(EGColorFilter.transform);
  253. go.transform.localPosition = Vector3.zero;
  254. go.transform.localScale = Vector3.one;
  255. go.transform.GetChild(0).GetComponent<Image>().sprite =ColorStyle.GetSprite(item.Value.assetsName);
  256. go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
  257. {
  258. if (x)
  259. {
  260. ColorType = item.Value.Id;
  261. Filter();
  262. }
  263. else
  264. {
  265. }
  266. });
  267. ColorObjList.Add(go);
  268. }
  269. }
  270. private void Filter(bool isTypeSelect = false, int type = -1)
  271. {
  272. int count = 0;
  273. ColorList.Clear();
  274. Dictionary<GameObject, Good> showObj = new Dictionary<GameObject, Good>();
  275. if (type != -1)
  276. {
  277. if (TypeItemClassGroup[type].Count == 0 &&ItemTypeData.ContainsKey(type))
  278. {
  279. AddUIScrollItems(itemPackage, ItemTypeData[type].Count);
  280. GameObject obj = itempackage;
  281. for (int i = 0; i < ItemTypeData[type].Count; i++)
  282. {
  283. GameObject go = GameObject.Instantiate(obj);
  284. RefreshBagItem(go.transform, type, i);
  285. go.transform.SetParent(EBagContent.content);
  286. go.transform.localScale = Vector3.one;
  287. go.transform.localPosition = Vector3.zero;
  288. go.transform.SetAsFirstSibling();
  289. go.name = ItemTypeData[type][i].id.ToString();
  290. }
  291. }
  292. }
  293. int typeUsed = 0;
  294. if (Ebody.isOn)
  295. {
  296. typeUsed =BodyType;
  297. }
  298. else
  299. {
  300. typeUsed = ClothType;
  301. }
  302. Good dressItem = null;
  303. foreach (var item in TypeItemClassGroup)
  304. {
  305. var list = item.Value;
  306. for (int i = 0; i < list.Count; i++)
  307. {
  308. Good data = ItemTypeData[item.Key][i];
  309. ColorList.Add(data.color);
  310. showObj.Add(list[i], data);
  311. if (item.Key == typeUsed)
  312. {
  313. list[i].SetActive(true);
  314. count++;
  315. //todo 穿戴
  316. //if (UserData.Instance.TempWornPackageItems != null && UserData.Instance.TempWornPackageItems.Count != 0)
  317. //{
  318. // dressItem = UserData.Instance.TempWornPackageItems.Find(x => x.id == data.id);
  319. //}
  320. //else
  321. //{
  322. // dressItem = UserData.Instance.WornPackageItems.Find(x => x.id == data.id);
  323. //}
  324. }
  325. else
  326. {
  327. list[i].SetActive(false);
  328. ColorList.Remove(data.color);
  329. showObj.Remove(list[i]);
  330. }
  331. list[i].GetComponent<Toggle>().SetIsOnWithoutNotify(false);
  332. list[i].GetComponent<Toggle>().interactable = true;
  333. }
  334. }
  335. if (isTypeSelect)
  336. {
  337. if (typeUsed == 2)
  338. {
  339. CreatColorItem();
  340. }
  341. else
  342. {
  343. ColorType = 0;
  344. }
  345. EColor.gameObject.SetActive(typeUsed == 2);
  346. EColorText.gameObject.SetActive(typeUsed == 2);
  347. }
  348. foreach (var item in showObj)
  349. {
  350. if (int.Parse(item.Value.color) !=ColorType && ColorType != 0)
  351. {
  352. item.Key.SetActive(false);
  353. count--;
  354. }
  355. }
  356. SetCellShow(count, Cells);
  357. }
  358. /// <summary>
  359. /// 显示多少个格子
  360. /// </summary>
  361. /// <param name="self"></param>
  362. /// <param name="count">隐藏的数量</param>
  363. private void SetCellShow(int count, List<GameObject> cells)
  364. {
  365. int show = count;
  366. if (show > cells.Count)
  367. {
  368. show = show % 5;
  369. show = 5 - show;
  370. show = cells.Count - show;
  371. }
  372. for (int i = 0; i < cells.Count; i++)
  373. {
  374. if (i < show)
  375. {
  376. cells[i].gameObject.SetActive(false);
  377. }
  378. else
  379. {
  380. cells[i].gameObject.SetActive(true);
  381. }
  382. }
  383. }
  384. private void RefreshBagItem(Transform t, int type, int id)
  385. {
  386. itemPackage[id].BindTrans(t);
  387. Good config = ItemTypeData[type][id];
  388. //TODO:物品多语言;
  389. //itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
  390. //LanguageMatchManager.Instance.changeLanguage += () =>
  391. //{
  392. // itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
  393. //};
  394. //itemPackage[id].ENameTextMeshProUGUI.text = config.name;
  395. itemPackage[id].EIconImage.sprite =ItemIcon.GetSprite(config.id.ToString());
  396. itemPackage[id].uiTransform.SetAsFirstSibling();
  397. //设置穿戴功能 CharacterRender.addDress();
  398. t.GetComponent<Toggle>().group =EBagContent.content.GetComponent<ToggleGroup>();
  399. t.GetComponent<Toggle>().onValueChanged.AddListener(async (b) =>
  400. {
  401. //todo 装备
  402. if (b)
  403. {
  404. await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
  405. ShowEquipName();
  406. }
  407. else
  408. {
  409. //if (!self.Unworn.Contains(config.id))
  410. //{
  411. // self.Unworn.Add(config.id);
  412. //}
  413. //CharacterManager.Inst.UnEquip(CharacterManager.Inst.selfRender, config, UserData.Instance.WornPackageItems);
  414. }
  415. });
  416. if (config.level == 1)
  417. {
  418. itemPackage[id].ENameColorImage.color = new Color(0.8f, 0.8f, 0.8f, 1);
  419. }
  420. if (config.level == 2)
  421. {
  422. itemPackage[id].ENameColorImage.color = new Color(0.82f, 0.98f, 0, 1);
  423. }
  424. if (config.level == 3)
  425. {
  426. itemPackage[id].ENameColorImage.color = new Color(0.63f, 0.82f, 1, 1);
  427. }
  428. if (config.level == 4)
  429. {
  430. itemPackage[id].ENameColorImage.color = new Color(0.392f, 0, 1, 1);
  431. itemPackage[id].ENameTextMeshProUGUI.color = new Color(0.82f, 0.98f, 0, 1);
  432. itemPackage[id].ELinkIconImage.gameObject.SetActive(true);
  433. }
  434. TypeItemClassGroup[type].Add(t.gameObject);
  435. }
  436. public void AddUIScrollItems(Dictionary<int , ItemPackage_View> dictionary, int count)
  437. {
  438. if (dictionary == null)
  439. {
  440. dictionary = new Dictionary<int, ItemPackage_View>();
  441. }
  442. if (count <= 0)
  443. {
  444. return;
  445. }
  446. if (count > dictionary.Count)
  447. {
  448. for (int i = dictionary.Count; i < count; i++)
  449. {
  450. ItemPackage_View itemServer = new ItemPackage_View();
  451. dictionary.Add(i, itemServer);
  452. }
  453. }
  454. else if (count < dictionary.Count)
  455. {
  456. for (int i = (dictionary.Count - 1); i >= count; i--)
  457. {
  458. dictionary[i].Dispose();
  459. dictionary.Remove(i);
  460. }
  461. }
  462. }
  463. /// <summary>显示装备名</summary>
  464. public void ShowEquipName()
  465. {
  466. List<Good> goods =new List<Good>();
  467. foreach (var item in CharacterManager.Inst.selfRender.dressRenders)
  468. {
  469. goods.Add(item.Value.good);
  470. }
  471. string text = null;
  472. for (int i = 0; i < goods.Count; i++)
  473. {
  474. if (i < 5)
  475. {
  476. text += $"-{goods[i].name}\r\n";
  477. }
  478. }
  479. if (goods.Count > 5)
  480. {
  481. text += "-and_more";
  482. }
  483. EClothName.text = text;
  484. }
  485. }