123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using TMPro;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.U2D;
- using UnityEngine.UI;
- using YooAsset;
- using static UnityEngine.EventSystems.EventTrigger;
- public class FaceBuildSystem : MonoBehaviour
- {
- private ReferenceCollector re;
- private Button EFinish;
- private RectTransform EModel;
- private RectTransform EGShowClothName;
- private RectTransform ESNickName;
- private GameObject EColorText;
- private ScrollRect EColor;
- private ScrollRect EG_TypeFilter;
- private ScrollRect EBagContent;
- private Toggle Edress;
- private Toggle Ebody;
- private GameObject EItem_TypeFilter;
- private RectTransform EGTypeContent;
- private RectTransform EGColorItem;
- private RectTransform EGColorFilter;
- private TextMeshProUGUI EClothName;
- private SpriteAtlas ColorStyle;
- private SpriteAtlas ItemIcon;
- /// <summary>所有的物品</summary>
- public Dictionary<int, ItemPackage_View> itemPackage = new Dictionary<int, ItemPackage_View>();
- /// <summary>所有的物品信息</summary>
- public Dictionary<int, Good> standardPackageItems =new Dictionary<int, Good>();
- /// <summary>物品种类</summary>
- public Dictionary<int, List<GameObject>> TypeItemClassGroup = new Dictionary<int, List<GameObject>>();
- /// <summary>空格子</summary>
- public List<GameObject> Cells = new List<GameObject>();
- /// <summary>取消穿戴的装备</summary>
- public List<int> Unworn = new List<int>();
- public List<string> ColorList = new List<string>();
- public List<GameObject> ColorObjList = new List<GameObject>();
- public int ColorType;
- public int BodyType;
- public int ClothType;
- public Dictionary<int, List<Good>> ItemTypeData = new Dictionary<int, List<Good>>();
- public List<Good> ImageGoods = new List<Good>();
- public Dictionary<int, GameObject> ClassType = new Dictionary<int, GameObject>();
- private GameObject itempackage;
- private async void Start()
- {
- re = GetComponent<ReferenceCollector>();
- EFinish = re.Get<GameObject>("EFinish").GetComponent<Button>();
- EModel = re.Get<GameObject>("EModel").GetComponent<RectTransform>();
- EGShowClothName = re.Get<GameObject>("EGShowClothName").GetComponent<RectTransform>();
- ESNickName = re.Get<GameObject>("ESNickName").GetComponent<RectTransform>();
- EColorText = re.Get<GameObject>("EColorText");
- EColor = re.Get<GameObject>("EColor").GetComponent<ScrollRect>();
- EG_TypeFilter = re.Get<GameObject>("EG_TypeFilter").GetComponent<ScrollRect>();
- EBagContent = re.Get<GameObject>("EBagContent").GetComponent<ScrollRect>();
- Edress = re.Get<GameObject>("Edress").GetComponent<Toggle>();
- Ebody = re.Get<GameObject>("Ebody").GetComponent<Toggle>();
- EItem_TypeFilter = re.Get<GameObject>("EItem_TypeFilter");
- EGTypeContent = re.Get<GameObject>("EGTypeContent").GetComponent<RectTransform>();
- EGColorItem = re.Get<GameObject>("EGColorItem").GetComponent<RectTransform>();
- EGColorFilter = re.Get<GameObject>("EGColorFilter").GetComponent<RectTransform>();
- EClothName = re.Get<GameObject>("EClothName").GetComponent<TextMeshProUGUI>();
- EModel.GetComponent<RawImage>().texture = GameObject.Find("UICamera").GetComponent<Camera>().targetTexture;
- ColorStyle =await YooAssetManager.Instance.LoadAsset<SpriteAtlas>("colorItems");
- ItemIcon = await YooAssetManager.Instance.LoadAsset<SpriteAtlas>("ItemIcon");
- itempackage = await YooAssetManager.Instance.LoadAsset<GameObject>("Item_Package");
-
- List<Good> goods = JsonMapper.ToObject<List<Good>>(Resources.Load<TextAsset>("goodsData").text);
- for (int i = 0; i < goods.Count; i++)
- {
- goods[i].assetsType = goods[i].dressType;
- goods[i].dressType = (goods[i].id / 10000).ToString();
- }
- for (int i = 0; i < goods.Count; i++)
- {
- //if (goods[i].type == 1)
- //{
-
- //}
- ImageGoods.Add(goods[i]);
- }
- ItemTypeData = new Dictionary<int, List<Good>>();
- for (int i = 0; i < 17; i++)
- {
- TypeItemClassGroup.Add(i + 1, new List<GameObject>());
- }
- for (int i = 0; i < ImageGoods.Count; i++)
- {
- int type = ImageGoods[i].id / 10000;
- if (!ItemTypeData.ContainsKey(type))
- {
- ItemTypeData.Add(type, new List<Good>());
- }
- ItemTypeData[type].Add(ImageGoods[i]);
- if (!TypeItemClassGroup.ContainsKey(type))
- {
- TypeItemClassGroup.Add(type, new List<GameObject>());
- }
- }
- //foreach (var item in ItemTypeData)
- // {
- // Debug.Log(item.Key + "************" + item.Value.Count);
- // }
- AddCreatCell();
- CreatTypeItem();
- CreatColorItem();
- Init();
- Ebody.isOn = true;
- //Good config = ItemTypeData[10][6];
- //Good config2= ItemTypeData[11][2];
- //await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
- //await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config2);
- //ShowEquipName();
- }
- private void Init()
- {
- //身体
- Ebody.onValueChanged.AddListener((x) =>
- {
- foreach (var item in ClassType)
- {
- if (item.Key < 10)
- {
- item.Value.SetActive(x);
- }
- }
- if (x)
- {
- Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = Color.black;
- Filter(true);
- ClassType[1].GetComponent<Toggle>().isOn = true;
- }
- else
- {
- Ebody.transform.Find("lan_Ebody").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
- }
- });
- //服饰
- Edress.onValueChanged.AddListener((x) =>
- {
- foreach (var item in ClassType)
- {
- if (item.Key > 9)
- {
- item.Value.SetActive(x);
- }
- }
- if (x)
- {
- Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = Color.black;
- Filter(true);
- ClassType[10].GetComponent<Toggle>().isOn = true;
- }
- else
- {
- Edress.transform.Find("lan_Edress").GetComponent<TextMeshProUGUI>().color = new Color(1, 1, 1, 0.4f);
- }
- });
- }
- private void AddCreatCell()
- {
- int count = 15;
- GameObject obj = itempackage;
- for (int i = 0; i < count; i++)
- {
- GameObject go = GameObject.Instantiate(obj);
- Cells.Add(go);
- go.transform.SetParent(EBagContent.content, false);
- go.transform.SetAsLastSibling();
- go.GetComponent<Toggle>().enabled = false;
- go.SetActive(false);
- for (int j = 0; j < go.transform.childCount; j++)
- {
- go.transform.GetChild(j).gameObject.SetActive(false);
- }
- }
- }
- /// <summary>
- /// 创建身体部位类别
- /// </summary>
- /// <param name="self"></param>
- private void CreatTypeItem()
- {
- var classdir = JsonMapper.ToObject<Dictionary<string,ItemClassConfig>>(Resources.Load<TextAsset>("ItemClass").text);
- EItem_TypeFilter.gameObject.SetActive(false);
- Toggle toggle = null;
- foreach (var item in classdir)
- {
- GameObject go = GameObject.Instantiate(EItem_TypeFilter.gameObject);
- go.SetActive(true);
- go.GetComponent<Toggle>().group = EGTypeContent.GetComponent<ToggleGroup>();
- go.transform.SetParent(EGTypeContent.transform);
- go.transform.localPosition = Vector3.zero;
- go.transform.localScale = Vector3.one;
- var text = go.transform.Find("Image/Text").GetComponent<TextMeshProUGUI>();
- text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
- LanguageMatchManager.Instance.changeLanguage += () =>
- {
- text.text = LanguageMatchManager.Instance.GetLanByKey(item.Value.Id.ToString());
- };
- go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
- {
- if (x)
- {
- text.color = Color.black;
- if (item.Value.type == 1)
- {
- ClothType = item.Value.Id;
- }
- else
- {
- BodyType = item.Value.Id;
- }
- Filter(true, item.Value.Id);
- }
- else
- {
- text.color = new Color(1, 1, 1, 0.4f);
- }
- });
- if (item.Value.Id == 1)
- {
- go.GetComponent<Toggle>().isOn = true;
- }
- ClassType.Add(item.Value.Id, go);
- }
- }
- /// <summary>
- /// 创建颜色种类
- /// </summary>
- /// <param name="self"></param>
- private void CreatColorItem()
- {
- for (int i = 0; i <ColorObjList.Count; i++)
- {
- GameObject.Destroy(ColorObjList[i]);
- }
- ColorObjList.Clear();
- ColorList = ColorList.Distinct().ToList();
- if (ColorList.Count != 0)
- {
- ColorType = int.Parse(ColorList[0]);
- }
- var colordir = JsonMapper.ToObject<Dictionary<string, ItemColorConfig>>(Resources.Load<TextAsset>("ItemColor").text);
- EGColorItem.gameObject.SetActive(false);
- foreach (var item in colordir)
- {
- string color =ColorList.Find(x => x == item.Value.Id.ToString());
- if (string.IsNullOrEmpty(color)) continue;
- GameObject go = GameObject.Instantiate(EGColorItem.gameObject);
- go.SetActive(true);
- go.GetComponent<Toggle>().group =EGColorFilter.GetComponent<ToggleGroup>();
- go.transform.SetParent(EGColorFilter.transform);
- go.transform.localPosition = Vector3.zero;
- go.transform.localScale = Vector3.one;
- go.transform.GetChild(0).GetComponent<Image>().sprite =ColorStyle.GetSprite(item.Value.assetsName);
- go.GetComponent<Toggle>().onValueChanged.AddListener((x) =>
- {
- if (x)
- {
- ColorType = item.Value.Id;
- Filter();
- }
- else
- {
- }
- });
- ColorObjList.Add(go);
- }
- }
- private void Filter(bool isTypeSelect = false, int type = -1)
- {
- int count = 0;
- ColorList.Clear();
- Dictionary<GameObject, Good> showObj = new Dictionary<GameObject, Good>();
-
- if (type != -1)
- {
- if (TypeItemClassGroup[type].Count == 0 &&ItemTypeData.ContainsKey(type))
- {
- AddUIScrollItems(itemPackage, ItemTypeData[type].Count);
- GameObject obj = itempackage;
- for (int i = 0; i < ItemTypeData[type].Count; i++)
- {
- GameObject go = GameObject.Instantiate(obj);
- RefreshBagItem(go.transform, type, i);
- go.transform.SetParent(EBagContent.content);
- go.transform.localScale = Vector3.one;
- go.transform.localPosition = Vector3.zero;
- go.transform.SetAsFirstSibling();
- go.name = ItemTypeData[type][i].id.ToString();
- }
-
- }
- }
- int typeUsed = 0;
- if (Ebody.isOn)
- {
- typeUsed =BodyType;
- }
- else
- {
- typeUsed = ClothType;
- }
- Good dressItem = null;
- foreach (var item in TypeItemClassGroup)
- {
- var list = item.Value;
- for (int i = 0; i < list.Count; i++)
- {
- Good data = ItemTypeData[item.Key][i];
- ColorList.Add(data.color);
- showObj.Add(list[i], data);
- if (item.Key == typeUsed)
- {
- list[i].SetActive(true);
- count++;
- //todo 穿戴
- //if (UserData.Instance.TempWornPackageItems != null && UserData.Instance.TempWornPackageItems.Count != 0)
- //{
- // dressItem = UserData.Instance.TempWornPackageItems.Find(x => x.id == data.id);
- //}
- //else
- //{
- // dressItem = UserData.Instance.WornPackageItems.Find(x => x.id == data.id);
- //}
- }
- else
- {
- list[i].SetActive(false);
- ColorList.Remove(data.color);
- showObj.Remove(list[i]);
- }
- list[i].GetComponent<Toggle>().SetIsOnWithoutNotify(false);
- list[i].GetComponent<Toggle>().interactable = true;
- }
- }
- if (isTypeSelect)
- {
- if (typeUsed == 2)
- {
- CreatColorItem();
- }
- else
- {
- ColorType = 0;
- }
- EColor.gameObject.SetActive(typeUsed == 2);
- EColorText.gameObject.SetActive(typeUsed == 2);
- }
- foreach (var item in showObj)
- {
- if (int.Parse(item.Value.color) !=ColorType && ColorType != 0)
- {
- item.Key.SetActive(false);
- count--;
- }
- }
- SetCellShow(count, Cells);
- }
- /// <summary>
- /// 显示多少个格子
- /// </summary>
- /// <param name="self"></param>
- /// <param name="count">隐藏的数量</param>
- private void SetCellShow(int count, List<GameObject> cells)
- {
- int show = count;
- if (show > cells.Count)
- {
- show = show % 5;
- show = 5 - show;
- show = cells.Count - show;
- }
- for (int i = 0; i < cells.Count; i++)
- {
- if (i < show)
- {
- cells[i].gameObject.SetActive(false);
- }
- else
- {
- cells[i].gameObject.SetActive(true);
- }
- }
- }
- private void RefreshBagItem(Transform t, int type, int id)
- {
- itemPackage[id].BindTrans(t);
- Good config = ItemTypeData[type][id];
- //TODO:物品多语言;
- //itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
- //LanguageMatchManager.Instance.changeLanguage += () =>
- //{
- // itemPackage[id].ENameTextMeshProUGUI.text = LanguageMatchManager.Instance.GetLanByKey(config.name);
- //};
- //itemPackage[id].ENameTextMeshProUGUI.text = config.name;
- itemPackage[id].EIconImage.sprite =ItemIcon.GetSprite(config.id.ToString());
- itemPackage[id].uiTransform.SetAsFirstSibling();
- //设置穿戴功能 CharacterRender.addDress();
- t.GetComponent<Toggle>().group =EBagContent.content.GetComponent<ToggleGroup>();
- t.GetComponent<Toggle>().onValueChanged.AddListener(async (b) =>
- {
- //todo 装备
- if (b)
- {
- await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
- //ShowEquipName();
- }
- else
- {
- //if (!self.Unworn.Contains(config.id))
- //{
- // self.Unworn.Add(config.id);
- //}
- //CharacterManager.Inst.UnEquip(CharacterManager.Inst.selfRender, config, UserData.Instance.WornPackageItems);
- }
- });
- if (config.level == 1)
- {
- itemPackage[id].ENameColorImage.color = new Color(0.8f, 0.8f, 0.8f, 1);
- }
- if (config.level == 2)
- {
- itemPackage[id].ENameColorImage.color = new Color(0.82f, 0.98f, 0, 1);
- }
- if (config.level == 3)
- {
- itemPackage[id].ENameColorImage.color = new Color(0.63f, 0.82f, 1, 1);
- }
- if (config.level == 4)
- {
- itemPackage[id].ENameColorImage.color = new Color(0.392f, 0, 1, 1);
- itemPackage[id].ENameTextMeshProUGUI.color = new Color(0.82f, 0.98f, 0, 1);
- itemPackage[id].ELinkIconImage.gameObject.SetActive(true);
- }
- TypeItemClassGroup[type].Add(t.gameObject);
- }
- public void AddUIScrollItems(Dictionary<int , ItemPackage_View> dictionary, int count)
- {
- if (dictionary == null)
- {
- dictionary = new Dictionary<int, ItemPackage_View>();
- }
- if (count <= 0)
- {
- return;
- }
- if (count > dictionary.Count)
- {
- for (int i = dictionary.Count; i < count; i++)
- {
- ItemPackage_View itemServer = new ItemPackage_View();
- dictionary.Add(i, itemServer);
- }
- }
- else if (count < dictionary.Count)
- {
- for (int i = (dictionary.Count - 1); i >= count; i--)
- {
- dictionary[i].Dispose();
- dictionary.Remove(i);
- }
- }
- }
- /// <summary>显示装备名</summary>
- public void ShowEquipName()
- {
- List<Good> goods =new List<Good>();
- foreach (var item in CharacterManager.Inst.selfRender.dressRenders)
- {
- goods.Add(item.Value.good);
- }
- string text = null;
- for (int i = 0; i < goods.Count; i++)
- {
- if (i < 5)
- {
- text += $"-{goods[i].name}\r\n";
- }
- }
- if (goods.Count > 5)
- {
- text += "-and_more";
- }
- EClothName.text = text;
- }
- }
|