Unity类银河恶魔城学习记录12-5 p127 Stat ToolTip源代码

时间:2024-04-06 09:22:05

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 UI_statToolTip statToopTip;

    public void Start()
    {
        //itemToolTip = characterUI.GetComponentInChildren<UI_itemTooltip>();
        //statToopTip = characterUI.GetComponentInChildren<UI_statToolTip>();
    }
    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_statTooltip.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class UI_statToolTip : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI description;

    public void ShowStatToolTip(string _text)
    {
        Debug.Log(3);
        if (_text == null)
            return;
        Debug.Log(4);
        description.text = _text;
        
        gameObject.SetActive(true);
    }

    public void HideStatToolTip()
    {
        description.text = "";
        gameObject.SetActive(false);
    }
}
UI_statslot.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class UI_statslot : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    private UI ui;

    [SerializeField] private StatType statType;
    [SerializeField] private TextMeshProUGUI statValueText;
    [SerializeField] private TextMeshProUGUI statNameText;

    [TextArea]
    [SerializeField] private string statDescription;
    
    private void OnValidate()
    {
        gameObject.name = "Stat - " + statType.ToString();

        if(statNameText != null)
        {
            statNameText.text = statType.ToString();
        }
    }

    private void Start()
    {
        UpdateStatValueUI();
        ui = GetComponentInParent<UI>();
    }

    public void UpdateStatValueUI()
    {
        PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();

        if(playerStats != null)
        {
            statValueText.text = playerStats.GetStats(statType).GetValue().ToString();

            if (statType == StatType.Health)
                statValueText.text = playerStats.GetMaxHealthValue().ToString();

            if (statType == StatType.damage)
                statValueText.text = (playerStats.damage.GetValue()+playerStats.strength.GetValue()).ToString();

            if (statType == StatType.critPower)
                statValueText.text = (playerStats.critPower.GetValue() + playerStats.strength.GetValue()).ToString();

            if (statType == StatType.critChance)
                statValueText.text = (playerStats.critChance.GetValue() + playerStats.agility.GetValue()).ToString();

            if (statType == StatType.evasion)
                statValueText.text = (playerStats.evasion.GetValue() + playerStats.agility.GetValue()).ToString();

            if (statType == StatType.magicResistance)
                statValueText.text = (playerStats.magicResistance.GetValue() + playerStats.intelligence.GetValue()*3).ToString();


        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (statDescription == null)
            return;
        Debug.Log("1");
        ui.statToopTip.ShowStatToolTip(statDescription);
    }

    public void OnPointerExit(PointerEventData eventData)
    {

        Debug.Log("2");
        ui.statToopTip.HideStatToolTip();
    }
}