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.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_Equipment.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 "";
}
}