Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
UI_CraftList.cs
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UI_CraftList : MonoBehaviour,IPointerDownHandler
{
[SerializeField] private GameObject craftSlotPrefab;//创建craftSlot的预制体
[SerializeField] private List<ItemData_Equipment> craftEquipment;//一个将要导进craftList的data类型组
[SerializeField] private Transform craftSlotParent;//便于寻找将要删除的craftSlot的父组件
[SerializeField] private List<UI_CraftSlot> craftSlots;//暂时保存将要删除的craftSlot
private void Start()
{
AssignCraftSlots();
}
private void AssignCraftSlots()//获得当前所有的craftSlot函数//主要用来为清空做准备
{
for (int i = 0; i < craftEquipment.Count; i++)
{
craftSlots.Add(craftSlotParent.GetChild(i).GetComponent<UI_CraftSlot>());
}
}
public void SetupCraftList()//将所有保存在其中的装备类型实例化craftslot并存入CraftList的函数
{
AssignCraftSlots();
for (int i = 0; i < craftSlots.Count;i++)//删除所有原来存在于其list里的slot
{
Destroy(craftSlots[i].gameObject);
Debug.Log(1);
}
craftSlots = new List<UI_CraftSlot>();//清空此槽,方便下一次装其他的装备
for(int i = 0;i < craftEquipment.Count;i++)
{
GameObject newSlot = Instantiate(craftSlotPrefab, craftSlotParent);//创建的实例往craftPartent里塞
newSlot.GetComponent<UI_CraftSlot>().SetUpCraftSlot(craftEquipment[i]);
}
}
public void OnPointerDown(PointerEventData eventData)
{
SetupCraftList();
}
}
UI_CraftSlot.cs
cs
using UnityEngine.EventSystems;
public class UI_CraftSlot : UI_itemSlot
{
protected override void Start()
{
base.Start();
}
public void SetUpCraftSlot(ItemData_Equipment _data)//设置CraftSlot的公开函数
{
if (_data == null)
return;
item.data = _data;
itemImage.sprite = _data.icon;
itemText.text = _data.name;
}
private void OnValidate()
{
//UpdateSlots(item);
}
public override void OnPointerDown(PointerEventData eventData)
{
ItemData_Equipment craftData = item.data as ItemData_Equipment;
Inventory.instance.CanCraft(craftData, craftData.craftingMaterials);
}
}