自学内容网 自学内容网

Unity类银河恶魔城学习记录12-7-2 p129 Craft UI - part 2源代码

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();
            materialsSlotText.color = Color.white;
        }

        itemIcon.sprite = _data.icon;
        itemName.text = _data.itemName;
        itemDescription.text = _data.GetDescription();


        craftButton.onClick.AddListener(() => Inventory.instance.CanCraft(_data, _data.craftingMaterials));
    }
}

UI_CraftList.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 UI_CraftSlot[] craftSlots;//暂时保存将要删除的craftSlot

    private void Start()
    {
        //AssignCraftSlots();
        transform.parent.GetChild(0).GetComponent<UI_CraftList>().SetupCraftList();
        SetupDefaultCraftWindow();
    }

    //private void AssignCraftSlots()//获得当前所有的craftSlot函数//主要用来为清空做准备
    //{
    //    craftSlots = craftSlotParent.GetComponentsInChildren<UI_CraftSlot>();
    //}

    public void SetupCraftList()//将所有保存在其中的装备类型实例化craftslot并存入CraftList的函数
    {
        //AssignCraftSlots();

        for (int i = 0; i < craftSlotParent.childCount;i++)//删除所有原来存在于其list里的slot
        {
            Destroy(craftSlotParent.GetChild(i).gameObject);
        }

        //craftSlots = null;//清空此槽,方便下一次装其他的装备

        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();
    }

    public void SetupDefaultCraftWindow()
    {
        if (craftEquipment[0] != null)
        {
            GetComponentInParent<UI>().craftWindow.SetUpCraftWIndow(craftEquipment[0]);
        }
    }
}

UI_CraftSlot.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;

        if(itemText.text.Length > 12)
        {
            itemText.fontSize = 24 * .7f;
        }
        else
        {
            itemText.fontSize = 24;
        }
       
    }
    private void OnValidate()
    {
        //UpdateSlots(item);
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        ui.craftWindow.SetUpCraftWIndow(item.data as ItemData_Equipment);
    }
}
UI.cs
using UnityEngine;

public class UI : MonoBehaviour
{
    [SerializeField] private GameObject characterUI;
    [SerializeField] private GameObject skillTreeUI;
    [SerializeField] private GameObject craftUI;
    [SerializeField] private GameObject optionsUI;

    public UI_itemTooltip itemToolTip;
    public UI_statToolTip statToopTip;
    public UI_CraftWindow craftWindow;
    public void Start()
    {
        SwitchTo(null);
        itemToolTip.gameObject.SetActive(false);
        statToopTip.gameObject.SetActive(false);
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.C))
        {
            SwitchWithKeyTo(characterUI);
        }

        if(Input.GetKeyDown(KeyCode.B))
        {
            SwitchWithKeyTo(craftUI);
        }

        if(Input.GetKeyDown(KeyCode.K))
        {
            SwitchWithKeyTo(skillTreeUI);
        }

        if(Input.GetKeyDown(KeyCode.O))
        {
            SwitchWithKeyTo(optionsUI);
        }    
    }

    public void SwitchTo(GameObject _menu)//切换窗口函数
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            transform.GetChild(i).gameObject.SetActive(false);
        }

        if (_menu != null)
        {
            _menu.SetActive(true);
        }
    }

    public void SwitchWithKeyTo(GameObject _menu)//键盘切换窗口函数
    {
        if (_menu != null && _menu.activeSelf)//通过判断是否传入mune和mune是否激活来决定使设置为可视或不可使
        {
            _menu.SetActive(false);
            return;
        }

        SwitchTo(_menu);
    }
}


UI_itemSlot.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class UI_itemSlot : MonoBehaviour ,IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField] protected Image itemImage;
    [SerializeField] protected TextMeshProUGUI itemText;

    protected UI ui;

    public InventoryItem item;

    protected virtual void Start()
    {
        ui = GetComponentInParent<UI>();
    }

    public void UpdateSlots(InventoryItem _newItem)
    {
        item = _newItem;

        itemImage.color = Color.white;

        if (item != null)
        {
            itemImage.sprite = item.data.icon;

            if (item.stackSize > 1)
            {
                itemText.text = item.stackSize.ToString();

            }
            else
            {
                itemText.text = "";
            }
        }
    }

    public void CleanUpSlot()//解决出现UI没有跟着Inventory变化的bug
    {
        item = null;

        itemImage.sprite = null;
        itemImage.color = Color.clear;

        itemText.text = "";
    }

    public virtual void OnPointerDown(PointerEventData eventData)
    {
        if(item == null)//修复点击空白处会报错的bug
        {
            return;
        }

        if(Input.GetKey(KeyCode.LeftControl))
        {
            Inventory.instance.RemoveItem(item.data);
            ui.itemToolTip.HideToolTip();
            return;
        }

        if (item.data.itemType == ItemType.Equipment)
            Inventory.instance.EquipItem(item.data);

        ui.itemToolTip.HideToolTip();//修复已经装备了装备还会出现详细信息栏的bug
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (item == null || item.data == null)
            return;

        
        ui.itemToolTip.ShowToolTip(item.data as ItemData_Equipment);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (item == null || item.data == null)
            return;

        ui.itemToolTip.HideToolTip();
    }
}

UI_equipementSlots.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UI_equipementSlots : UI_itemSlot
{
    public EquipmentType slotType;//这怎么拿到的

    private void OnValidate()
    {
        gameObject.name = "Equipment slot -" + slotType.ToString();
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        if (item == null || item.data == null)//修复点击空白处会报错的bug
            return;
        //点击装备槽后卸下装备
        Inventory.instance.AddItem(item.data as ItemData_Equipment);
        Inventory.instance.Unequipment(item.data as ItemData_Equipment);

        ui.itemToolTip.HideToolTip();//修复已经装备了装备还会出现详细信息栏的bug

        CleanUpSlot();


    }
}


原文地址:https://blog.csdn.net/Homura_/article/details/137520831

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!