Unity类银河战士恶魔城学习总结(P131 Improve Item Slot 改进物品描述)
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/
本章节提升了物品栏的效果,同样地为物品效果加上了描述
UI_ItemSlot.cs
修改部分!!!
public void OnPointerEnter(PointerEventData eventData)
{
if(item == null)
return;
Vector2 mousePosition = Input.mousePosition;
float xOffset = 0;
float yOffset = 0;
if (mousePosition.x > 600)
xOffset = -150;
else
xOffset = 150;//鼠标靠近屏幕右侧时,提示框向左偏移;否则向右偏移
if (mousePosition.y > 320)
yOffset = -150;
else
yOffset = 150;
ui.itemToolTip.ShowToolTIp(item.data as ItemData_Equipment);//显示物品详情
ui.itemToolTip.transform.position = new Vector2(mousePosition.x + xOffset, mousePosition.y + yOffset);//更新提示框位置为鼠标位置偏移后的点
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//物品数量的UI,放在ItemSlot上
// p105添加了Grad Layout Group,自动排列
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>();//获取UI组件
}
public void UpdataSlot(InventoryItem _newitem)//更新物品槽
{
item = _newitem;// 传入的新物品
itemImage.color = Color.white;//获取了物品,itemslot透明变成白色,变得可见
if (item != null)//列表中的物体不是空的
{
itemImage.sprite = item.data.icon;//获得图标
if (item.stackSize > 1)
{
itemText.text = item.stackSize.ToString();//显示堆叠数量
}
else
{
itemText.text = "";//不显示数量
}
}
}
public void CleanUpSlot()清空物品槽
{
item = null;// 将物品槽内物品设为空
itemImage.sprite = null;// 清除物品图标
itemImage.color = Color.clear;// 让物品槽变得不可见
itemText.text = ""; // 清空堆叠数量的显示
}
public virtual void OnPointerDown(PointerEventData eventData)//拾取之后点击物品,改成virtual之后可以被子类重写
{
if (item == null)//如果物品槽为空
return;
if (Input.GetKey(KeyCode.LeftControl))
{
Inventory.instance.RemoveItem(item.data);//移除物品
return;
}
if (item.data.itemType == ItemType.Equipment)// 如果点击的物品是装备类型
Inventory.instance.EquipItem(item.data);//装备物品
ui.itemToolTip.HideToolTip();//隐藏物品详情
}
public void OnPointerEnter(PointerEventData eventData)
{
if(item == null)
return;
Vector2 mousePosition = Input.mousePosition;
float xOffset = 0;
float yOffset = 0;
if (mousePosition.x > 600)
xOffset = -150;
else
xOffset = 150;//鼠标靠近屏幕右侧时,提示框向左偏移;否则向右偏移
if (mousePosition.y > 320)
yOffset = -150;
else
yOffset = 150;
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)
return;
ui.itemToolTip.HideToolTip();//隐藏物品详情
}
}
ItemData_Equipment.cs
修改部分!!!
public override string GetDescription()
{
sb.Length = 0;//确保每次调用时是从头开始
descriptionLength = 0;//重置描述的属性数量
AddItemDescription(strength, "力量");
AddItemDescription(agility, "敏捷");
AddItemDescription(intelligence, "智力");
AddItemDescription(vitality, "活力");
AddItemDescription(damage, "伤害");
AddItemDescription(critChance, "暴击率");
AddItemDescription(critPower, "暴击伤害");
AddItemDescription(maxHealth, "生命值");
AddItemDescription(armor, "护甲");
AddItemDescription(evasion, "闪避");
AddItemDescription(magicResistance, "魔抗");
AddItemDescription(fireDamage, "火焰伤害");
AddItemDescription(iceDamage, "冰霜伤害");
AddItemDescription(lightningDamage, "闪电伤害");
if (descriptionLength < 5)//确保最终返回的描述文本至少有 5 行内容
{
for (int i = 0; i < 5 - descriptionLength; i++)
{
sb.AppendLine();
sb.Append("");//空行
}
}
if (itemEffectDescription.Length > 0)
{
sb.AppendLine();
sb.Append(itemEffectDescription);
}
return sb.ToString();//返回描述文本
}
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
//2024年11月1日
public enum EquipmentType
{
Weapon,
Armor,
Amulet,//护身符
Flask//药水瓶
}
[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment")]//因为继承了ItemData,所以在资源管理器中创建新的Item,创建的物品自带这个脚本
public class ItemData_Equipment : ItemData
{
public EquipmentType equipmentType;
[Header("独特效果")]
public float itemCoolDown;//物品冷却时间
public ItemEffect[] itemEffects;//可以添加物品的效果
[TextArea]
public string itemEffectDescription;//独特效果描述
[Header("主属性")]
public int strength;//力量,1点增加1攻击力和%1爆伤
public int agility;//敏捷,1点增加1%闪避和%1暴击率
public int intelligence;//智力,1点增加1法术强度和%1魔抗
public int vitality;//活力,1点增加3生命值
[Header("攻击属性")]
public int damage;
public int critChance;//暴击率
public int critPower;//暴击伤害,默认%150
[Header("防守属性")]
public int maxHealth;
public int armor;//护甲
public int evasion;//闪避
public int magicResistance;//魔抗
[Header("魔法属性")]
public int fireDamage;
public int iceDamage;
public int lightningDamage;
[Header("制作需求")]//Craft requirements
public List<InventoryItem> craftingMaterials;
private int descriptionLength;
public void Effect(Transform _enemyPositon)//执行物品效果
{
foreach (var item in itemEffects)
{
item.ExcuteEffect(_enemyPositon);
}
}
public void AddModifiers()
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
playerStats.strength.AddModifier(strength);
playerStats.agility.AddModifier(agility);
playerStats.intelligence.AddModifier(intelligence);
playerStats.vitality.AddModifier(vitality);
playerStats.damage.AddModifier(damage);
playerStats.critChance.AddModifier(critChance);
playerStats.critPower.AddModifier(critPower);
playerStats.maxHealth.AddModifier(maxHealth);
playerStats.armor.AddModifier(armor);
playerStats.evasion.AddModifier(evasion);
playerStats.magicResistance.AddModifier(magicResistance);
playerStats.fireDamage.AddModifier(fireDamage);
playerStats.iceDamage.AddModifier(iceDamage);
playerStats.lightingDamage.AddModifier(lightningDamage);
}
public void RemoveModifiers()
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
playerStats.strength.RemoveModifier(strength);
playerStats.agility.RemoveModifier(agility);
playerStats.intelligence.RemoveModifier(intelligence);
playerStats.vitality.RemoveModifier(vitality);
playerStats.damage.RemoveModifier(damage);
playerStats.critChance.RemoveModifier(critChance);
playerStats.critPower.RemoveModifier(critPower);
playerStats.maxHealth.RemoveModifier(maxHealth);
playerStats.armor.RemoveModifier(armor);
playerStats.evasion.RemoveModifier(evasion);
playerStats.magicResistance.RemoveModifier(magicResistance);
playerStats.fireDamage.RemoveModifier(fireDamage);
playerStats.iceDamage.RemoveModifier(iceDamage);
playerStats.lightingDamage.RemoveModifier(lightningDamage);
}
public override string GetDescription()
{
sb.Length = 0;//确保每次调用时是从头开始
descriptionLength = 0;//重置描述的属性数量
AddItemDescription(strength, "力量");
AddItemDescription(agility, "敏捷");
AddItemDescription(intelligence, "智力");
AddItemDescription(vitality, "活力");
AddItemDescription(damage, "伤害");
AddItemDescription(critChance, "暴击率");
AddItemDescription(critPower, "暴击伤害");
AddItemDescription(maxHealth, "生命值");
AddItemDescription(armor, "护甲");
AddItemDescription(evasion, "闪避");
AddItemDescription(magicResistance, "魔抗");
AddItemDescription(fireDamage, "火焰伤害");
AddItemDescription(iceDamage, "冰霜伤害");
AddItemDescription(lightningDamage, "闪电伤害");
if (descriptionLength < 5)//确保最终返回的描述文本至少有 5 行内容
{
for (int i = 0; i < 5 - descriptionLength; i++)
{
sb.AppendLine();
sb.Append("");//空行
}
}
if (itemEffectDescription.Length > 0)
{
sb.AppendLine();
sb.Append(itemEffectDescription);
}
return sb.ToString();//返回描述文本
}
private void AddItemDescription(int _value, string _name)
{
if (_value != 0)//判断该属性没有实际值
{
if (sb.Length > 0)
sb.AppendLine();//换行
if (_value > 0)
//sb.Append(_name+";"+_value);
sb.Append("+ " + _value + " " + _name);//添加属性描述
descriptionLength++;//每成功添加一个属性
}
}
}
原文地址:https://blog.csdn.net/suzh1qian/article/details/143836928
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!