首页 > 其他分享 >Unity类银河恶魔城学习记录12-4 p126 Item Tooltip源代码

Unity类银河恶魔城学习记录12-4 p126 Item Tooltip源代码

时间:2024-04-05 18:31:38浏览次数:25  
标签:p126 12 int playerStats item AddItemDescription using 源代码 public

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

UI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UI : MonoBehaviour
{
    [SerializeField] private GameObject characterUI;
    public UI_itemTooltip itemTooltip;
    public void Start()
    {
        itemTooltip = characterUI.GetComponentInChildren<UI_itemTooltip>();
    }
    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);
        }
    }
}


UI_itemTooltip.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class UI_itemTooltip : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI itemNameText;
    [SerializeField] private TextMeshProUGUI itemTypeText;
    [SerializeField] private TextMeshProUGUI itemDescription;

    [SerializeField] private int defaultFontSize = 32;

    public void ShowToolTip(ItemData_Equipment item)//将item里面的数据读取到对应的text上并显示此组件
    {
        if (item == null)
            return;

        itemNameText.text = item.itemName;
        itemTypeText.text = item.equipmentType.ToString();
        itemDescription.text = item.GetDescription();

        if (itemNameText.text.Length > 12)//使标题超过最大尺寸时字体缩小
        {
            itemNameText.fontSize = itemNameText.fontSize * .7f;
        }
        else
        {
            itemNameText.fontSize = defaultFontSize;
        }

        gameObject.SetActive(true);
    }

    public void HideToolTip()//关闭此组件
    {
        itemNameText.fontSize = defaultFontSize;//防止字体越来越小
        gameObject.SetActive(false);
    }
}
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] private Image itemImage;
    [SerializeField] private TextMeshProUGUI itemText;

    private UI ui;

    public InventoryItem item;

    private 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);
            return;
        }

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

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

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;

    public float itemCooldown;

    public ItemEffect[] itemEffects;



    [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 int descriptionLength;

    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.Health.AddModifier(health);
        playerStats.armor.AddModifier(armor);
        playerStats.evasion.AddModifier(evasion);
        playerStats.magicResistance.AddModifier(magicResistance);

        playerStats.fireDamage.AddModifier(fireDamage);
        playerStats.iceDamage.AddModifier(iceDamage);
        playerStats.lightingDamage.AddModifier(lightingDamage);
    }

    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.Health.RemoveModifier(health);
        playerStats.armor.RemoveModifier(armor);
        playerStats.evasion.RemoveModifier(evasion);
        playerStats.magicResistance.RemoveModifier(magicResistance);

        playerStats.fireDamage.RemoveModifier(fireDamage);
        playerStats.iceDamage.RemoveModifier(iceDamage);
        playerStats.lightingDamage.RemoveModifier(lightingDamage);

    }

    public void Effect(Transform _enemyPosition)//循环调用Effect类里的Effect的函数
    {
        foreach(var item in itemEffects)
        {
            item.ExecuteEffect(_enemyPosition);
        }
    }
    public override string GetDescription()//让外界能够拿到拼出字符串的函数
    {
        sb.Length = 0;
        descriptionLength = 0;


        AddItemDescription(strength, "strength");
        AddItemDescription(agility, "agility");
        AddItemDescription(intelligence, "intelligence");
        AddItemDescription(vitality, "vitality");

        AddItemDescription(damage, "damage");
        AddItemDescription(critChance, "critChance");
        AddItemDescription(critPower, "critPower");

        AddItemDescription(health, "health");
        AddItemDescription(evasion, "evasion");
        AddItemDescription(armor, "armor");
        AddItemDescription(magicResistance, "magicResistance");

        AddItemDescription(fireDamage, "fireDamage");
        AddItemDescription(iceDamage, "iceDamage");
        AddItemDescription(lightingDamage, "lightingDamage");

        Debug.Log("In GetDescription");

        if(descriptionLength < 5)//使窗口拥有最小尺寸
        {
            for(int i = 0;i < 5- descriptionLength;i++)
            {
                sb.AppendLine();
                sb.Append("");
            }
        }

        return sb.ToString();
        
    }

    private void AddItemDescription(int _value,string _name)//通过判断拼出想要的字符串的函数
    {
        if(_value != 0)
        {
            Debug.Log(1);
            if(sb.Length >= 0)
                sb.AppendLine();//这是控制行数的
            if(_value > 0)
                sb.Append(" + "+_value + " " + _name);//这是实打实添加字符串的

            descriptionLength++;
            
        }
    }
}

ItemData.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public enum ItemType
{
    Material,
    Equipment
}

[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Item")]
public class ItemData : ScriptableObject 
{
    public ItemType itemType;
    public string itemName;
    public Sprite icon;//图标

    [Range(0,100)]
    public float dropChance;

    protected StringBuilder sb = new StringBuilder();

    public virtual string GetDescription()
    {
        return "";
    }
}

标签:p126,12,int,playerStats,item,AddItemDescription,using,源代码,public
From: https://blog.csdn.net/Homura_/article/details/137397188

相关文章

  • Unity类银河恶魔城学习记录12-5 p127 Stat ToolTip源代码
    Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考此代码仅为较上一P有所改变的代码【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibiliUI.csusingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngin......
  • 12种常见的软件架构风格
    什么是软件架构?软件架构是定义软件系统的高级结构和组织的过程。它涉及识别和选择正确的组件,决定它们之间如何交互,以及确定它们应该如何组织以实现特定的目标。软件架构的目标是创建一个可维护、可扩展和安全的系统,能够满足用户和组织的需求。为什么需要软件架构?强大的架......
  • 基于SpringBoot+Vue的美食烹饪互动平台附带文章和源代码部署视频讲解等
    在这里插入图片描述@toc前言......
  • P3052 [USACO12MAR] Cows in a Skyscraper G
    原题链接题解模拟,遍历n个物品,一开始一个箱子不给,遍历到某个物品时,先把所有已经给了的箱子放进去试试,再创一个新箱子放进去试试code#include<bits/stdc++.h>usingnamespacestd;intn,w;intcnt,ans;intchongdie=0;intbox[20],c[20];voidmoni(intnow,intcnt)//now......
  • 如何搭建一个免费的源代码托管工具?
    代码是企业的核心资产,每个用软件创造价值的企业有需要有一个源代码托管平台来存储企业的核心资产,这样既能方便公司内部人员通过协作来进行软件研发,又能对所有的研发过程做到版本控制、安全审计等。市面上能做到这些的产品又不少,但是在最终选择之前必须考虑以下几个因素:是......
  • 12、selenium框架之Cookie操作
    一、查看浏览器cookiecookie、session、token的区别:cookie存储在浏览器本地客户端,发送的请求携带cookie时可以实现登录操作。session存放在服务器。token应用于应用程序。F12查看浏览器cookie:二、cookie基本操作1、获取cookie:fromseleniumimportwebdriverfromsele......
  • C112 莫队算法 P1494 [国家集训队] 小 Z 的袜子
    视频链接:  LuoguP1494[国家集训队]小Z的袜子//普通莫队O(n*sqrt(n))#include<iostream>#include<cstring>#include<algorithm>#include<cmath>usingnamespacestd;constintN=50005;intn,m,B,a[N];intsum,cnt[N],ans1[N],ans2[N];str......
  • 【leetcode面试经典150题】12.O(1) 时间插入、删除和获取随机元素(C++)
    【leetcode面试经典150题】专栏系列将为准备暑期实习生以及秋招的同学们提高在面试时的经典面试算法题的思路和想法。本专栏将以一题多解和精简算法思路为主,题解使用C++语言。(若有使用其他语言的同学也可了解题解思路,本质上语法内容一致)【题目描述】实现RandomizedSet 类:......
  • python3.12.2银河麒麟v10鲲鹏离线快速部署
    python3.12.2银河麒麟v10鲲鹏离线快速部署背景清明假期忙活了一整天发现自己方向走错了.部署效率巨慢无比.其实简单情况下很快就可以弄好.自己最开始使用python3.9使用的是libressl发现最新版已经不需要了.并且使用仓库中的就可以.系统版本说明公司的银河麒麟v10......
  • goDbClient开源代码--通用查询
    开源地址: https://gitee.com/ichub/godbclient.git通用查询funcTest0010_SelectDptSub(t*testing.T){vardbRequest=NewPageDbRequest(2)dbRequest.TableName="department"dbRequest.Eq("id",1)dbRequest.NewSubTable()dbR......