using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UIElements; using static UnityEditor.Experimental.GraphView.GraphView; public class HealthGUIController : MonoBehaviour { private List players; public List images; private UIDocument uiDocument; private List playerLifeList; private List playerStressBarList; void Awake() { players = Object.FindObjectsByType(FindObjectsSortMode.InstanceID).ToList(); uiDocument ??= GetComponent(); if (uiDocument == null) { return; } var root = uiDocument.rootVisualElement; if (root == null) { return; } playerLifeList = new List() { new Image(), new Image(), new Image(), new Image() }; playerStressBarList = new List() { new ProgressBar(), new ProgressBar(), new ProgressBar(), new ProgressBar() }; playerLifeList[0] = root.Q("Player1Life"); playerLifeList[1] = root.Q("Player2Life"); playerLifeList[2] = root.Q("Player3Life"); playerLifeList[3] = root.Q("Player4Life"); playerLifeList[0].tintColor = players[0].gameObject.GetComponent() ? players[0].gameObject.GetComponent().color : Color.white; playerLifeList[1].tintColor = players[1].gameObject.GetComponent() ? players[1].gameObject.GetComponent().color : Color.white; playerLifeList[2].tintColor = players[2].gameObject.GetComponent() ? players[2].gameObject.GetComponent().color : Color.white; playerLifeList[3].tintColor = players[3].gameObject.GetComponent() ? players[3].gameObject.GetComponent().color : Color.white; playerStressBarList[0] = root.Q("Player1StressBar"); playerStressBarList[1] = root.Q("Player2StressBar"); playerStressBarList[2] = root.Q("Player3StressBar"); playerStressBarList[3] = root.Q("Player4StressBar"); } // Update is called once per frame void Update() { for (int i = 0; i < players.Count; i++) { //update stress bar float ratio = Mathf.Clamp01(players[i].currentStress / players[i].maxStress); // Update the bar's value (0-100) playerStressBarList[i].value = ratio * 100f; //update sprite playerLifeList[i].sprite = players[i].currentStress switch { >= 0 and <= 33 => images[0], > 33 and <= 66 => images[1], > 66 and <= 99 => images[2], _ => images[0] }; if (players[i].isParalyzed) { playerLifeList[i].sprite = images[3]; } else if (players[i].isPanicking) { playerLifeList[i].sprite = images[4]; } } } }