- fixed possible null on show tooltip of consumable item that was just used. - fixed an issue preventing all characters from being loaded on character list if one in between was pointing to a null reference in playerprefs
133 lines
4.2 KiB
C#
133 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Text;
|
|
|
|
namespace Kryz.CharacterStats.Examples
|
|
{
|
|
public class ItemTooltip : MonoBehaviour
|
|
{
|
|
public static ItemTooltip Instance;
|
|
|
|
[SerializeField] Text nameText;
|
|
[SerializeField] Text slotTypeText;
|
|
[SerializeField] Text sellPriceText;
|
|
[SerializeField] Text statsText;
|
|
|
|
private StringBuilder sb = new StringBuilder();
|
|
|
|
private string sellText = "Sell: ";
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
}
|
|
else
|
|
{
|
|
Destroy(this);
|
|
}
|
|
gameObject.SetActive(false);
|
|
}
|
|
private void ShowNonEquippableItemTooltip(Item itemToShow, bool fromVendor = false)
|
|
{
|
|
if (itemToShow == null) return;
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
nameText.text = itemToShow.ItemName;
|
|
slotTypeText.text = "";
|
|
if (fromVendor)
|
|
sellPriceText.text = sellText + itemToShow.sellPriceVendor.ToString();
|
|
else
|
|
sellPriceText.text = sellText + itemToShow.sellPricePlayer.ToString();
|
|
|
|
statsText.text = itemToShow.description;
|
|
}
|
|
public void ShowTooltip(Item itemToShow)
|
|
{
|
|
if (!(itemToShow is EquippableItem))
|
|
{
|
|
ShowNonEquippableItemTooltip(itemToShow);
|
|
return;
|
|
}
|
|
|
|
EquippableItem item = (EquippableItem)itemToShow;
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
nameText.text = item.ItemName;
|
|
slotTypeText.text = item.EquipmentType.ToString();
|
|
sellPriceText.text = sellText + item.sellPricePlayer.ToString();
|
|
|
|
sb.Length = 0;
|
|
|
|
AddStatText(item.StrengthBonus, " Strength");
|
|
AddStatText(item.AgilityBonus, " Agility");
|
|
AddStatText(item.IntelligenceBonus, " Intelligence");
|
|
AddStatText(item.SpiritBonus, " Spirit");
|
|
AddStatText(item.VitalityBonus, " Vitality");
|
|
|
|
AddStatText(item.StrengthPercentBonus * 100, "% Strength");
|
|
AddStatText(item.AgilityPercentBonus * 100, "% Agility");
|
|
AddStatText(item.IntelligencePercentBonus * 100, "% Intelligence");
|
|
AddStatText(item.SpiritPercentBonus * 100, "% Spirit");
|
|
AddStatText(item.VitalityPercentBonus * 100, "% Vitality");
|
|
|
|
statsText.text = sb.ToString();
|
|
}
|
|
public void ShowTooltipVendor(Item itemFromVendor)
|
|
{
|
|
if (!(itemFromVendor is EquippableItem))
|
|
{
|
|
ShowNonEquippableItemTooltip(itemFromVendor, true);
|
|
return;
|
|
}
|
|
|
|
EquippableItem item = (EquippableItem)itemFromVendor;
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
nameText.text = item.ItemName;
|
|
slotTypeText.text = item.EquipmentType.ToString();
|
|
sellPriceText.text = sellText + item.sellPriceVendor.ToString();
|
|
|
|
sb.Length = 0;
|
|
|
|
AddStatText(item.StrengthBonus, " Strength");
|
|
AddStatText(item.AgilityBonus, " Agility");
|
|
AddStatText(item.IntelligenceBonus, " Intelligence");
|
|
AddStatText(item.SpiritBonus, " Spirit");
|
|
AddStatText(item.VitalityBonus, " Vitality");
|
|
|
|
AddStatText(item.StrengthPercentBonus * 100, "% Strength");
|
|
AddStatText(item.AgilityPercentBonus * 100, "% Agility");
|
|
AddStatText(item.IntelligencePercentBonus * 100, "% Intelligence");
|
|
AddStatText(item.SpiritPercentBonus * 100, "% Spirit");
|
|
AddStatText(item.VitalityPercentBonus * 100, "% Vitality");
|
|
|
|
statsText.text = sb.ToString();
|
|
}
|
|
|
|
public void HideTooltip()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void AddStatText(float statBonus, string statName)
|
|
{
|
|
if (statBonus != 0)
|
|
{
|
|
if (sb.Length > 0)
|
|
sb.AppendLine();
|
|
|
|
if (statBonus > 0)
|
|
sb.Append("+");
|
|
|
|
sb.Append(statBonus);
|
|
sb.Append(statName);
|
|
}
|
|
}
|
|
}
|
|
}
|