FaceBuildSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. //Good config = ItemTypeData[10][6];
  114. //Good config2= ItemTypeData[11][2];
  115. //await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
  116. //await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config2);
  117. //ShowEquipName();
  118. }
  119. private void Init()
  120. {
  121. //身体
  122. Ebody.onValueChanged.AddListener((x) =>
  123. {
  124. foreach (var item in ClassType)
  125. {
  126. if (item.Key < 10)
  127. {
  128. item.Value.SetActive(x);
  129. }
  130. }
  131. if (x)
  132. {
  133. Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = Color.black;
  134. Filter(true);
  135. ClassType[1].GetComponent<Toggle>().isOn = true;
  136. }
  137. else
  138. {
  139. Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
  140. }
  141. });
  142. //服饰
  143. Edress.onValueChanged.AddListener((x) =>
  144. {
  145. foreach (var item in ClassType)
  146. {
  147. if (item.Key > 9)
  148. {
  149. item.Value.SetActive(x);
  150. }
  151. }
  152. if (x)
  153. {
  154. Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = Color.black;
  155. Filter(true);
  156. ClassType[10].GetComponent<Toggle>().isOn = true;
  157. }
  158. else
  159. {
  160. Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
  161. }
  162. });
  163. }
  164. private void AddCreatCell()
  165. {
  166. int count = 15;
  167. GameObject obj = itempackage;
  168. for (int i = 0; i < count; i++)
  169. {
  170. GameObject go = GameObject.Instantiate(obj);
  171. Cells.Add(go);
  172. go.transform.SetParent(EBagContent.content, false);
  173. go.transform.SetAsLastSibling();
  174. go.GetComponent<Toggle>().enabled = false;
  175. go.SetActive(false);
  176. for (int j = 0; j < go.transform.childCount; j++)
  177. {
  178. go.transform.GetChild(j).gameObject.SetActive(false);
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// 创建身体部位类别
  184. /// </summary>
  185. /// <param name="self"></param>
  186. private void CreatTypeItem()
  187. {
  188. var classdir = JsonMapper.ToObject<Dictionary<string,ItemClassConfig>>(Resources.Load<TextAsset>("ItemClass").text);
  189. EItem_TypeFilter.gameObject.SetActive(false);
  190. Toggle toggle = null;
  191. foreach (var item in classdir)
  192. {
  193. GameObject go = GameObject.Instantiate(EItem_TypeFilter.gameObject);
  194. go.SetActive(true);
  195. go.GetComponent<Toggle>().group = EGTypeContent.GetComponent<ToggleGroup>();
  196. go.transform.SetParent(EGTypeContent.transform);
  197. go.transform.localPosition = Vector3.zero;
  198. go.transform.localScale = Vector3.one;
  199. var text = go.transform.Find("Image/Text").GetComponent<TextMeshProUGUI>();
  200. text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
  201. LanguageMatchManager.Instance.changeLanguage += () =>
  202. {
  203. text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
  204. };
  205. go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
  206. {
  207. if (x)
  208. {
  209. text.color = Color.black;
  210. if (item.Value.type == 1)
  211. {
  212. ClothType = item.Value.Id;
  213. }
  214. else
  215. {
  216. BodyType = item.Value.Id;
  217. }
  218. Filter(true, item.Value.Id);
  219. }
  220. else
  221. {
  222. text.color = new Color(1, 1, 1, 0.4f);
  223. }
  224. });
  225. if (item.Value.Id == 1)
  226. {
  227. go.GetComponent<Toggle>().isOn = true;
  228. }
  229. ClassType.Add(item.Value.Id, go);
  230. }
  231. }
  232. /// <summary>
  233. /// 创建颜色种类
  234. /// </summary>
  235. /// <param name="self"></param>
  236. private void CreatColorItem()
  237. {
  238. for (int i = 0; i <ColorObjList.Count; i++)
  239. {
  240. GameObject.Destroy(ColorObjList[i]);
  241. }
  242. ColorObjList.Clear();
  243. ColorList = ColorList.Distinct().ToList();
  244. if (ColorList.Count != 0)
  245. {
  246. ColorType = int.Parse(ColorList[0]);
  247. }
  248. var colordir = JsonMapper.ToObject<Dictionary<string, ItemColorConfig>>(Resources.Load<TextAsset>("ItemColor").text);
  249. EGColorItem.gameObject.SetActive(false);
  250. foreach (var item in colordir)
  251. {
  252. string color =ColorList.Find(x => x == item.Value.Id.ToString());
  253. if (string.IsNullOrEmpty(color)) continue;
  254. GameObject go = GameObject.Instantiate(EGColorItem.gameObject);
  255. go.SetActive(true);
  256. go.GetComponent<Toggle>().group =EGColorFilter.GetComponent<ToggleGroup>();
  257. go.transform.SetParent(EGColorFilter.transform);
  258. go.transform.localPosition = Vector3.zero;
  259. go.transform.localScale = Vector3.one;
  260. go.transform.GetChild(0).GetComponent<Image>().sprite =ColorStyle.GetSprite(item.Value.assetsName);
  261. go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
  262. {
  263. if (x)
  264. {
  265. ColorType = item.Value.Id;
  266. Filter();
  267. }
  268. else
  269. {
  270. }
  271. });
  272. ColorObjList.Add(go);
  273. }
  274. }
  275. private void Filter(bool isTypeSelect = false, int type = -1)
  276. {
  277. int count = 0;
  278. ColorList.Clear();
  279. Dictionary<GameObject, Good> showObj = new Dictionary<GameObject, Good>();
  280. if (type != -1)
  281. {
  282. if (TypeItemClassGroup[type].Count == 0 &&ItemTypeData.ContainsKey(type))
  283. {
  284. AddUIScrollItems(itemPackage, ItemTypeData[type].Count);
  285. GameObject obj = itempackage;
  286. for (int i = 0; i < ItemTypeData[type].Count; i++)
  287. {
  288. GameObject go = GameObject.Instantiate(obj);
  289. RefreshBagItem(go.transform, type, i);
  290. go.transform.SetParent(EBagContent.content);
  291. go.transform.localScale = Vector3.one;
  292. go.transform.localPosition = Vector3.zero;
  293. go.transform.SetAsFirstSibling();
  294. go.name = ItemTypeData[type][i].id.ToString();
  295. }
  296. }
  297. }
  298. int typeUsed = 0;
  299. if (Ebody.isOn)
  300. {
  301. typeUsed =BodyType;
  302. }
  303. else
  304. {
  305. typeUsed = ClothType;
  306. }
  307. Good dressItem = null;
  308. foreach (var item in TypeItemClassGroup)
  309. {
  310. var list = item.Value;
  311. for (int i = 0; i < list.Count; i++)
  312. {
  313. Good data = ItemTypeData[item.Key][i];
  314. ColorList.Add(data.color);
  315. showObj.Add(list[i], data);
  316. if (item.Key == typeUsed)
  317. {
  318. list[i].SetActive(true);
  319. count++;
  320. //todo 穿戴
  321. //if (UserData.Instance.TempWornPackageItems != null && UserData.Instance.TempWornPackageItems.Count != 0)
  322. //{
  323. // dressItem = UserData.Instance.TempWornPackageItems.Find(x => x.id == data.id);
  324. //}
  325. //else
  326. //{
  327. // dressItem = UserData.Instance.WornPackageItems.Find(x => x.id == data.id);
  328. //}
  329. }
  330. else
  331. {
  332. list[i].SetActive(false);
  333. ColorList.Remove(data.color);
  334. showObj.Remove(list[i]);
  335. }
  336. list[i].GetComponent<Toggle>().SetIsOnWithoutNotify(false);
  337. list[i].GetComponent<Toggle>().interactable = true;
  338. }
  339. }
  340. if (isTypeSelect)
  341. {
  342. if (typeUsed == 2)
  343. {
  344. CreatColorItem();
  345. }
  346. else
  347. {
  348. ColorType = 0;
  349. }
  350. EColor.gameObject.SetActive(typeUsed == 2);
  351. EColorText.gameObject.SetActive(typeUsed == 2);
  352. }
  353. foreach (var item in showObj)
  354. {
  355. if (int.Parse(item.Value.color) !=ColorType && ColorType != 0)
  356. {
  357. item.Key.SetActive(false);
  358. count--;
  359. }
  360. }
  361. SetCellShow(count, Cells);
  362. }
  363. /// <summary>
  364. /// 显示多少个格子
  365. /// </summary>
  366. /// <param name="self"></param>
  367. /// <param name="count">隐藏的数量</param>
  368. private void SetCellShow(int count, List<GameObject> cells)
  369. {
  370. int show = count;
  371. if (show > cells.Count)
  372. {
  373. show = show % 5;
  374. show = 5 - show;
  375. show = cells.Count - show;
  376. }
  377. for (int i = 0; i < cells.Count; i++)
  378. {
  379. if (i < show)
  380. {
  381. cells[i].gameObject.SetActive(false);
  382. }
  383. else
  384. {
  385. cells[i].gameObject.SetActive(true);
  386. }
  387. }
  388. }
  389. private void RefreshBagItem(Transform t, int type, int id)
  390. {
  391. itemPackage[id].BindTrans(t);
  392. Good config = ItemTypeData[type][id];
  393. //TODO:物品多语言;
  394. //itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
  395. //LanguageMatchManager.Instance.changeLanguage += () =>
  396. //{
  397. // itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
  398. //};
  399. //itemPackage[id].ENameTextMeshProUGUI.text = config.name;
  400. itemPackage[id].EIconImage.sprite =ItemIcon.GetSprite(config.id.ToString());
  401. itemPackage[id].uiTransform.SetAsFirstSibling();
  402. //设置穿戴功能 CharacterRender.addDress();
  403. t.GetComponent<Toggle>().group =EBagContent.content.GetComponent<ToggleGroup>();
  404. t.GetComponent<Toggle>().onValueChanged.AddListener(async (b) =>
  405. {
  406. //todo 装备
  407. if (b)
  408. {
  409. await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
  410. //ShowEquipName();
  411. }
  412. else
  413. {
  414. //if (!self.Unworn.Contains(config.id))
  415. //{
  416. // self.Unworn.Add(config.id);
  417. //}
  418. //CharacterManager.Inst.UnEquip(CharacterManager.Inst.selfRender, config, UserData.Instance.WornPackageItems);
  419. }
  420. });
  421. if (config.level == 1)
  422. {
  423. itemPackage[id].ENameColorImage.color = new Color(0.8f, 0.8f, 0.8f, 1);
  424. }
  425. if (config.level == 2)
  426. {
  427. itemPackage[id].ENameColorImage.color = new Color(0.82f, 0.98f, 0, 1);
  428. }
  429. if (config.level == 3)
  430. {
  431. itemPackage[id].ENameColorImage.color = new Color(0.63f, 0.82f, 1, 1);
  432. }
  433. if (config.level == 4)
  434. {
  435. itemPackage[id].ENameColorImage.color = new Color(0.392f, 0, 1, 1);
  436. itemPackage[id].ENameTextMeshProUGUI.color = new Color(0.82f, 0.98f, 0, 1);
  437. itemPackage[id].ELinkIconImage.gameObject.SetActive(true);
  438. }
  439. TypeItemClassGroup[type].Add(t.gameObject);
  440. }
  441. public void AddUIScrollItems(Dictionary<int , ItemPackage_View> dictionary, int count)
  442. {
  443. if (dictionary == null)
  444. {
  445. dictionary = new Dictionary<int, ItemPackage_View>();
  446. }
  447. if (count <= 0)
  448. {
  449. return;
  450. }
  451. if (count > dictionary.Count)
  452. {
  453. for (int i = dictionary.Count; i < count; i++)
  454. {
  455. ItemPackage_View itemServer = new ItemPackage_View();
  456. dictionary.Add(i, itemServer);
  457. }
  458. }
  459. else if (count < dictionary.Count)
  460. {
  461. for (int i = (dictionary.Count - 1); i >= count; i--)
  462. {
  463. dictionary[i].Dispose();
  464. dictionary.Remove(i);
  465. }
  466. }
  467. }
  468. /// <summary>显示装备名</summary>
  469. public void ShowEquipName()
  470. {
  471. List<Good> goods =new List<Good>();
  472. foreach (var item in CharacterManager.Inst.selfRender.dressRenders)
  473. {
  474. goods.Add(item.Value.good);
  475. }
  476. string text = null;
  477. for (int i = 0; i < goods.Count; i++)
  478. {
  479. if (i < 5)
  480. {
  481. text += $"-{goods[i].name}\r\n";
  482. }
  483. }
  484. if (goods.Count > 5)
  485. {
  486. text += "-and_more";
  487. }
  488. EClothName.text = text;
  489. }
  490. }