Stannellc

Skill Tree UI

Aug 19th, 2026
8,806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 KB | Gaming | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using TMPro;
  5.  
  6. /// <summary>
  7. /// Manages the UI display for the skill tree, dynamically creating buttons
  8. /// and updating their state based on player progression.
  9. /// </summary>
  10. public class SkillTreeUI : MonoBehaviour
  11. {
  12.     [Header("UI References")]
  13.     [Tooltip("The parent transform where all skill buttons will be instantiated.")]
  14.     public Transform skillButtonParent;
  15.     [Tooltip("The prefab for a single skill button.")]
  16.     public GameObject skillButtonPrefab;
  17.     [Tooltip("Text element to display the player's available skill points.")]
  18.     public TextMeshProUGUI availableSkillPointsText;
  19.  
  20.     [Header("Skill Info Pop-up")]
  21.     [Tooltip("The panel that displays the skill information.")]
  22.     public GameObject skillInfoPopupPanel;
  23.     [Tooltip("Text element for the skill's name in the pop-up.")]
  24.     public TextMeshProUGUI skillNameText;
  25.     [Tooltip("Text element for the skill's description in the pop-up.")]
  26.     public TextMeshProUGUI skillDescriptionText;
  27.     [Tooltip("Text element for the skill's cost in the pop-up.")]
  28.     public TextMeshProUGUI skillCostText;
  29.     [Tooltip("The button to learn the skill from the pop-up.")]
  30.     public Button learnSkillButton;
  31.  
  32.     private SkillTreeManager skillTreeManager;
  33.     private PlayerProgression playerProgression;
  34.     private Dictionary<string, GameObject> skillButtons = new Dictionary<string, GameObject>();
  35.     private string activeSkillID;
  36.  
  37.     private void Start()
  38.     {
  39.         // Get references to the singleton managers
  40.         skillTreeManager = SkillTreeManager.Instance;
  41.         playerProgression = PlayerProgression.Instance;
  42.  
  43.         if (skillTreeManager == null || playerProgression == null)
  44.         {
  45.             Debug.LogError("Required managers not found. Ensure SkillTreeManager and PlayerProgression are in the scene.");
  46.             return;
  47.         }
  48.  
  49.         // Subscribe to events to refresh the UI
  50.         skillTreeManager.OnSkillLearned.AddListener(RefreshUI);
  51.         playerProgression.OnSkillPointChanged.AddListener(RefreshUI);
  52.  
  53.         PopulateSkillTreeUI();
  54.         RefreshUI();
  55.  
  56.         // Hide the pop-up panel at the start
  57.         if (skillInfoPopupPanel != null)
  58.         {
  59.             skillInfoPopupPanel.SetActive(false);
  60.         }
  61.     }
  62.  
  63.     private void OnDestroy()
  64.     {
  65.         // Unsubscribe from events to prevent memory leaks
  66.         if (skillTreeManager != null)
  67.         {
  68.             skillTreeManager.OnSkillLearned.RemoveListener(RefreshUI);
  69.         }
  70.         if (playerProgression != null)
  71.         {
  72.             playerProgression.OnSkillPointChanged.RemoveListener(RefreshUI);
  73.         }
  74.     }
  75.  
  76.     /// <summary>
  77.     /// Instantiates buttons for all skills defined in the Skill Tree ScriptableObject.
  78.     /// </summary>
  79.     private void PopulateSkillTreeUI()
  80.     {
  81.         // This line was updated to get the ScriptableObject directly from the manager.
  82.         SkillTree skillTreeData = skillTreeManager.GetComponent<SkillTreeManager>().skillTreeData;
  83.        
  84.         foreach (Skill skill in skillTreeData.skills)
  85.         {
  86.             GameObject newButton = Instantiate(skillButtonPrefab, skillButtonParent);
  87.             skillButtons[skill.skillID] = newButton;
  88.            
  89.             // Get the button's components
  90.             Button buttonComponent = newButton.GetComponent<Button>();
  91.             TextMeshProUGUI skillNameText = newButton.GetComponentInChildren<TextMeshProUGUI>();
  92.             Image skillIcon = newButton.GetComponentInChildren<Image>(); // Assuming the image is a child
  93.  
  94.             // Set the skill button's text and icon
  95.             if (skillNameText != null) skillNameText.text = skill.skillName;
  96.             if (skillIcon != null && skill.icon != null) skillIcon.sprite = skill.icon;
  97.            
  98.             // Add a listener to the button's click event
  99.             buttonComponent.onClick.AddListener(() => OnSkillButtonClicked(skill.skillID));
  100.         }
  101.     }
  102.  
  103.     /// <summary>
  104.     /// Refreshes the state of all skill buttons based on player progression.
  105.     /// </summary>
  106.     private void RefreshUI()
  107.     {
  108.         // Update the available skill points text
  109.         if (availableSkillPointsText != null)
  110.         {
  111.             availableSkillPointsText.text = $"Skill Points: {playerProgression.GetSkillPoints()}";
  112.         }
  113.  
  114.         foreach (var pair in skillButtons)
  115.         {
  116.             string skillID = pair.Key;
  117.             GameObject buttonObject = pair.Value;
  118.             Button buttonComponent = buttonObject.GetComponent<Button>();
  119.            
  120.             // Check if the skill has been learned
  121.             if (skillTreeManager.IsSkillLearned(skillID))
  122.             {
  123.                 // Skill is learned: Disable the button and change its appearance
  124.                 buttonComponent.interactable = false;
  125.                 buttonComponent.image.color = Color.gray; // Example: change color to gray
  126.             }
  127.             else
  128.             {
  129.                 // Skill is not learned: Check if it's learnable
  130.                 bool canLearn = CanLearnSkill(skillID);
  131.                 buttonComponent.interactable = canLearn;
  132.                 // Example: change color to green if learnable, red if not
  133.                 buttonComponent.image.color = canLearn ? Color.green : Color.red;
  134.             }
  135.         }
  136.     }
  137.    
  138.     /// <summary>
  139.     /// Checks if a skill is currently learnable (has prerequisites and skill points).
  140.     /// </summary>
  141.     private bool CanLearnSkill(string skillID)
  142.     {
  143.         Skill skillData = skillTreeManager.GetSkillData(skillID);
  144.         if (skillData == null) return false;
  145.  
  146.         // Check if player has enough skill points
  147.         if (playerProgression.GetSkillPoints() < skillData.skillPointCost)
  148.         {
  149.             return false;
  150.         }
  151.  
  152.         // Check if prerequisites are met
  153.         foreach (string prereqID in skillData.prerequisites)
  154.         {
  155.             if (!skillTreeManager.IsSkillLearned(prereqID))
  156.             {
  157.                 return false;
  158.             }
  159.         }
  160.  
  161.         return true;
  162.     }
  163.  
  164.     /// <summary>
  165.     /// Called when a skill button is clicked.
  166.     /// </summary>
  167.     /// <param name="skillID">The unique ID of the skill associated with the button.</param>
  168.     private void OnSkillButtonClicked(string skillID)
  169.     {
  170.         // Show the info pop-up instead of trying to learn immediately.
  171.         ShowSkillPopup(skillID);
  172.     }
  173.    
  174.     /// <summary>
  175.     /// Displays the skill info pop-up and populates it with data.
  176.     /// </summary>
  177.     /// <param name="skillID">The ID of the skill to display.</param>
  178.     public void ShowSkillPopup(string skillID)
  179.     {
  180.         Skill skillData = skillTreeManager.GetSkillData(skillID);
  181.         if (skillData == null) return;
  182.  
  183.         // Store the active skill ID to learn it later
  184.         activeSkillID = skillID;
  185.  
  186.         // Populate the pop-up with skill data
  187.         skillNameText.text = skillData.skillName;
  188.         skillDescriptionText.text = skillData.description;
  189.         skillCostText.text = $"Cost: {skillData.skillPointCost} SP";
  190.  
  191.         // Enable the "Learn" button if the skill is learnable
  192.         learnSkillButton.interactable = CanLearnSkill(skillID);
  193.  
  194.         // Show the panel
  195.         skillInfoPopupPanel.SetActive(true);
  196.     }
  197.  
  198.     /// <summary>
  199.     /// Called by the UI button to attempt learning the active skill.
  200.     /// </summary>
  201.     public void LearnSkillFromPopup()
  202.     {
  203.         skillTreeManager.TryLearnSkill(activeSkillID);
  204.         skillInfoPopupPanel.SetActive(false);
  205.     }
  206.  
  207.     /// <summary>
  208.     /// Hides the skill info pop-up.
  209.     /// </summary>
  210.     public void HideSkillPopup()
  211.     {
  212.         skillInfoPopupPanel.SetActive(false);
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment