简体   繁体   中英

Unity UI image.fill amount not partially displaying image

I am trying to implement a health bar into my game which has a maximum health of 1000. The health of 1000 is split over 20 squares which each represent 50 health. What happens is that the health bar controller script goes through each square and "x" amount of them full. Then "x+1" square is then filled partially depending on how much of the health there is remaining from the previous health squares which each have 50 health each. The remaining health squares are then not displayed and appear invisible. The problem that I am facing is that the health unit that partially displays is not partially displaying and is instead only displaying fully or not at all. I am aware that the code I have implemented into making the health units does work because as I use Debug.log for the partially filled health unit's fill amount, the fill amount is shown in the console to be set to a float. I am using the image component within the image UI Gameobject in order to alter the fillAmount of the health unit.

Here is my code to help:

Code for health bar itself:

    public void Update_Health(int health)
    {
        //HEALTH UNITS ARE IMAGE OBJECTS THAT ARE CREATED BY PREFABS AND STORED IN HEALTH UNITS ARRAY SHOWN BELOW:
        int number_full = health / 50;
        for (int y = 0; y < number_full; y++)
        {
            health_units[y].GetComponent<Health_Unit_Controller>().Display_full();
        }

        if (number_full != 20)//USED FOR THE PARTIALLY DISPLAYED HEALTH UNIT
        {
            int number_remaining = 20 - number_full;
            int remainder_health = health - (number_full * 50);

            health_units[number_full].GetComponent<Health_Unit_Controller>().Display_partially(remainder_health);

            for (int a = (20 - number_remaining); a < 20; a++)
            {
                health_units[a].GetComponent<Health_Unit_Controller>().Display_partially(0);
            }
        }

    }

Code for each health unit:

public class Health_Unit_Controller : MonoBehaviour, I_Unit
{
    private Image image_componenet;//getting the image componenet from the image UI object.

    void Start()
    {
        image_componenet = GetComponent<Image>();
    }

    public void Display_full()
    {
        image_componenet.fillAmount = 1;
    }

    public void Display_partially(int health_remaining)
    {
        if (health_remaining != 0)
        {
            image_componenet.fillAmount = health_remaining / 50.0f;
            Debug.Log("health_remaining: " + image_componenet.fillAmount);
        }
        else
        {
            image_componenet.fillAmount = 0;
        }
    }

}

Here is some diagrams to help: 在此处输入图片说明

In this image, the red squares represent the health units. As shown above 2 health units have gone invisible. 1 of those 2 health units are supposed to be invisible. But the other health unit has also gone fully invisible even though the fill amount shown by the console indicates the fill amount of the 2nd health unit should be 0.8f. Instead what has happened is that the 2nd health unit is not appearing to show at all.

have you tried simplifying the code? You are storing many variables, it may be better to run all of this through one function.

public void DisplayHealth(int health){
    int hp = health;
    for (int i = 0; i < 20 (or health_units length); i++) {
         health_units[i].GetComponent<Health_Unit_Controller>().Display_partially(health);
         health -= 50;
}

also I would change your partially function from

public void Display_partially(int health_remaining)
{
    if (health_remaining != 0)
    {
        image_componenet.fillAmount = health_remaining / 50.0f;
        Debug.Log("health_remaining: " + image_componenet.fillAmount);
    }

into

public void Display_partially(int health_remaining)
{
    if (health_remaining > 50)
    {
        image_componenet.fillAmount = 1
    }

    else if (health_remaining <= 0)
    {
        image_componenet.fillAmount = 0f;
    }
    else
    {
        image_componenet.fillAmount = health_remaining / 50f;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM