_xiaofang/xiaofang/Assets/Script/UI/PanelUI/MaterialPanel.cs

67 lines
2.4 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;
Debug.Log(materialData.Value.Note);
//Debug.Log("$$$$$$$$$$$$$$" + dutyItem.dutyNameText.text);
//materialItems.Add(item);// 将每个实例化的角色添加到列表中
}
//for (int i = 0; i < 8; i++) // 假设你有8个物品
//{
// GameObject item = Instantiate(materialPrefab, materialContent); // 实例化物品项
// MaterialItem materialItem = item.GetComponent<MaterialItem>(); // 获取物品项的脚本
// materialItems.Add(materialItem); // 将物品项添加到列表中
// // 设置物品的名称和其他数据(如果有的话)
// materialItem.materialName = "物品" + (i + 1); // 设置物品名称
// materialItem.materialNameText.text = materialItem.materialName; // 更新UI中的名称
//}
}
// 获取所有物品的数量并提交
public void SubmitMaterialData()
{
int index = 0;
foreach (var materialItem in materialItems)
{
createTemplateInfo.Instance.auth_CreateTemplate.materialList[index].materialId = materialItem.materialId;
createTemplateInfo.Instance.auth_CreateTemplate.materialList[index].num = materialItem.materialNum;
index++;
Debug.Log(materialItem.materialName + " 数量: " + materialItem.GetMaterialNum());
}
}
}