_xiaofang/xiaofang/Assets/Script/UI/PanelUI/MaterialPanel.cs
2024-12-12 15:21:07 +08:00

57 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MaterialPanel : MonoBehaviour
{
[Header("物体")]
public GameObject materialPrefab; // 物品项预制体
public Transform materialContent; // 用于显示物品的容器
public Button addBtn; // 增加物品数量按钮
public Button subtractBtn; // 减少物品数量按钮
// 用于存储所有生成的物品项
private List<MaterialItem> materialItems = new List<MaterialItem>();
public JSONReader jsonReader1;
// Start is called before the first frame update
void Start()
{
SetMaterialPrefab();
}
// Update is called once per frame
void Update()
{
}
// 初始化物品预制体,动态生成物品项
public void SetMaterialPrefab()
{
foreach (var materialData in jsonReader1.matialDictionary)
{
GameObject item = GameObject.Instantiate<GameObject>(materialPrefab, materialContent);
MaterialItem materialItem = item.GetComponent<MaterialItem>();
materialItem.materialNameText.text = materialData.Value.Note;
materialItem.materialId = materialData.Key.ToString();
materialItems.Add(materialItem);
}
}
// 获取所有物品的数量并提交
public void SubmitMaterialData()
{
createTemplateInfo.Instance.auth_CreateTemplate.materialList = new List<MaterialList>();
foreach (var materialItem in materialItems)
{
MaterialList materialList = new MaterialList();
materialList.materialId = materialItem.materialId;
Debug.Log("materialList.materialId--------------------" + materialList.materialId);
materialList.num = materialItem.materialNum;
createTemplateInfo.Instance.auth_CreateTemplate.materialList.Add(materialList);
Debug.Log(materialItem.materialName + " 数量: " + materialItem.GetMaterialNum());
}
}
}