Create a health bar

喜夏-厌秋 提交于 2019-12-04 07:02:36

问题


I am trying to create a working health bar, and I have tried methods like

  1. Creating GUI.Box() that contains the health bar texture that I have created, but this makes the health bar resize to scale like this:

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!