Forum begins after the advertisement:


[Part 18] Hiding unboosted stats in the UI

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 18] Hiding unboosted stats in the UI

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #14476
    Terence
    Keymaster

    Hi everyone,

    A Patron is asking how we hide unboosted stats in the UI window. You can update the UpdateStatFields() function with the following line (highlighted in green):

        public void UpdateStatFields()
        {
            if (!player) return;
    
            // Get a reference to both Text objects to render stat names and stat values.
            if (!statNames) statNames = transform.GetChild(0).GetComponent();
            if (!statValues) statValues = transform.GetChild(1).GetComponent();
    
            // Render all stat names and values.
            // Use StringBuilders so that the string manipulation runs faster.
            StringBuilder names = new StringBuilder();
            StringBuilder values = new StringBuilder();
    
            // Add the current health to the stat box.
            if(displayCurrentHealth)
            {
                names.AppendLine("Health");
                values.AppendLine(player.CurrentHealth.ToString());
            }
    
            FieldInfo[] fields = typeof(CharacterData.Stats).GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                // Render stat names.
                names.AppendLine(field.Name);
    
                // Get the stat value.
                object val = field.GetValue(player.Stats);
                float fval = val is int ? (int)val : (float)val;
    
                // Print it as a percentage if it has an attribute assigned and is a float.
                PropertyAttribute attribute = (PropertyAttribute)PropertyAttribute.GetCustomAttribute(field, typeof(PropertyAttribute));
                if (attribute != null && field.FieldType == typeof(float))
                {
                    float percentage = Mathf.Round(fval * 100 - 100);
    
                    // If the stat value is 0, just put a dash.
                    if (Mathf.Approximately(percentage, 0))
                    {
                        values.Append('-').Append('\n');
                    }
                    else
                    {
                        if (percentage > 0)
                            values.Append('+');
                        values.Append(percentage).Append('%').Append('\n');
                    }
                }
                else
                {
                    if(Mathf.Approximately(fval, 1)) values.Append('-').Append('\n');
                    else values.Append(fval).Append('\n');
                }
    
                // Updates the fields with the strings we built.
                statNames.text = PrettifyNames(names);
                statValues.text = values.ToString();
            }
        }
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: