创建健康栏 [英] Create a health bar
问题描述
我正在尝试创建可运行的健康栏,并且尝试了
I am trying to create a working health bar, and I have tried methods like
-
创建包含我创建的健康栏纹理的GUI.Box(),但这会使健康栏的大小调整为如下所示:
Creating GUI.Box() that contains the health bar texture that I have created, but this makes the health bar resize to scale like this:
使用 Texture2D.Resize()方法在没有 GUI.Box()的情况下调整纹理本身的大小,但是当我使用 Texture2D时.resize()方法,我的纹理变为黑色.看起来像这样:
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!
推荐答案
删除以 GUI ....开头的所有API,然后删除您的On GUI(){....}
函数.
Remove all API that starts with GUI.... then remove your On GUI(){....}
function.
滑块:
转到 GameObject-> UI->滑块.
从滑块中删除处理幻灯片区域.
然后通过脚本控制运行状况栏:
Then to control the health bar from script:
首先,添加using UnityEngine.UI;
public Slider healthBar;
//Initialize health bar
void Start()
{
healthBar.interactable = false;
healthBar.minValue = 0;
healthBar.maxValue = 100;
healthBar.value = 100;
}
然后您可以使用healthBar.value
更改健康栏.
You can then change your health bar with healthBar.value
.
用于袋装图像:
转到 GameObject-UI->图像.停止使用旧的GUI.
Go to GameObject-UI->Image. Stop using the old GUI.
https://unity3d.com/learn/tutorials/topics/user-界面用户界面
这篇关于创建健康栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!