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
- This topic has 0 replies, 1 voice, and was last updated 7 months, 3 weeks ago by Terence.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
May 2, 2024 at 3:29 pm #14476::
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<TextMeshProUGUI>(); if (!statValues) statValues = transform.GetChild(1).GetComponent<TextMeshProUGUI>(); // 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(); } }
-
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
Advertisement below: