Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
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;
//给装备描述窗口以跟着鼠标移动的功能
Vector2 mousePosition = Input.mousePosition;
float xOffset = 0;
float yOffset = 0;
if (mousePosition.x > 600)
{
xOffset = -75;
}
else
{
xOffset = 75;
}
if (mousePosition.y > 320)
{
yOffset = -75;
}
else
{
yOffset = 75;
}
ui.itemToolTip.ShowToolTip(item.data as ItemData_Equipment);
ui.itemToolTip.transform.position = new Vector2(mousePosition.x + xOffset, mousePosition.y + yOffset);
}
public void OnPointerExit(PointerEventData eventData)
{
if (item == null || item.data == null)
return;
ui.itemToolTip.HideToolTip();
}
}
ItemData_Equipment.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EquipmentType
{
Weapon,
Armor,
Amulet,
Flask
}
[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment")]
public class ItemData_Equipment : ItemData
{
public EquipmentType equipmentType;
[Header("Unique effect")]
public float itemCooldown;
public ItemEffect[] itemEffects;
[TextArea]
public string itemEffectDescription;//给装备加上独特的描述
[Header("Major stats")]
public int strength; // 力量 增伤1点 爆伤增加 1% 物抗
public int agility;// 敏捷 闪避 1% 闪避几率增加 1%
public int intelligence;// 1 点 魔法伤害 1点魔抗
public int vitality;//加血的
[Header("Offensive stats")]
public int damage;
public int critChance; // 暴击率
public int critPower; //150% 爆伤
[Header("Defensive stats")]
public int health;
public int armor;
public int evasion;//闪避值
public int magicResistance;
[Header("Magic stats")]
public int fireDamage;
public int iceDamage;
public int lightingDamage;
[Header("Craft requirements")]
public List<InventoryItem> craftingMaterials;
public int descriptionLength;
public void AddModifiers()
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
playerStats.strength.AddModifier(strength);
playerStats.agility.AddModifier(agility);
playerStats.intelligence