FaceBuildSystem.cs 16 KB

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