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 SpriteAtlas ColorStyle;
private SpriteAtlas ItemIcon;
/// 所有的物品
public Dictionary itemPackage = new Dictionary();
/// 所有的物品信息
public Dictionary standardPackageItems =new Dictionary();
/// 物品种类
public Dictionary> TypeItemClassGroup = new Dictionary>();
/// 空格子
public List Cells = new List();
/// 取消穿戴的装备
public List Unworn = new List();
public List ColorList = new List();
public List ColorObjList = new List();
public int ColorType;
public int BodyType;
public int ClothType;
public Dictionary> ItemTypeData = new Dictionary>();
public List ImageGoods = new List();
public Dictionary ClassType = new Dictionary();
private GameObject itempackage;
private async void Start()
{
re = GetComponent();
EFinish = re.Get("EFinish").GetComponent();
EModel = re.Get("EModel").GetComponent();
EGShowClothName = re.Get("EGShowClothName").GetComponent();
ESNickName = re.Get("ESNickName").GetComponent();
EColorText = re.Get("EColorText");
EColor = re.Get("EColor").GetComponent();
EG_TypeFilter = re.Get("EG_TypeFilter").GetComponent();
EBagContent = re.Get("EBagContent").GetComponent();
Edress = re.Get("Edress").GetComponent();
Ebody = re.Get("Ebody").GetComponent();
EItem_TypeFilter = re.Get("EItem_TypeFilter");
EGTypeContent = re.Get("EGTypeContent").GetComponent();
EGColorItem = re.Get("EGColorItem").GetComponent();
EGColorFilter = re.Get("EGColorFilter").GetComponent();
EModel.GetComponent().texture = GameObject.Find("UICamera").GetComponent().targetTexture;
ColorStyle =await YooAssetManager.Instance.LoadAsset("colorItems");
ItemIcon = await YooAssetManager.Instance.LoadAsset("ItemIcon");
itempackage = await YooAssetManager.Instance.LoadAsset("Item_Package");
List goods = JsonMapper.ToObject>(Resources.Load("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>();
for (int i = 0; i < 17; i++)
{
TypeItemClassGroup.Add(i + 1, new List());
}
for (int i = 0; i < ImageGoods.Count; i++)
{
int type = ImageGoods[i].id / 10000;
if (!ItemTypeData.ContainsKey(type))
{
ItemTypeData.Add(type, new List());
}
ItemTypeData[type].Add(ImageGoods[i]);
if (!TypeItemClassGroup.ContainsKey(type))
{
TypeItemClassGroup.Add(type, new List());
}
}
//foreach (var item in ItemTypeData)
// {
// Debug.Log(item.Key + "************" + item.Value.Count);
// }
AddCreatCell();
CreatTypeItem();
CreatColorItem();
Init();
Ebody.isOn = true;
}
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("Text (TMP)").GetComponent().color = Color.black;
Filter(true);
ClassType[1].GetComponent().isOn = true;
}
else
{
Ebody.transform.Find("Text (TMP)").GetComponent().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("Text (TMP)").GetComponent().color = Color.black;
Filter(true);
ClassType[10].GetComponent().isOn = true;
}
else
{
Edress.transform.Find("Text (TMP)").GetComponent().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().enabled = false;
go.SetActive(false);
for (int j = 0; j < go.transform.childCount; j++)
{
go.transform.GetChild(j).gameObject.SetActive(false);
}
}
}
///
/// 创建身体部位类别
///
///
private void CreatTypeItem()
{
var classdir = JsonMapper.ToObject>(Resources.Load("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().group = EGTypeContent.GetComponent();
go.transform.SetParent(EGTypeContent.transform);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
var text = go.transform.Find("Image/Text").GetComponent();
text.text = item.Value.Name;
go.GetComponent().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().isOn = true;
}
ClassType.Add(item.Value.Id, go);
}
}
///
/// 创建颜色种类
///
///
private void CreatColorItem()
{
for (int i = 0; i >(Resources.Load("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().group =EGColorFilter.GetComponent();
go.transform.SetParent(EGColorFilter.transform);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
go.transform.GetChild(0).GetComponent().sprite =ColorStyle.GetSprite(item.Value.assetsName);
go.GetComponent().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 showObj = new Dictionary();
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().SetIsOnWithoutNotify(false);
list[i].GetComponent().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);
}
///
/// 显示多少个格子
///
///
/// 隐藏的数量
private void SetCellShow(int count, List 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];
itemPackage[id].ENameTextMeshProUGUI.text = config.name;
itemPackage[id].EIconImage.sprite =ItemIcon.GetSprite(config.id.ToString());
itemPackage[id].uiTransform.SetAsFirstSibling();
//设置穿戴功能 CharacterRender.addDress();
t.GetComponent().group =EBagContent.content.GetComponent();
t.GetComponent().onValueChanged.AddListener(async (b) =>
{
//todo 装备
if (b)
{
await CharacterManager.Inst.ChangePlayerTex(CharacterManager.Inst.selfRender, config);
//self.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 dictionary, int count)
{
if (dictionary == null)
{
dictionary = new Dictionary();
}
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);
}
}
}
}