Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
UI_CraftWindow.cs
using UnityEngine.UI;
using TMPro;
using UnityEngine;
using Unity.VisualScripting;
public class UI_CraftWindow : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI itemName;
[SerializeField] private TextMeshProUGUI itemDescription;
[SerializeField] private Image itemIcon;
[SerializeField] private Button craftButton;
[SerializeField] private Image[] materialsImage;
public void SetUpCraftWIndow(ItemData_Equipment _data)
{
craftButton.onClick.RemoveAllListeners();//防止出现点击Button出现其他的函数
for (int i = 0; i < materialsImage.Length; i++)//将所有的UI设置为clear颜色
{
materialsImage[i].color = Color.clear;
materialsImage[i].GetComponentInChildren<TextMeshProUGUI>().color = Color.clear;
}
for(int i = 0;i < _data.craftingMaterials.Count; i++)
{
if(_data.craftingMaterials.Count > materialsImage.Length)
{
Debug.LogWarning("材料比格子数还多了");
}
materialsImage[i].sprite = _data.craftingMaterials[i].data.icon;
materialsImage[i].color = Color.white;
TextMeshProUGUI materialsSlotText = materialsImage[i].GetComponentInChildren<TextMeshProUGUI>();
materialsSlotText.text = _data.craftingMaterials[i].stackSize.ToString();