问题
I am trying to create a working health bar, and I have tried methods like
Creating GUI.Box() that contains the health bar texture that I have created, but this makes the health bar resize to scale like this:
Resizing the texture itself without the GUI.Box(), using the Texture2D.Resize() Method, but when I used the Texture2D.Resize() method, my texture turned black. It looks like this:
My code looks like this. It was edited so that the irrelevant parts were deleted for ease of reading.
Why does my health bar turn black when I resize it? Are there any ways I can prevent this from happening? Thank you in advance!
回答1:
Remove all API that starts with GUI.... then remove your On GUI(){....}
function.
SLIDER:
Go to GameObject->UI->Slider.
Delete Handle Slide Area from the slider.
Then to control the health bar from script:
First, Include using UnityEngine.UI;
public Slider healthBar;
//Initialize health bar
void Start()
{
healthBar.interactable = false;
healthBar.minValue = 0;
healthBar.maxValue = 100;
healthBar.value = 100;
}
You can then change your health bar with healthBar.value
.
FOR THE BAG IMAGE:
Go to GameObject-UI->Image. Stop using the old GUI.
https://unity3d.com/learn/tutorials/topics/user-interface-ui
来源:https://stackoverflow.com/questions/36535318/create-a-health-bar